-
Notifications
You must be signed in to change notification settings - Fork 29
/
NavigationServiceTests.cs
134 lines (114 loc) · 5.14 KB
/
NavigationServiceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Moq;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Templates;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common;
using UmbracoNineDemoSite.Core.Features.Shared.Components.Navigation;
using UmbracoNineDemoSite.Core.Features.Shared.Constants;
namespace UmbracoNineDemoSite.Tests.Unit.Features.Shared.Components.Navigation
{
public class NavigationServiceTests
{
private delegate void ServiceTryGetUmbracoContext(out IUmbracoContext context);
private delegate void ServiceSetPublishedContentById(int id);
private Mock<IPublishedContentCache> contentCache;
private NavigationService navigationService;
private IEnumerable<IPublishedContent> topItems = new List<IPublishedContent>();
private List<IPublishedContent> allPages = new List<IPublishedContent>();
[SetUp]
public void SetUp()
{
var firstSubPage = new Mock<IPublishedContent>();
firstSubPage.Setup(s => s.Id).Returns(1011);
firstSubPage.Setup(s => s.Level).Returns(3);
firstSubPage.Setup(s => s.Name).Returns("First Subpage");
var secondSubPage = new Mock<IPublishedContent>();
secondSubPage.Setup(s => s.Id).Returns(1012);
secondSubPage.Setup(s => s.Level).Returns(3);
secondSubPage.Setup(s => s.Name).Returns("Second Subpage");
var aboutChildren = new List<IPublishedContent>()
{
firstSubPage.Object,
secondSubPage.Object,
};
var aboutPage = new Mock<IPublishedContent>();
aboutPage.Setup(s => s.Id).Returns(1001);
aboutPage.Setup(s => s.Level).Returns(2);
aboutPage.Setup(s => s.Name).Returns("About us");
aboutPage.Setup(s => s.Children).Returns(aboutChildren);
firstSubPage.Setup(s => s.Parent).Returns(aboutPage.Object);
secondSubPage.Setup(s => s.Parent).Returns(aboutPage.Object);
var productsPage = new Mock<IPublishedContent>();
productsPage.Setup(s => s.Id).Returns(1002);
productsPage.Setup(s => s.Level).Returns(2);
productsPage.Setup(s => s.Name).Returns("Products");
var roorChildren = new List<IPublishedContent>()
{
aboutPage.Object,
productsPage.Object,
};
var root = new Mock<IPublishedContent>();
root.Setup(s => s.Id).Returns(1000);
root.Setup(s => s.Level).Returns(1);
root.Setup(s => s.Name).Returns("Home");
root.Setup(s => s.Children).Returns(roorChildren);
var pages = new List<IPublishedContent>()
{
root.Object,
aboutPage.Object,
productsPage.Object,
};
topItems = pages;
allPages.AddRange(pages);
allPages.AddRange(aboutChildren);
var roots = new List<IPublishedContent>();
roots.Add(root.Object);
var contentType = Mock.Of<IPublishedContentType>();
contentCache = new Mock<IPublishedContentCache>();
contentCache.Setup(s => s.GetAtRoot(null))
.Returns(pages);
var umbracoContext = new Mock<IUmbracoContext>();
umbracoContext.Setup(s => s.Content)
.Returns(contentCache.Object);
IUmbracoContext ctx;
var umbracoContextAccessor = new Mock<IUmbracoContextAccessor>();
umbracoContextAccessor
.Setup(x => x.TryGetUmbracoContext(out ctx))
.Callback(new ServiceTryGetUmbracoContext((out IUmbracoContext uContext) =>
{
uContext = umbracoContext.Object;
}));
navigationService = new NavigationService(umbracoContextAccessor.Object);
}
[Test]
[TestCase(1011)]
[TestCase(1012)]
public void Given_CurrentId_When_GetSubNavigation_Then_ReturnSiblingsAsList(int currentId)
{
contentCache.Setup(s => s.GetById(currentId))
.Returns(allPages.FirstOrDefault(c => c.Id == currentId));
var result = navigationService.GetSubNavigation(currentId);
var currentContent = allPages.FirstOrDefault(c => c.Id == currentId);
var parent = currentContent.Parent;
var siblings = parent.Children;
Assert.AreEqual(siblings, result);
}
[Test]
public void When_GetTopNavigation_Then_ReturnRootChildrenAndSelf()
{
var result = navigationService.GetTopNavigation();
var root = topItems.FirstOrDefault(c => c.Level == 1);
var firstChild = root.Children.ElementAt(0);
var secondChild = root.Children.ElementAt(1);
Assert.True(result.Contains(root));
Assert.True(result.Contains(firstChild));
Assert.True(result.Contains(secondChild));
}
}
}