- Reusable create/edit dialog for person management - 4 bUnit tests: validation, create, edit, server error
101 lines
3.7 KiB
C#
101 lines
3.7 KiB
C#
using Bunit;
|
|
using Heimdall.Web.Components.Shared;
|
|
using Heimdall.Web.Services;
|
|
using Heimdall.Web.Tests.Components.Pages;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using MudBlazor;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Web.Tests.Components.FormDialogs;
|
|
|
|
public class PersonFormDialogTests : PageTestBase
|
|
{
|
|
private static readonly Guid PersonId = Guid.NewGuid();
|
|
|
|
private static PersonClientItem Existing() => new()
|
|
{
|
|
Id = PersonId,
|
|
DisplayName = "Alice",
|
|
Category = "family",
|
|
IsWatchlisted = false,
|
|
FaceCount = 0,
|
|
CreatedAt = DateTime.UtcNow
|
|
};
|
|
|
|
[Fact]
|
|
public void PersonFormDialog_Creation_SansNom_Erreur()
|
|
{
|
|
LoginAs();
|
|
var cut = RenderPage<PersonFormDialog>(p => p.Add(x => x.Visible, true));
|
|
|
|
cut.FindAll("div.mud-dialog-actions button").Single(b => b.TextContent.Trim() == "Create").Click();
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Name is required"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PersonFormDialog_Creation_Reussi_AppellePost()
|
|
{
|
|
Handler.On(HttpMethod.Post, "api/persons",
|
|
() => RoutedHandler.JsonResponse(System.Net.HttpStatusCode.OK,
|
|
new PersonClientItem { Id = PersonId, DisplayName = "New Person" }));
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<PersonFormDialog>(p => p.Add(x => x.Visible, true));
|
|
|
|
cut.FindAll("input")[0].Input("New Person");
|
|
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button").Single(b => b.TextContent.Trim() == "Create").Click());
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
Handler.Requests.Count(r => r.Method == HttpMethod.Post
|
|
&& r.RequestUri!.PathAndQuery.Contains("api/persons")).ShouldBe(1);
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "").ShouldContain("Person created");
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PersonFormDialog_Modification_AppellePut()
|
|
{
|
|
Handler.On(HttpMethod.Put, "api/persons/", System.Net.HttpStatusCode.OK);
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<PersonFormDialog>(p =>
|
|
{
|
|
p.Add(x => x.Visible, true);
|
|
p.Add(x => x.Existing, Existing());
|
|
});
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Edit Person"));
|
|
|
|
cut.FindAll("input")[0].Input("Alice Updated");
|
|
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button")
|
|
.Single(b => b.TextContent.Trim() == "Save").Click());
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
Handler.Requests.Count(r => r.Method == HttpMethod.Put
|
|
&& r.RequestUri!.PathAndQuery.Contains("api/persons")).ShouldBe(1);
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("updated", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PersonFormDialog_Creation_ErreurServeur_AfficheMessage()
|
|
{
|
|
Handler.On(HttpMethod.Post, "api/persons",
|
|
() => RoutedHandler.JsonResponse(System.Net.HttpStatusCode.BadRequest,
|
|
new { error = "Display name already exists" }));
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<PersonFormDialog>(p => p.Add(x => x.Visible, true));
|
|
|
|
cut.FindAll("input")[0].Input("New Person");
|
|
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button").Single(b => b.TextContent.Trim() == "Create").Click());
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Display name already exists"));
|
|
}
|
|
} |