Skip to content

Instantly share code, notes, and snippets.

@christiansparre
Last active September 4, 2019 19:14
Show Gist options
  • Select an option

  • Save christiansparre/2b0e16048cffca538e07ceba2bbaf5e5 to your computer and use it in GitHub Desktop.

Select an option

Save christiansparre/2b0e16048cffca538e07ceba2bbaf5e5 to your computer and use it in GitHub Desktop.
Test of Blazor component with child component requiring cascading value
<aside class="menu">
<p class="menu-label">
Something
</p>
<ul class="menu-list">
<li><a>This</a></li>
<li><a>And that</a></li>
</ul>
<AuthorizeView Policy="RequireAdmin">
<Authorized>
<p id="admin-menu-label" class="menu-label">
Administration
</p>
<ul id="admin-menu-list" class="menu-list">
<li><a>Admin stuff</a></li>
<li><a>Assert your power!</a></li>
</ul>
</Authorized>
</AuthorizeView>
</aside>
using BlazorAuthenticationSample.Client.Features.App.Components;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Testing;
using Moq;
using System;
using Xunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Components.Authorization;
using System.Threading.Tasks;
using System.Security.Claims;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using Microsoft.AspNetCore.Components.Rendering;
namespace BlazorAuthenticationSample.Client.Tests
{
public class SidebarTests
{
private TestHost testHost = new TestHost();
public SidebarTests()
{
testHost.ConfigureServices(services =>
{
services.AddLogging();
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdmin", c => c.RequireRole("Admin"));
});
});
}
[Fact]
public void ShouldShowAdminMenuIfUserHasAdminRole()
{
var principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Role, "Admin") }));
Task<AuthenticationState> t = Task.FromResult(new AuthenticationState(principal));
var parameterView = ParameterView.FromDictionary(new Dictionary<string, object> { ["AuthenticationState"] = t });
var sidebar = testHost.AddComponent<SidebarWrapperTestComponent>(parameterView);
var menuLabel = sidebar.Find("#admin-menu-label");
Assert.NotNull(menuLabel);
}
[Fact]
public void ShouldNotShowAdminMenuIfUserDoesNotHaveAdminRole()
{
var principal = new ClaimsPrincipal(new ClaimsIdentity());
Task<AuthenticationState> t = Task.FromResult(new AuthenticationState(principal));
var parameterView = ParameterView.FromDictionary(new Dictionary<string, object> { ["AuthenticationState"] = t });
var sidebar = testHost.AddComponent<SidebarWrapperTestComponent>(parameterView);
var menuLabel = sidebar.Find("#admin-menu-label");
Assert.Null(menuLabel);
}
public class SidebarWrapperTestComponent : ComponentBase
{
[Parameter] public Task<AuthenticationState> AuthenticationState { get; set; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenComponent<CascadingValue<Task<AuthenticationState>>>(0);
builder.AddAttribute(1, nameof(CascadingValue<Task<AuthenticationState>>.Value), AuthenticationState);
builder.AddAttribute(2, "ChildContent", (RenderFragment)(builder =>
{
builder.OpenComponent<Sidebar>(0);
builder.CloseComponent();
}));
builder.CloseComponent();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment