|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using System.Windows.Data; |
| 6 | +using GitHub.Models; |
| 7 | +using GitHub.Primitives; |
| 8 | +using GitHub.Services; |
| 9 | +using GitHub.ViewModels.Dialog.Clone; |
| 10 | +using NSubstitute; |
| 11 | +using NUnit.Framework; |
| 12 | + |
| 13 | +public class RepositorySelectViewModelTests |
| 14 | +{ |
| 15 | + public class TheFilterProperty |
| 16 | + { |
| 17 | + [TestCase("unknown", "owner", "name", "https://github.com/owner/name", 0)] |
| 18 | + [TestCase("", "owner", "name", "https://github.com/owner/name", 1)] |
| 19 | + [TestCase("owner", "owner", "name", "https://github.com/owner/name", 1)] |
| 20 | + [TestCase("name", "owner", "name", "https://github.com/owner/name", 1)] |
| 21 | + [TestCase("owner/name", "owner", "name", "https://github.com/owner/name", 1)] |
| 22 | + [TestCase("OWNER/NAME", "owner", "name", "https://github.com/owner/name", 1)] |
| 23 | + public async Task Filter(string filter, string owner, string name, string url, int expectCount) |
| 24 | + { |
| 25 | + var contributedToRepositories = new[] |
| 26 | + { |
| 27 | + new RepositoryListItemModel |
| 28 | + { |
| 29 | + Owner = owner, |
| 30 | + Name = name, |
| 31 | + Url = new Uri(url) |
| 32 | + } |
| 33 | + }; |
| 34 | + var hostAddress = HostAddress.GitHubDotComHostAddress; |
| 35 | + var connection = CreateConnection(hostAddress); |
| 36 | + var repositoryCloneService = CreateRepositoryCloneService(contributedToRepositories, hostAddress); |
| 37 | + var target = new RepositorySelectViewModel(repositoryCloneService); |
| 38 | + target.Filter = filter; |
| 39 | + target.Initialize(connection); |
| 40 | + |
| 41 | + await target.Activate(); |
| 42 | + |
| 43 | + var items = target.ItemsView.Groups |
| 44 | + .Cast<CollectionViewGroup>() |
| 45 | + .SelectMany(g => g.Items) |
| 46 | + .Cast<RepositoryItemViewModel>(); |
| 47 | + Assert.That(items.Count, Is.EqualTo(expectCount)); |
| 48 | + } |
| 49 | + |
| 50 | + static IConnection CreateConnection(HostAddress hostAddress) |
| 51 | + { |
| 52 | + var connection = Substitute.For<IConnection>(); |
| 53 | + connection.HostAddress.Returns(hostAddress); |
| 54 | + return connection; |
| 55 | + } |
| 56 | + |
| 57 | + static IRepositoryCloneService CreateRepositoryCloneService( |
| 58 | + IList<RepositoryListItemModel> contributedToRepositories, |
| 59 | + HostAddress hostAddress) |
| 60 | + { |
| 61 | + var viewRepositoriesModel = CreateViewerRepositoriesModel(contributedToRepositories: contributedToRepositories); |
| 62 | + var repositoryCloneService = Substitute.For<IRepositoryCloneService>(); |
| 63 | + repositoryCloneService.ReadViewerRepositories(hostAddress).Returns(viewRepositoriesModel); |
| 64 | + return repositoryCloneService; |
| 65 | + } |
| 66 | + |
| 67 | + private static ViewerRepositoriesModel CreateViewerRepositoriesModel( |
| 68 | + string owner = "owner", |
| 69 | + IList<RepositoryListItemModel> repositories = null, |
| 70 | + IList<RepositoryListItemModel> contributedToRepositories = null) |
| 71 | + { |
| 72 | + repositories = repositories ?? Array.Empty<RepositoryListItemModel>(); |
| 73 | + contributedToRepositories = contributedToRepositories ?? Array.Empty<RepositoryListItemModel>(); |
| 74 | + |
| 75 | + return new ViewerRepositoriesModel |
| 76 | + { |
| 77 | + Owner = owner, |
| 78 | + Repositories = CreateRepositoriesList(repositories), |
| 79 | + ContributedToRepositories = CreateRepositoriesList(contributedToRepositories), |
| 80 | + Organizations = CreateOrganizationsList() |
| 81 | + }; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + static IReadOnlyList<RepositoryListItemModel> CreateRepositoriesList(IList<RepositoryListItemModel> repositories) |
| 86 | + { |
| 87 | + return repositories.ToList().AsReadOnly(); |
| 88 | + } |
| 89 | + |
| 90 | + static IDictionary<string, IReadOnlyList<RepositoryListItemModel>> CreateOrganizationsList() |
| 91 | + { |
| 92 | + return new Dictionary<string, IReadOnlyList<RepositoryListItemModel>>(); |
| 93 | + } |
| 94 | +} |
0 commit comments