Skip to content

Commit 26e1118

Browse files
committed
Client Blazor Tests 2
1 parent 678d949 commit 26e1118

9 files changed

Lines changed: 771 additions & 10 deletions

File tree

‎samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub.EFCore/Components/Layout/MainLayout.razor‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
@inherits UAuthHubLayoutComponentBase
1+
@inherits UAuthHubLayoutBase
22
@inject IUAuthClient UAuthClient
33
@inject ISnackbar Snackbar
44
@inject NavigationManager Nav
55

6-
@if (!IsHubAuthorized)
6+
@if (!IsHubActive)
77
{
88
<MudAppBar Class="uauth-blur" Color="Color.Transparent" Dense="true" Elevation="0">
99
<UAuthLogo />

‎samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub.EFCore/Components/Pages/Home.razor.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5757

5858
if (HubSessionId.TryParse(HubKey, out var hubSessionId))
5959
{
60-
await ReloadState();
60+
await ReloadStateAsync();
6161
}
6262

6363
await _loginForm.ReloadAsync();

‎samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub/Components/Layout/MainLayout.razor‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
@inherits UAuthHubLayoutComponentBase
1+
@inherits UAuthHubLayoutBase
22
@inject IUAuthClient UAuthClient
33
@inject ISnackbar Snackbar
44
@inject NavigationManager Nav
55

6-
@if (!IsHubAuthorized)
6+
@if (!IsHubActive)
77
{
88
<MudAppBar Class="uauth-blur" Color="Color.Transparent" Dense="true" Elevation="0">
99
<UAuthLogo />

‎samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub/Components/Pages/Home.razor.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5757

5858
if (HubSessionId.TryParse(HubKey, out var hubSessionId))
5959
{
60-
await ReloadState();
60+
await ReloadStateAsync();
6161
}
6262

6363
await _loginForm.ReloadAsync();

src/client/CodeBeam.UltimateAuth.Client.Blazor/Components/Base/UAuthHubLayoutComponentBase.cs renamed to src/client/CodeBeam.UltimateAuth.Client.Blazor/Components/Base/UAuthHubLayoutBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
namespace CodeBeam.UltimateAuth.Client.Blazor;
77

8-
public abstract class UAuthHubLayoutComponentBase : LayoutComponentBase
8+
public abstract class UAuthHubLayoutBase : LayoutComponentBase
99
{
1010
[Inject] protected NavigationManager Navigation { get; set; } = default!;
1111
[Inject] protected IHubFlowReader HubFlowReader { get; set; } = default!;
1212

1313
protected HubFlowState? HubState { get; private set; }
1414

1515
protected bool HasHub => HubState?.Exists == true;
16-
protected bool IsHubAuthorized => HasHub && HubState?.IsActive == true;
16+
protected bool IsHubActive => HasHub && HubState?.IsActive == true;
1717
protected bool IsExpired => HubState?.IsExpired == true;
1818
protected HubErrorCode? Error => HubState?.Error;
1919

‎src/client/CodeBeam.UltimateAuth.Client.Blazor/Components/Base/UAuthHubPageBase.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public abstract class UAuthHubPageBase : UAuthComponentBase
2020
protected override async Task OnParametersSetAsync()
2121
{
2222
await base.OnParametersSetAsync();
23-
await ReloadState();
23+
await ReloadStateAsync();
2424
}
2525

26-
public async Task ReloadState()
26+
public async Task ReloadStateAsync()
2727
{
2828
if (string.IsNullOrWhiteSpace(HubKey))
2929
{
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Bunit;
2+
using CodeBeam.UltimateAuth.Client.Blazor;
3+
using CodeBeam.UltimateAuth.Core.Abstractions;
4+
using CodeBeam.UltimateAuth.Core.Domain;
5+
using FluentAssertions;
6+
using Microsoft.AspNetCore.Components;
7+
using Microsoft.AspNetCore.Components.Rendering;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Moq;
10+
11+
namespace CodeBeam.UltimateAuth.Tests.Unit.Client.Blazor;
12+
13+
public sealed class UAuthHubLayoutBaseTests : BunitContext
14+
{
15+
private readonly Mock<IHubFlowReader> _reader = new();
16+
17+
public UAuthHubLayoutBaseTests()
18+
{
19+
Services.AddSingleton(_reader.Object);
20+
}
21+
22+
[Fact]
23+
public void Render_WithoutHubQuery_DoesNotReadHubState()
24+
{
25+
var cut = Render<TestHubLayout>();
26+
27+
cut.Instance.CurrentHubState.Should().BeNull();
28+
cut.Instance.HasCurrentHub.Should().BeFalse();
29+
cut.Instance.HubActive.Should().BeFalse();
30+
cut.Instance.HubExpired.Should().BeFalse();
31+
cut.Instance.HubError.Should().BeNull();
32+
33+
_reader.Verify(
34+
x => x.GetStateAsync(
35+
It.IsAny<HubSessionId>(),
36+
It.IsAny<CancellationToken>()),
37+
Times.Never);
38+
}
39+
40+
[Fact]
41+
public void Render_WithInvalidHubQuery_DoesNotReadHubState()
42+
{
43+
Nav.NavigateTo("/login?uauth_hub=invalid");
44+
45+
var cut = Render<TestHubLayout>();
46+
47+
cut.Instance.CurrentHubState.Should().BeNull();
48+
49+
_reader.Verify(
50+
x => x.GetStateAsync(
51+
It.IsAny<HubSessionId>(),
52+
It.IsAny<CancellationToken>()),
53+
Times.Never);
54+
}
55+
56+
[Fact]
57+
public void ResolveHubKey_UsesUAuthHubQueryParameter()
58+
{
59+
Nav.NavigateTo("/login?uauth_hub=my-hub-value&hub=legacy-value");
60+
61+
var cut = Render<TestHubLayout>();
62+
63+
cut.Instance.ResolvedHubKey
64+
.Should()
65+
.Be("my-hub-value");
66+
}
67+
68+
private NavigationManager Nav =>
69+
Services.GetRequiredService<NavigationManager>();
70+
71+
private sealed class TestHubLayout : UAuthHubLayoutBase
72+
{
73+
public HubFlowState? CurrentHubState => HubState;
74+
public bool HasCurrentHub => HasHub;
75+
public bool HubActive => IsHubActive;
76+
public bool HubExpired => IsExpired;
77+
public HubErrorCode? HubError => Error;
78+
79+
public string? ResolvedHubKey => ResolveHubKey();
80+
81+
protected override void BuildRenderTree(
82+
RenderTreeBuilder builder)
83+
{
84+
builder.AddContent(0, Body);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)