-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainLayoutTests.cs
More file actions
304 lines (241 loc) · 12.6 KB
/
Copy pathMainLayoutTests.cs
File metadata and controls
304 lines (241 loc) · 12.6 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using Bunit;
using System.Globalization;
using ClearMeasure.Bootcamp.Core;
using ClearMeasure.Bootcamp.Core.Model;
using ClearMeasure.Bootcamp.Core.Services;
using ClearMeasure.Bootcamp.UI.Shared;
using ClearMeasure.Bootcamp.UI.Shared.Authentication;
using ClearMeasure.Bootcamp.UI.Shared.Components;
using ClearMeasure.Bootcamp.UI.Shared.Services;
using ClearMeasure.Bootcamp.UnitTests.UI.Shared.Pages;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Palermo.BlazorMvc;
using Shouldly;
using ClearMeasure.Bootcamp.UnitTests.UI.Client.Authentication;
namespace ClearMeasure.Bootcamp.UnitTests.UI.Shared;
[TestFixture]
public class MainLayoutTests
{
[Test]
public async Task ShouldRenderNavRailToggleWithExpandedStateByDefault()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
var toggle = layout.Find($"[data-testid='{nameof(MainLayout.Elements.NavRailToggle)}']");
toggle.GetAttribute("aria-expanded").ShouldBe("true");
toggle.GetAttribute("aria-controls").ShouldBe("app-navigation-rail");
toggle.GetAttribute("title")!.ShouldContain("Hide");
toggle.GetAttribute("aria-label")!.ShouldContain("Hide");
layout.Find("#app-navigation-rail").ClassList.ShouldContain("modern-sidebar");
layout.Find(".modern-app").ClassList.ShouldNotContain("rail-collapsed");
}
[Test]
public async Task ShouldToggleNavRailCollapseAndUpdateAriaOnWideLayout()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
await component.WaitForAssertionAsync(() =>
{
layout.Find($"[data-testid='{nameof(MainLayout.Elements.NavRailToggle)}']").ShouldNotBeNull();
});
await component.InvokeAsync(() => layout.Instance.OnViewportChanged(false));
var toggle = layout.Find($"[data-testid='{nameof(MainLayout.Elements.NavRailToggle)}']");
await toggle.ClickAsync(new());
toggle.GetAttribute("aria-expanded").ShouldBe("false");
toggle.GetAttribute("title")!.ShouldContain("Show");
layout.Find(".modern-app").ClassList.ShouldContain("rail-collapsed");
layout.Find("#app-navigation-rail").ClassList.ShouldContain("rail-hidden");
await toggle.ClickAsync(new());
toggle.GetAttribute("aria-expanded").ShouldBe("true");
toggle.GetAttribute("title")!.ShouldContain("Hide");
layout.Find(".modern-app").ClassList.ShouldNotContain("rail-collapsed");
layout.Find("#app-navigation-rail").ClassList.ShouldNotContain("rail-hidden");
}
[Test]
public async Task ShouldRenderCorrectIconForNavVisibility()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
await component.WaitForAssertionAsync(() =>
{
layout.Find($"[data-testid='{nameof(MainLayout.Elements.NavRailToggle)}']").ShouldNotBeNull();
});
await component.InvokeAsync(() => layout.Instance.OnViewportChanged(false));
var toggle = layout.Find($"[data-testid='{nameof(MainLayout.Elements.NavRailToggle)}']");
toggle.InnerHtml.ShouldContain("bi-chevron-double-left");
await toggle.ClickAsync(new());
toggle.InnerHtml.ShouldContain("bi-list");
}
[Test]
public async Task ShouldUseOverlayOpenClassOnNarrowViewportWhenNavVisible()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
await component.WaitForAssertionAsync(() =>
{
layout.Find($"[data-testid='{nameof(MainLayout.Elements.NavRailToggle)}']").ShouldNotBeNull();
});
await component.InvokeAsync(() => layout.Instance.OnViewportChanged(true));
var rail = layout.Find("#app-navigation-rail");
rail.ClassList.ShouldNotContain("open");
var toggle = layout.Find($"[data-testid='{nameof(MainLayout.Elements.NavRailToggle)}']");
await toggle.ClickAsync(new());
rail.ClassList.ShouldContain("open");
toggle.GetAttribute("aria-expanded").ShouldBe("true");
}
[Test]
public void ShouldUseDocumentedNavRailBreakpointMediaQuery()
{
MainLayout.NavRailBreakpointMediaQuery.ShouldBe("(max-width: 768px)");
}
[Test]
public async Task MainLayout_AfterFirstRender_ShouldCallThemeInitialize_WhenImplemented()
{
await using var ctx = CreateContext();
var themeModule = ctx.JSInterop.SetupModule(ThemePreferenceService.ThemeJsModulePath);
themeModule.Setup<string>("getTheme").SetResult("light");
themeModule.SetupVoid("syncDomFromTheme", _ => true).SetVoidResult();
ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
themeModule.VerifyInvoke("getTheme");
}
[Test]
public async Task ShouldRenderLoginLink_InHeader_WhenUserIsNotAuthenticated()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
var loginAnchor = layout.Find($"a[data-testid='{nameof(LoginLink.Elements.LoginLink)}']");
loginAnchor.GetAttribute("href").ShouldBe("/login");
loginAnchor.ClassList.ShouldContain("login-link-blink");
}
[Test]
public async Task ShouldRenderLoginLink_WithBlinkClass_WhenUserIsNotAuthenticated()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
var loginAnchor = layout.Find($"a[data-testid='{nameof(LoginLink.Elements.LoginLink)}']");
loginAnchor.ClassList.ShouldContain("login-link-blink");
loginAnchor.GetAttribute("href").ShouldBe("/login");
loginAnchor.GetAttribute("data-testid").ShouldBe(nameof(LoginLink.Elements.LoginLink));
loginAnchor.TextContent.Trim().ShouldBe("Login");
}
[Test]
public async Task ShouldNotRenderLoginLink_WhenUserIsAuthenticated()
{
await using var ctx = CreateContext(authenticateAsUser: "hsimpson");
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
layout.FindAll($"a[data-testid='{nameof(LoginLink.Elements.LoginLink)}']").Count.ShouldBe(0);
layout.Find($"[data-testid='{nameof(Logout.Elements.LogoutLink)}']").ShouldNotBeNull();
}
[Test]
public async Task ShouldPreserveLoginLinkInteraction_Unchanged()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
var loginAnchor = layout.Find($"a[data-testid='{nameof(LoginLink.Elements.LoginLink)}']");
loginAnchor.GetAttribute("href").ShouldBe("/login");
}
[Test]
public async Task ShouldRenderCopyrightFooter_WithCurrentYear_OrganizationAndLink_WhenNotAuthenticated()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
var footer = layout.Find($"[data-testid='{nameof(MainLayout.Elements.CopyrightFooter)}']");
footer.TagName.ShouldBe("FOOTER");
layout.FindAll("#app-navigation-rail footer").Count.ShouldBe(0);
var yearText = DateTime.UtcNow.Year.ToString(CultureInfo.InvariantCulture);
footer.TextContent.ShouldContain(yearText);
footer.TextContent.ShouldContain("ClearMeasure Labs");
var link = layout.Find($"[data-testid='{nameof(MainLayout.Elements.CopyrightFooter)}'] .site-footer-link");
link.GetAttribute("href")!.TrimEnd('/').ShouldBe("https://clearmeasure.com");
link.TextContent.Trim().ShouldBe("ClearMeasure Labs");
}
[Test]
public async Task ShouldRenderCopyrightFooter_WhenAuthenticated()
{
await using var ctx = CreateContext(authenticateAsUser: "hsimpson");
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
var footer = layout.Find($"[data-testid='{nameof(MainLayout.Elements.CopyrightFooter)}']");
var yearText = DateTime.UtcNow.Year.ToString(CultureInfo.InvariantCulture);
footer.TextContent.ShouldContain(yearText);
footer.TextContent.ShouldContain("ClearMeasure Labs");
layout.Find($"[data-testid='{nameof(MainLayout.Elements.CopyrightFooter)}'] .site-footer-link").GetAttribute("href")!.TrimEnd('/').ShouldBe("https://clearmeasure.com");
}
[Test]
public async Task ShouldRenderFooterNote_WithinSiteFooter()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
var note = layout.Find($"[data-testid='{nameof(MainLayout.Elements.FooterNote)}']");
note.TextContent.Trim().ShouldBe("Submit a new work order any time — requests are typically reviewed within one business day.");
var footer = layout.Find($"[data-testid='{nameof(MainLayout.Elements.CopyrightFooter)}']");
footer.QuerySelector($"[data-testid='{nameof(MainLayout.Elements.FooterNote)}']").ShouldNotBeNull();
}
[Test]
public async Task ShouldRenderCompanyLink_WithAccessibleAttributes_WhenExternalLinkUsesNewTab()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
var link = layout.Find($"[data-testid='{nameof(MainLayout.Elements.CopyrightFooter)}'] .site-footer-link");
link.GetAttribute("target").ShouldBe("_blank");
var rel = link.GetAttribute("rel");
rel.ShouldNotBeNull();
rel.ShouldContain("noopener");
rel.ShouldContain("noreferrer");
link.TextContent.Trim().ShouldNotContain("://");
}
[Test]
public async Task ShouldInvokeFocusOnNavRailToggleWhenClosingOverlayOnNarrowViewport()
{
await using var ctx = CreateContext();
var component = ctx.Render<CascadingAuthenticationState>(p => p.AddChildContent<MainLayout>());
var layout = component.FindComponent<MainLayout>();
await component.WaitForAssertionAsync(() =>
{
layout.Find($"[data-testid='{nameof(MainLayout.Elements.NavRailToggle)}']").ShouldNotBeNull();
});
await component.InvokeAsync(() => layout.Instance.OnViewportChanged(true));
var toggle = layout.Find($"[data-testid='{nameof(MainLayout.Elements.NavRailToggle)}']");
await toggle.ClickAsync(new());
await toggle.ClickAsync(new());
ctx.JSInterop.VerifyFocusAsyncInvoke();
}
private static BunitContext CreateContext(string? authenticateAsUser = null)
{
var ctx = new BunitContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
var bunitAuth = ctx.AddAuthorization();
if (authenticateAsUser != null)
{
bunitAuth.SetAuthorized(authenticateAsUser);
}
ctx.Services.AddSingleton<IUiBus>(new StubUiBus());
ctx.Services.AddSingleton<IBus>(new StubBus());
ctx.Services.AddSingleton<IUserSession>(new StubUserSession());
ctx.Services.AddSingleton(ctx.JSInterop.JSRuntime);
ctx.Services.AddSingleton<ThemePreferenceService>();
var customAuth = new CustomAuthenticationStateProvider(new StubUserSessionStore());
if (authenticateAsUser != null)
{
customAuth.Login(authenticateAsUser).GetAwaiter().GetResult();
}
ctx.Services.AddSingleton(customAuth);
return ctx;
}
private sealed class StubUserSession : IUserSession
{
public Task<Employee?> GetCurrentUserAsync() => Task.FromResult<Employee?>(null);
}
}