-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathNavigationLifecycleTests.cs
More file actions
447 lines (353 loc) · 15.6 KB
/
NavigationLifecycleTests.cs
File metadata and controls
447 lines (353 loc) · 15.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using Microsoft.UI.Reactor.Core;
using Microsoft.UI.Reactor.Navigation;
using Xunit;
namespace Microsoft.UI.Reactor.Tests;
/// <summary>
/// Phase 3 tests: Navigation lifecycle hooks.
///
/// Unit tests covering:
/// - UseNavigationLifecycle hook state management
/// - onNavigatedTo / onNavigatingFrom / onNavigatedFrom callback invocation
/// - onNavigatingFrom cancellation
/// - Lifecycle context values (Route, PreviousRoute, TargetRoute, Mode)
/// - Multiple components with lifecycle hooks
/// - Lifecycle hooks coexisting with other hooks
/// - Guard + lifecycle guard ordering
/// </summary>
public class NavigationLifecycleTests
{
private abstract record Route;
private sealed record Home : Route;
private sealed record Detail(int Id) : Route;
private sealed record Settings : Route;
private sealed record Profile(string Name) : Route;
// ════════════════════════════════════════════════════════════════
// NavigatedToContext / NavigatedFromContext construction
// ════════════════════════════════════════════════════════════════
[Fact]
public void NavigatedToContext_Exposes_All_Properties()
{
var ctx = new NavigatedToContext(new Detail(1), new Home(), NavigationMode.Push);
Assert.Equal(new Detail(1), ctx.Route);
Assert.Equal(new Home(), ctx.PreviousRoute);
Assert.Equal(NavigationMode.Push, ctx.Mode);
}
[Fact]
public void NavigatedToContext_Allows_Null_PreviousRoute()
{
var ctx = new NavigatedToContext(new Home(), null, NavigationMode.Reset);
Assert.Equal(new Home(), ctx.Route);
Assert.Null(ctx.PreviousRoute);
}
[Fact]
public void NavigatedFromContext_Exposes_All_Properties()
{
var ctx = new NavigatedFromContext(new Home(), new Detail(1), NavigationMode.Push);
Assert.Equal(new Home(), ctx.Route);
Assert.Equal(new Detail(1), ctx.TargetRoute);
Assert.Equal(NavigationMode.Push, ctx.Mode);
}
// ════════════════════════════════════════════════════════════════
// UseNavigationLifecycle hook state
// ════════════════════════════════════════════════════════════════
[Fact]
public void UseNavigationLifecycle_Registers_Hook_State()
{
var ctx = new RenderContext();
ctx.BeginRender(() => { });
ctx.UseNavigationLifecycle(
onNavigatedTo: _ => { },
onNavigatingFrom: _ => { },
onNavigatedFrom: _ => { });
var hook = ctx.GetNavigationLifecycleHook();
Assert.NotNull(hook);
Assert.NotNull(hook.OnNavigatedTo);
Assert.NotNull(hook.OnNavigatingFrom);
Assert.NotNull(hook.OnNavigatedFrom);
}
[Fact]
public void UseNavigationLifecycle_With_Null_Callbacks()
{
var ctx = new RenderContext();
ctx.BeginRender(() => { });
ctx.UseNavigationLifecycle(); // All null
var hook = ctx.GetNavigationLifecycleHook();
Assert.NotNull(hook);
Assert.Null(hook.OnNavigatedTo);
Assert.Null(hook.OnNavigatingFrom);
Assert.Null(hook.OnNavigatedFrom);
}
[Fact]
public void UseNavigationLifecycle_Updates_Callbacks_On_Rerender()
{
var ctx = new RenderContext();
int callCount1 = 0, callCount2 = 0;
// First render
ctx.BeginRender(() => { });
ctx.UseNavigationLifecycle(onNavigatedTo: _ => callCount1++);
// Second render with different callback
ctx.BeginRender(() => { });
ctx.UseNavigationLifecycle(onNavigatedTo: _ => callCount2++);
var hook = ctx.GetNavigationLifecycleHook();
Assert.NotNull(hook);
// Should use latest callback
hook.OnNavigatedTo!(new NavigatedToContext(new Home(), null, NavigationMode.Push));
Assert.Equal(0, callCount1);
Assert.Equal(1, callCount2);
}
[Fact]
public void GetNavigationLifecycleHook_Returns_Null_When_No_Hook()
{
var ctx = new RenderContext();
ctx.BeginRender(() => { });
ctx.UseState(0); // Register a different hook type
Assert.Null(ctx.GetNavigationLifecycleHook());
}
[Fact]
public void UseNavigationLifecycle_Coexists_With_UseState()
{
var ctx = new RenderContext();
ctx.BeginRender(() => { });
var (count, setCount) = ctx.UseState(0);
ctx.UseNavigationLifecycle(onNavigatedTo: _ => { });
var (name, setName) = ctx.UseState("test");
Assert.Equal(0, count);
Assert.Equal("test", name);
Assert.NotNull(ctx.GetNavigationLifecycleHook());
}
[Fact]
public void UseNavigationLifecycle_Throws_If_Hook_Order_Changes()
{
var ctx = new RenderContext();
// First render: UseState then UseNavigationLifecycle
ctx.BeginRender(() => { });
ctx.UseState(0);
ctx.UseNavigationLifecycle();
// Second render: try UseNavigationLifecycle at UseState's index
ctx.BeginRender(() => { });
Assert.Throws<HookOrderException>(() => ctx.UseNavigationLifecycle());
}
// ════════════════════════════════════════════════════════════════
// LifecycleGuard on NavigationStack
// ════════════════════════════════════════════════════════════════
[Fact]
public void LifecycleGuard_Fires_Before_Programmatic_Guard()
{
var stack = new NavigationStack<Route>(new Home());
var order = new List<string>();
stack.LifecycleGuard = ctx => order.Add("lifecycle");
stack.Guard = ctx => { order.Add("guard"); };
stack.Push(new Detail(1));
Assert.Equal(new[] { "lifecycle", "guard" }, order);
}
[Fact]
public void LifecycleGuard_Cancellation_Prevents_Programmatic_Guard()
{
var stack = new NavigationStack<Route>(new Home());
bool guardCalled = false;
stack.LifecycleGuard = ctx => ctx.Cancel();
stack.Guard = ctx => { guardCalled = true; };
var result = stack.Push(new Detail(1));
Assert.False(result);
Assert.False(guardCalled);
Assert.IsType<Home>(stack.Current); // Stack unchanged
}
[Fact]
public void LifecycleGuard_Cancellation_Prevents_Push()
{
var stack = new NavigationStack<Route>(new Home());
stack.LifecycleGuard = ctx => ctx.Cancel();
Assert.False(stack.Push(new Detail(1)));
Assert.IsType<Home>(stack.Current);
Assert.False(stack.CanGoBack);
}
[Fact]
public void LifecycleGuard_Cancellation_Prevents_Pop()
{
var stack = new NavigationStack<Route>(new Home());
stack.Push(new Detail(1));
stack.LifecycleGuard = ctx => ctx.Cancel();
Assert.False(stack.Pop());
Assert.IsType<Detail>(stack.Current);
}
[Fact]
public void LifecycleGuard_Cancellation_Prevents_Replace()
{
var stack = new NavigationStack<Route>(new Home());
stack.LifecycleGuard = ctx => ctx.Cancel();
Assert.False(stack.Replace(new Settings()));
Assert.IsType<Home>(stack.Current);
}
[Fact]
public void LifecycleGuard_Cancellation_Prevents_Reset()
{
var stack = new NavigationStack<Route>(new Home());
stack.Push(new Detail(1));
stack.LifecycleGuard = ctx => ctx.Cancel();
Assert.False(stack.Reset(new Settings()));
Assert.IsType<Detail>(stack.Current);
Assert.True(stack.CanGoBack);
}
[Fact]
public void LifecycleGuard_Cancellation_Prevents_Forward()
{
var stack = new NavigationStack<Route>(new Home());
stack.Push(new Detail(1));
stack.Pop();
// Now at Home with Detail in forward stack
stack.LifecycleGuard = ctx => ctx.Cancel();
Assert.False(stack.Forward());
Assert.IsType<Home>(stack.Current);
Assert.True(stack.CanGoForward);
}
[Fact]
public void LifecycleGuard_Receives_Correct_Context_On_Push()
{
var stack = new NavigationStack<Route>(new Home());
NavigatingFromContext? captured = null;
stack.LifecycleGuard = ctx => captured = ctx;
stack.Push(new Detail(42));
Assert.NotNull(captured);
Assert.Equal(new Home(), captured!.Route);
Assert.Equal(new Detail(42), captured.TargetRoute);
Assert.Equal(NavigationMode.Push, captured.Mode);
}
[Fact]
public void LifecycleGuard_Receives_Correct_Context_On_Pop()
{
var stack = new NavigationStack<Route>(new Home());
stack.Push(new Detail(1));
NavigatingFromContext? captured = null;
stack.LifecycleGuard = ctx => captured = ctx;
stack.Pop();
Assert.NotNull(captured);
Assert.Equal(new Detail(1), captured!.Route);
Assert.Equal(new Home(), captured.TargetRoute);
Assert.Equal(NavigationMode.Pop, captured.Mode);
}
[Fact]
public void LifecycleGuard_Not_Called_When_Null()
{
var stack = new NavigationStack<Route>(new Home());
// No lifecycle guard set — just verify no exception
Assert.True(stack.Push(new Detail(1)));
}
// ════════════════════════════════════════════════════════════════
// INavigationHandle.LifecycleGuard
// ════════════════════════════════════════════════════════════════
[Fact]
public void INavigationHandle_LifecycleGuard_Delegates_To_Stack()
{
var stack = new NavigationStack<Route>(new Home());
var handle = new NavigationHandle<Route>(stack);
var iHandle = (INavigationHandle)handle;
bool called = false;
iHandle.LifecycleGuard = ctx => called = true;
handle.Navigate(new Detail(1));
Assert.True(called);
}
[Fact]
public void INavigationHandle_LifecycleGuard_Cancellation_Prevents_Navigate()
{
var stack = new NavigationStack<Route>(new Home());
var handle = new NavigationHandle<Route>(stack);
var iHandle = (INavigationHandle)handle;
iHandle.LifecycleGuard = ctx => ctx.Cancel();
bool result = handle.Navigate(new Detail(1));
Assert.False(result);
Assert.IsType<Home>(handle.CurrentRoute);
}
[Fact]
public void INavigationHandle_LifecycleGuard_Can_Be_Cleared()
{
var stack = new NavigationStack<Route>(new Home());
var handle = new NavigationHandle<Route>(stack);
var iHandle = (INavigationHandle)handle;
iHandle.LifecycleGuard = ctx => ctx.Cancel();
iHandle.LifecycleGuard = null;
// Should now succeed since guard was cleared
Assert.True(handle.Navigate(new Detail(1)));
Assert.IsType<Detail>(handle.CurrentRoute);
}
// ════════════════════════════════════════════════════════════════
// NavigationHandle Navigated event still fires correctly with lifecycle guard
// ════════════════════════════════════════════════════════════════
[Fact]
public void Navigated_Event_Fires_After_Successful_Navigation_With_LifecycleGuard()
{
var stack = new NavigationStack<Route>(new Home());
var handle = new NavigationHandle<Route>(stack);
var iHandle = (INavigationHandle)handle;
// Set a non-cancelling lifecycle guard
iHandle.LifecycleGuard = ctx => { /* allow */ };
NavigationEventArgs<Route>? eventArgs = null;
handle.Navigated += args => eventArgs = args;
handle.Navigate(new Detail(1));
Assert.NotNull(eventArgs);
Assert.Equal(new Detail(1), eventArgs!.Route);
Assert.Equal(new Home(), eventArgs.PreviousRoute);
}
[Fact]
public void Navigated_Event_Does_Not_Fire_When_LifecycleGuard_Cancels()
{
var stack = new NavigationStack<Route>(new Home());
var handle = new NavigationHandle<Route>(stack);
var iHandle = (INavigationHandle)handle;
iHandle.LifecycleGuard = ctx => ctx.Cancel();
bool eventFired = false;
handle.Navigated += _ => eventFired = true;
handle.Navigate(new Detail(1));
Assert.False(eventFired);
}
// ════════════════════════════════════════════════════════════════
// Full lifecycle sequence (unit-level, no reconciler)
// ════════════════════════════════════════════════════════════════
[Fact]
public void Lifecycle_Callbacks_Capture_Current_State_Via_Closures()
{
// Verifies that lifecycle callbacks can close over component state
var ctx = new RenderContext();
string capturedValue = "";
ctx.BeginRender(() => { });
var (value, _) = ctx.UseState("hello");
ctx.UseNavigationLifecycle(
onNavigatedTo: navCtx => capturedValue = value);
var hook = ctx.GetNavigationLifecycleHook();
hook!.OnNavigatedTo!(new NavigatedToContext(new Home(), null, NavigationMode.Push));
Assert.Equal("hello", capturedValue);
}
[Fact]
public void Multiple_Navigations_Each_Get_Correct_Context()
{
var stack = new NavigationStack<Route>(new Home());
var contexts = new List<(object Route, object Target, NavigationMode Mode)>();
stack.LifecycleGuard = ctx =>
contexts.Add((ctx.Route, ctx.TargetRoute, ctx.Mode));
stack.Push(new Detail(1));
stack.Push(new Detail(2));
stack.Pop();
stack.Replace(new Settings());
Assert.Equal(4, contexts.Count);
Assert.Equal((new Home(), new Detail(1), NavigationMode.Push), contexts[0]);
Assert.Equal((new Detail(1), new Detail(2), NavigationMode.Push), contexts[1]);
Assert.Equal((new Detail(2), new Detail(1), NavigationMode.Pop), contexts[2]);
Assert.Equal((new Detail(1), new Settings(), NavigationMode.Replace), contexts[3]);
}
[Fact]
public void Conditional_Cancellation_Based_On_Target_Route()
{
var stack = new NavigationStack<Route>(new Home());
// Only block navigation to Settings
stack.LifecycleGuard = ctx =>
{
if (ctx.TargetRoute is Settings)
ctx.Cancel();
};
Assert.True(stack.Push(new Detail(1)));
Assert.IsType<Detail>(stack.Current);
Assert.False(stack.Push(new Settings()));
Assert.IsType<Detail>(stack.Current); // Blocked
Assert.True(stack.Replace(new Profile("Alice")));
Assert.IsType<Profile>(stack.Current);
}
}