Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 919f571

Browse files
committed
Add unit test for PR refresh issue.
1 parent c770992 commit 919f571

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Reflection;
2+
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
34

45
[assembly: AssemblyTitle("GitHub.VisualStudio")]
56
[assembly: AssemblyDescription("GitHub for Visual Studio VSPackage")]
6-
[assembly: Guid("fad77eaa-3fe1-4c4b-88dc-3753b6263cd7")]
7+
[assembly: Guid("fad77eaa-3fe1-4c4b-88dc-3753b6263cd7")]
8+
[assembly: InternalsVisibleTo("UnitTests")]
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using GitHub.Api;
2+
using GitHub.Exports;
3+
using GitHub.Models;
4+
using GitHub.Primitives;
5+
using GitHub.Services;
6+
using GitHub.UI;
7+
using GitHub.VisualStudio;
8+
using GitHub.VisualStudio.UI.Views;
9+
using NSubstitute;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Collections.ObjectModel;
13+
using System.ComponentModel.Composition;
14+
using System.ComponentModel.Design;
15+
using System.Linq;
16+
using System.Reactive.Linq;
17+
using UnitTests;
18+
using Xunit;
19+
20+
public class GitHubPaneViewModelTests : TestBaseClass
21+
{
22+
private readonly IServiceProvider serviceProvider;
23+
private readonly IUIController uiController;
24+
private readonly FakeMenuCommandService menuCommandService;
25+
private readonly GitHubPaneViewModel viewModel;
26+
private UIViewType lastUiControllerJump;
27+
28+
public GitHubPaneViewModelTests()
29+
{
30+
#region Start tests already logged in with an active repo.
31+
32+
var repositoryHosts = Substitutes.RepositoryHosts;
33+
repositoryHosts.IsLoggedInToAnyHost.Returns(true);
34+
35+
var teamExplorerServiceHolder = Substitute.For<ITeamExplorerServiceHolder>();
36+
var activeRepo = Substitute.For<ISimpleRepositoryModel>();
37+
activeRepo.CloneUrl.Returns(new UriString("https://github.com/foo/foo"));
38+
teamExplorerServiceHolder
39+
.When(_ => _.Subscribe(Arg.Any<object>(), Arg.Any<Action<ISimpleRepositoryModel>>()))
40+
.Do(_ =>
41+
{
42+
var invokeAction = _.Arg<Action<ISimpleRepositoryModel>>();
43+
invokeAction(activeRepo);
44+
});
45+
46+
var connectionManager = Substitutes.ConnectionManager;
47+
var connection = Substitutes.Connection;
48+
var connectionHostAddress = HostAddress.Create(activeRepo.CloneUrl.ToString());
49+
connection.HostAddress.Returns(connectionHostAddress);
50+
connectionManager.Connections.Returns(new ObservableCollection<IConnection>(new[] {
51+
connection
52+
}));
53+
connection.Login().Returns(Observable.Return(connection));
54+
55+
var host = Substitute.For<IRepositoryHost>();
56+
host.IsLoggedIn.Returns(true);
57+
repositoryHosts.LookupHost(connectionHostAddress).Returns(host);
58+
59+
#endregion
60+
61+
#region Wire up service/UI provider.
62+
63+
serviceProvider = Substitutes.GetFullyMockedServiceProvider();
64+
menuCommandService = new FakeMenuCommandService();
65+
serviceProvider.GetService(typeof(IMenuCommandService)).Returns(menuCommandService);
66+
67+
// TODO: Maybe could avoid cast by making a new interface that inherits IServiceProvider and IUIProvider
68+
var uiProvider = serviceProvider as IUIProvider;
69+
uiProvider.TryGetService(typeof(IUIProvider)).Returns(serviceProvider);
70+
71+
#endregion
72+
73+
uiController = Substitute.For<IUIController>();
74+
uiController.CurrentFlow.Returns(UIControllerFlow.PullRequests);
75+
uiController.SelectedFlow.Returns(UIControllerFlow.PullRequests);
76+
uiController
77+
.When(_ => _.Jump(Arg.Any<ViewWithData>()))
78+
.Do(_ => lastUiControllerJump = _.Arg<ViewWithData>().ViewType);
79+
80+
var exportFactoryProvider = Substitutes.ExportFactoryProvider;
81+
uiProvider.TryGetService(typeof(IExportFactoryProvider)).Returns(exportFactoryProvider);
82+
exportFactoryProvider.UIControllerFactory.Returns(new ExportFactory<IUIController>(
83+
() => Tuple.Create<IUIController, Action>(uiController, () => { })));
84+
85+
viewModel = new GitHubPaneViewModel(
86+
Substitute.For<ISimpleApiClientFactory>(),
87+
teamExplorerServiceHolder,
88+
connectionManager,
89+
repositoryHosts,
90+
Substitute.For<INotificationDispatcher>());
91+
92+
viewModel.ActiveRepo = activeRepo;
93+
}
94+
95+
[Fact]
96+
public void ListRefreshKeepsListVisible_DoesNotSwitchToPRCreation()
97+
{
98+
RunSteps(new[]
99+
{
100+
new NavStep(LoadDirection.Forward, UIViewType.PRList),
101+
new NavStep(LoadDirection.Forward, UIViewType.PRCreation),
102+
new NavStep(LoadDirection.Back, UIViewType.PRList),
103+
new NavStep(LoadDirection.Forward, UIViewType.PRCreation),
104+
new NavStep(LoadDirection.Forward, UIViewType.PRList),
105+
});
106+
107+
menuCommandService.ExecuteCommand(PkgCmdIDList.refreshCommand);
108+
109+
Assert.Equal(UIViewType.PRList, lastUiControllerJump);
110+
}
111+
112+
private void RunSteps(IEnumerable<NavStep> steps)
113+
{
114+
var observableSteps = steps
115+
.Select(_ => new LoadData
116+
{
117+
Direction = _.Direction,
118+
Data = new ViewWithData() { ViewType = _.ViewType },
119+
View = Substitute.For<IView>()
120+
})
121+
.ToObservable();
122+
123+
uiController.SelectFlow(UIControllerFlow.PullRequests).Returns(observableSteps);
124+
125+
viewModel.Initialize(serviceProvider);
126+
}
127+
128+
private class NavStep
129+
{
130+
public NavStep(LoadDirection direction, UIViewType viewtype)
131+
{
132+
Direction = direction;
133+
ViewType = viewtype;
134+
}
135+
136+
public LoadDirection Direction { get; private set; }
137+
public UIViewType ViewType { get; private set; }
138+
}
139+
}

src/UnitTests/TestDoubles/FakeMenuCommandService.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public void AddCommand(MenuCommand command)
1717
addedCommands.Add(command);
1818
}
1919

20+
public void ExecuteCommand(int commandId)
21+
{
22+
var command = addedCommands.Find(_ => _.CommandID.ID == commandId);
23+
if (command != null)
24+
{
25+
command.Invoke();
26+
}
27+
}
28+
2029
public void AddVerb(DesignerVerb verb)
2130
{
2231
throw new NotImplementedException();

src/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
<Compile Include="GitHub.VisualStudio\Services\ConnectionManagerTests.cs" />
189189
<Compile Include="GitHub.VisualStudio\Services\RepositoryPublishServiceTests.cs" />
190190
<Compile Include="GitHub.VisualStudio\TeamExplorer\Home\GraphsNavigationItemTests.cs" />
191+
<Compile Include="GitHub.VisualStudio\UI\Views\GitHubPaneViewModelTests.cs" />
191192
<Compile Include="GitHubPackageTests.cs" />
192193
<Compile Include="Helpers\CommandTestHelpers.cs" />
193194
<Compile Include="Helpers\LazySubstitute.cs" />

0 commit comments

Comments
 (0)