Skip to content

Commit 7162fba

Browse files
committed
Add more tests
1 parent 39914ac commit 7162fba

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/Components/Components/test/Routing/RouterTest.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
using Microsoft.Extensions.DependencyInjection;
1111
using Microsoft.Extensions.Logging;
1212
using Microsoft.Extensions.Logging.Abstractions;
13-
using Moq;
1413
using Xunit;
15-
using Microsoft.AspNetCore.Components;
1614

1715
namespace Microsoft.AspNetCore.Components.Test.Routing
1816
{
@@ -70,6 +68,12 @@ public async Task CanceledFailedOnNavigateAsyncDoesNothing()
7068
throw new Exception("This is an uncaught exception.");
7169
}
7270
};
71+
var refreshCalled = 0;
72+
_renderer.OnUpdateDisplay = (renderBatch) =>
73+
{
74+
refreshCalled += 1;
75+
return;
76+
};
7377
_router.OnNavigateAsync = new EventCallback<NavigationContext>(null, OnNavigateAsync);
7478

7579
// Act
@@ -82,6 +86,7 @@ public async Task CanceledFailedOnNavigateAsyncDoesNothing()
8286
// Assert that we render the second route component and don't throw an exception
8387
Assert.Empty(_renderer.HandledExceptions);
8488
Assert.Equal(2, onNavigateInvoked);
89+
Assert.Equal(2, refreshCalled);
8590
}
8691

8792
[Fact]

src/Components/test/E2ETest/Tests/RoutingTest.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,14 +570,24 @@ public void OnNavigate_CanRenderUIForExceptions()
570570
{
571571
var app = Browser.MountTestComponent<TestRouterWithOnNavigate>();
572572

573-
// Navigating from one page to another should
574-
// cancel the previous OnNavigate Task
575573
SetUrlViaPushState("/Other");
576574

577575
var errorUiElem = Browser.Exists(By.Id("blazor-error-ui"), TimeSpan.FromSeconds(10));
578576
Assert.NotNull(errorUiElem);
579577
}
580578

579+
[Fact]
580+
public void OnNavigate_CanRenderUIForSyncExceptions()
581+
{
582+
var app = Browser.MountTestComponent<TestRouterWithOnNavigate>();
583+
584+
// Should capture exception from synchronously thrown
585+
SetUrlViaPushState("/WithLazyAssembly");
586+
587+
var errorUiElem = Browser.Exists(By.Id("blazor-error-ui"), TimeSpan.FromSeconds(10));
588+
Assert.NotNull(errorUiElem);
589+
}
590+
581591
[Fact]
582592
public void OnNavigate_DoesNotRenderWhileOnNavigateExecuting()
583593
{

src/Components/test/testassets/BasicTestApp/RouterTest/TestRouterWithOnNavigate.razor

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@
2626
{ "LongPage1", new Func<NavigationContext, Task>(TestLoadingPageShows) },
2727
{ "LongPage2", new Func<NavigationContext, Task>(TestOnNavCancel) },
2828
{ "Other", new Func<NavigationContext, Task>(TestOnNavException) },
29-
{"WithParameters/name/Abc", new Func<NavigationContext, Task>(TestRefreshHandling)}
29+
{ "WithLazyAssembly", new Func<NavigationContext, Task>(TestOnNavException) },
30+
{ "WithParameters/name/Abc", new Func<NavigationContext, Task>(TestRefreshHandling) }
3031
};
3132

33+
protected override void OnAfterRender(bool firstRender)
34+
{
35+
Console.WriteLine("Render triggered...");
36+
}
37+
3238
private async Task OnNavigateAsync(NavigationContext args)
3339
{
3440
Console.WriteLine($"Running OnNavigate for {args.Path}...");
@@ -56,6 +62,11 @@
5662
throw new Exception("This is an uncaught exception.");
5763
}
5864

65+
public static Task TestOnNavSyncException(NavigationContext args)
66+
{
67+
throw new Exception("This is an uncaught exception.");
68+
}
69+
5970
public static async Task TestRefreshHandling(NavigationContext args)
6071
{
6172
await Task.Delay(Timeout.Infinite, args.CancellationToken);

0 commit comments

Comments
 (0)