|
| 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 | + readonly IServiceProvider serviceProvider; |
| 23 | + readonly IUIController uiController; |
| 24 | + readonly FakeMenuCommandService menuCommandService; |
| 25 | + readonly GitHubPaneViewModel viewModel; |
| 26 | + UIViewType lastUiControllerJump; |
| 27 | + |
| 28 | + public GitHubPaneViewModelTests() |
| 29 | + { |
| 30 | + var repositoryHosts = Substitutes.RepositoryHosts; |
| 31 | + repositoryHosts.IsLoggedInToAnyHost.Returns(true); |
| 32 | + |
| 33 | + var teamExplorerServiceHolder = Substitute.For<ITeamExplorerServiceHolder>(); |
| 34 | + var activeRepo = Substitute.For<ILocalRepositoryModel>(); |
| 35 | + activeRepo.CloneUrl.Returns(new UriString("https://github.com/foo/foo")); |
| 36 | + teamExplorerServiceHolder |
| 37 | + .When(x => x.Subscribe(Arg.Any<object>(), Arg.Any<Action<ILocalRepositoryModel>>())) |
| 38 | + .Do(x => |
| 39 | + { |
| 40 | + var invokeAction = x.Arg<Action<ILocalRepositoryModel>>(); |
| 41 | + invokeAction(activeRepo); |
| 42 | + }); |
| 43 | + |
| 44 | + var connectionManager = Substitutes.ConnectionManager; |
| 45 | + var connection = Substitutes.Connection; |
| 46 | + var connectionHostAddress = HostAddress.Create(activeRepo.CloneUrl.ToString()); |
| 47 | + connection.HostAddress.Returns(connectionHostAddress); |
| 48 | + connectionManager.Connections.Returns(new ObservableCollection<IConnection>(new[] { |
| 49 | + connection |
| 50 | + })); |
| 51 | + connection.Login().Returns(Observable.Return(connection)); |
| 52 | + |
| 53 | + var host = Substitute.For<IRepositoryHost>(); |
| 54 | + host.IsLoggedIn.Returns(true); |
| 55 | + repositoryHosts.LookupHost(connectionHostAddress).Returns(host); |
| 56 | + |
| 57 | + serviceProvider = Substitutes.GetFullyMockedServiceProvider(); |
| 58 | + menuCommandService = new FakeMenuCommandService(); |
| 59 | + serviceProvider.GetService(typeof(IMenuCommandService)).Returns(menuCommandService); |
| 60 | + |
| 61 | + var uiProvider = serviceProvider as IUIProvider; |
| 62 | + uiProvider.TryGetService(typeof(IUIProvider)).Returns(serviceProvider); |
| 63 | + |
| 64 | + uiController = Substitute.For<IUIController>(); |
| 65 | + uiController.CurrentFlow.Returns(UIControllerFlow.PullRequests); |
| 66 | + uiController.SelectedFlow.Returns(UIControllerFlow.PullRequests); |
| 67 | + uiController |
| 68 | + .When(x => x.Jump(Arg.Any<ViewWithData>())) |
| 69 | + .Do(x => lastUiControllerJump = x.Arg<ViewWithData>().ViewType); |
| 70 | + |
| 71 | + var exportFactoryProvider = Substitutes.ExportFactoryProvider; |
| 72 | + uiProvider.TryGetService(typeof(IExportFactoryProvider)).Returns(exportFactoryProvider); |
| 73 | + exportFactoryProvider.UIControllerFactory.Returns(new ExportFactory<IUIController>( |
| 74 | + () => Tuple.Create<IUIController, Action>(uiController, () => { }))); |
| 75 | + |
| 76 | + viewModel = new GitHubPaneViewModel( |
| 77 | + Substitute.For<ISimpleApiClientFactory>(), |
| 78 | + teamExplorerServiceHolder, |
| 79 | + connectionManager, |
| 80 | + repositoryHosts, |
| 81 | + Substitute.For<INotificationDispatcher>()); |
| 82 | + |
| 83 | + viewModel.ActiveRepo = activeRepo; |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void ListRefreshKeepsListVisible_DoesNotSwitchToPRCreation() |
| 88 | + { |
| 89 | + RunSteps(new[] |
| 90 | + { |
| 91 | + new NavStep(LoadDirection.Forward, UIViewType.PRList), |
| 92 | + new NavStep(LoadDirection.Forward, UIViewType.PRCreation), |
| 93 | + new NavStep(LoadDirection.Back, UIViewType.PRList), |
| 94 | + new NavStep(LoadDirection.Forward, UIViewType.PRCreation), |
| 95 | + new NavStep(LoadDirection.Forward, UIViewType.PRList), |
| 96 | + }); |
| 97 | + |
| 98 | + menuCommandService.ExecuteCommand(PkgCmdIDList.refreshCommand); |
| 99 | + |
| 100 | + Assert.Equal(UIViewType.PRList, lastUiControllerJump); |
| 101 | + } |
| 102 | + |
| 103 | + private void RunSteps(IEnumerable<NavStep> steps) |
| 104 | + { |
| 105 | + var observableSteps = steps |
| 106 | + .Select(x => new LoadData |
| 107 | + { |
| 108 | + Direction = x.Direction, |
| 109 | + Data = new ViewWithData() { ViewType = x.ViewType }, |
| 110 | + View = Substitute.For<IView>() |
| 111 | + }) |
| 112 | + .ToObservable(); |
| 113 | + |
| 114 | + uiController.SelectFlow(UIControllerFlow.PullRequests).Returns(observableSteps); |
| 115 | + |
| 116 | + viewModel.Initialize(serviceProvider); |
| 117 | + } |
| 118 | + |
| 119 | + private class NavStep |
| 120 | + { |
| 121 | + public NavStep(LoadDirection direction, UIViewType viewtype) |
| 122 | + { |
| 123 | + Direction = direction; |
| 124 | + ViewType = viewtype; |
| 125 | + } |
| 126 | + |
| 127 | + public LoadDirection Direction { get; private set; } |
| 128 | + public UIViewType ViewType { get; private set; } |
| 129 | + } |
| 130 | +} |
0 commit comments