Improve admin field names and generation-safe dialog lifecycle
This commit is contained in:
@@ -27,6 +27,14 @@ namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class DeleteConfirmRenderTests : BunitContext
|
||||
{
|
||||
/// <summary>Configure only the shared confirmation's documented local DOM adapter.</summary>
|
||||
public DeleteConfirmRenderTests()
|
||||
{
|
||||
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
|
||||
JSInterop.SetupVoid("otDialog.update", _ => true).SetVoidResult();
|
||||
JSInterop.SetupVoid("otDialog.deactivate", _ => true).SetVoidResult();
|
||||
}
|
||||
|
||||
private const string ModalSelector = ".modal-dialog";
|
||||
|
||||
private IDbContextFactory<OtOpcUaConfigDbContext> NewDb(Action<OtOpcUaConfigDbContext> seed)
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using Bunit;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Forms;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Forms;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
|
||||
|
||||
/// <summary>Actual driver inputs retain visible names, parser feedback and per-instance relationships.</summary>
|
||||
public class FormAccessibilityTests : BunitContext
|
||||
{
|
||||
private IRenderedComponent<CascadingValue<EditContext>> RenderCalculation(int count = 1) =>
|
||||
Render<CascadingValue<EditContext>>(p => p.Add(x => x.Value, new EditContext(new object()))
|
||||
.Add(x => x.ChildContent, (RenderFragment)(builder =>
|
||||
{
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
builder.OpenComponent<CalculationDriverForm>(0);
|
||||
builder.SetKey(i);
|
||||
builder.CloseComponent();
|
||||
}
|
||||
})));
|
||||
|
||||
/// <summary>The modal's standalone form body remains valid without an external EditForm.</summary>
|
||||
[Fact]
|
||||
[Trait("Category", "Unit")]
|
||||
public void CalculationTimeout_StandaloneDoesNotRequireEditContext()
|
||||
{
|
||||
var cut = Render<CalculationDriverForm>();
|
||||
cut.Find("input[type=number]").Id.ShouldNotBeNullOrWhiteSpace();
|
||||
cut.Instance.GetConfigJson().ShouldBe("{}");
|
||||
}
|
||||
|
||||
/// <summary>The visible timeout label and hint name and describe the actual number input.</summary>
|
||||
[Fact]
|
||||
[Trait("Category", "Unit")]
|
||||
public void CalculationTimeout_HasAssociatedVisibleLabelAndHint()
|
||||
{
|
||||
var cut = RenderCalculation();
|
||||
var input = cut.Find("input[type=number]");
|
||||
input.Id.ShouldNotBeNullOrWhiteSpace();
|
||||
cut.Find($"label[for='{input.Id}']").TextContent.ShouldContain("Run timeout");
|
||||
var ids = input.GetAttribute("aria-describedby")!.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
ids.ShouldNotBeEmpty();
|
||||
string.Join(" ", ids.Select(id => cut.Find("#" + id).TextContent)).ShouldContain("2000 ms");
|
||||
}
|
||||
|
||||
/// <summary>Two independently rendered forms do not share label/hint targets.</summary>
|
||||
[Fact]
|
||||
[Trait("Category", "Unit")]
|
||||
public void CalculationTimeout_TwoFormsHaveDistinctAssociationTargets()
|
||||
{
|
||||
var cut = RenderCalculation(2);
|
||||
var forms = cut.FindComponents<CalculationDriverForm>();
|
||||
var inputs = forms.Select(form => form.Find("input[type=number]")).ToArray();
|
||||
inputs[0].Id.ShouldNotBeNullOrWhiteSpace();
|
||||
inputs[0].Id.ShouldNotBe(inputs[1].Id);
|
||||
inputs[0].GetAttribute("aria-describedby").ShouldNotBe(inputs[1].GetAttribute("aria-describedby"));
|
||||
}
|
||||
|
||||
/// <summary>Existing InputNumber parse feedback is visibly linked without changing config JSON.</summary>
|
||||
[Fact]
|
||||
[Trait("Category", "Unit")]
|
||||
public void CalculationTimeout_InvalidTextLinksExistingParserFeedback()
|
||||
{
|
||||
var cut = RenderCalculation();
|
||||
var form = cut.FindComponent<CalculationDriverForm>();
|
||||
var before = form.Instance.GetConfigJson();
|
||||
cut.Find("input[type=number]").Change("not-a-number");
|
||||
var input = cut.Find("input[type=number]");
|
||||
input.GetAttribute("aria-invalid").ShouldBe("true");
|
||||
var text = string.Join(" ", input.GetAttribute("aria-describedby")!.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(id => cut.Find("#" + id).TextContent));
|
||||
text.ShouldContain("number");
|
||||
form.Instance.GetConfigJson().ShouldBe(before);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
using Bunit;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Raw;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
|
||||
|
||||
/// <summary>Confirmation names and cancellation preserve the existing destructive callback contract.</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class ModalAccessibilityTests : BunitContext
|
||||
{
|
||||
/// <summary>Only the defined local focus-adapter calls are configured.</summary>
|
||||
public ModalAccessibilityTests()
|
||||
{
|
||||
JSInterop.SetupVoid("otDialog.update", _ => true).SetVoidResult();
|
||||
JSInterop.SetupVoid("otDialog.deactivate", _ => true).SetVoidResult();
|
||||
}
|
||||
|
||||
/// <summary>Cancel closes a named confirmation without invoking the destructive action.</summary>
|
||||
[Fact]
|
||||
public void ConfirmDialog_IsNamedAndCancelDoesNotInvokeDelete()
|
||||
{
|
||||
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
|
||||
var deletes = 0;
|
||||
var cut = Render<RawConfirmDialog>(p => p.Add(x => x.Visible, true)
|
||||
.Add(x => x.Title, "Delete driver press-01").Add(x => x.Message, "Delete press-01? This cannot be undone.")
|
||||
.Add(x => x.OnConfirm, () => deletes++));
|
||||
var dialog = cut.Find("[role=dialog]");
|
||||
dialog.GetAttribute("aria-modal").ShouldBe("true");
|
||||
var id = dialog.GetAttribute("aria-labelledby");
|
||||
id.ShouldNotBeNullOrWhiteSpace();
|
||||
cut.Find("#" + id).TextContent.ShouldContain("press-01");
|
||||
var description = dialog.GetAttribute("aria-describedby");
|
||||
description.ShouldNotBeNullOrWhiteSpace();
|
||||
cut.Find("#" + description).TextContent.ShouldContain("cannot be undone");
|
||||
cut.Find(".modal-footer .btn-outline-secondary").Click();
|
||||
deletes.ShouldBe(0);
|
||||
cut.FindAll("[role=dialog]").ShouldBeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>Closing while confirmation is in flight cannot imply the operation was cancelled.</summary>
|
||||
[Fact]
|
||||
public async Task ConfirmDialog_BusyClosePreservesInFlightOperation()
|
||||
{
|
||||
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
|
||||
var entered = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var pending = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var calls = 0;
|
||||
var cut = Render<RawConfirmDialog>(p => p.Add(x => x.Visible, true).Add(x => x.OnConfirm, async () =>
|
||||
{ calls++; entered.TrySetResult(true); await pending.Task; }));
|
||||
var confirming = cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
||||
await entered.Task;
|
||||
try
|
||||
{
|
||||
cut.Find(".btn-close").HasAttribute("disabled").ShouldBeTrue();
|
||||
cut.Find(".modal-footer .btn-outline-secondary").HasAttribute("disabled").ShouldBeTrue();
|
||||
cut.FindAll("[role=dialog]").Count.ShouldBe(1);
|
||||
calls.ShouldBe(1);
|
||||
await cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
||||
calls.ShouldBe(1); // A queued second activation cannot start another deletion.
|
||||
}
|
||||
finally { await cut.InvokeAsync(() => pending.TrySetResult(true)); await confirming; }
|
||||
cut.FindAll("[role=dialog]").ShouldBeEmpty();
|
||||
}
|
||||
/// <summary>A delayed old activation is cleaned before a later presentation is activated.</summary>
|
||||
[Fact]
|
||||
public void Frame_HideDuringDeferredActivation_ReopenActivatesFreshPresentation()
|
||||
{
|
||||
var activation = JSInterop.Setup<bool>("otDialog.activate", _ => true);
|
||||
var cut = Render<ModalFrame>(p => p.Add(x => x.Visible, true));
|
||||
cut.Render(p => p.Add(x => x.Visible, false));
|
||||
activation.SetResult(true);
|
||||
cut.WaitForAssertion(() => JSInterop.Invocations["otDialog.deactivate"].Count.ShouldBeGreaterThan(0));
|
||||
cut.Render(p => p.Add(x => x.Visible, true));
|
||||
cut.WaitForAssertion(() => JSInterop.Invocations["otDialog.activate"].Count.ShouldBe(2));
|
||||
}
|
||||
|
||||
/// <summary>Disposal waits for a pending activation and cleans any late native state.</summary>
|
||||
[Fact]
|
||||
public async Task Frame_DisposeDuringDeferredActivation_CleansLateActivation()
|
||||
{
|
||||
var activation = JSInterop.Setup<bool>("otDialog.activate", _ => true);
|
||||
var cut = Render<ModalFrame>(p => p.Add(x => x.Visible, true));
|
||||
var disposing = cut.Instance.DisposeAsync().AsTask();
|
||||
activation.SetResult(true);
|
||||
await disposing;
|
||||
cut.WaitForAssertion(() => JSInterop.Invocations["otDialog.deactivate"].Count.ShouldBeGreaterThan(0));
|
||||
}
|
||||
|
||||
/// <summary>A new target queued during activation opens only after the old native generation is cleaned.</summary>
|
||||
[Fact]
|
||||
public void Frame_TargetSwitchDuringDeferredActivation_IsSerialized()
|
||||
{
|
||||
var activation = JSInterop.Setup<bool>("otDialog.activate", _ => true);
|
||||
var cut = Render<ModalFrame>(p => p.Add(x => x.Visible, true).Add(x => x.Target, "A"));
|
||||
try
|
||||
{
|
||||
cut.Render(p => p.Add(x => x.Visible, false));
|
||||
cut.Render(p => p.Add(x => x.Visible, true).Add(x => x.Target, "B"));
|
||||
JSInterop.Invocations["otDialog.activate"].Count.ShouldBe(1);
|
||||
}
|
||||
finally { activation.SetResult(true); }
|
||||
cut.WaitForAssertion(() => JSInterop.Invocations["otDialog.activate"].Count.ShouldBe(2));
|
||||
var generations = JSInterop.Invocations["otDialog.activate"].Select(call => Convert.ToInt64(call.Arguments[2])).ToArray();
|
||||
generations[1].ShouldBeGreaterThan(generations[0]);
|
||||
JSInterop.Invocations["otDialog.deactivate"].Count.ShouldBeGreaterThan(0);
|
||||
}
|
||||
|
||||
/// <summary>A stale Escape cannot close a different reopened target.</summary>
|
||||
[Fact]
|
||||
public async Task Frame_StaleNativeCloseCannotCancelReopenedPresentation()
|
||||
{
|
||||
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
|
||||
var closes = 0;
|
||||
var cut = Render<ModalFrame>(p => p.Add(x => x.Visible, true).Add(x => x.Target, "A").Add(x => x.OnClose, () => closes++));
|
||||
var oldGeneration = Convert.ToInt64(JSInterop.Invocations["otDialog.activate"][0].Arguments[2]);
|
||||
cut.Render(p => p.Add(x => x.Visible, false));
|
||||
cut.Render(p => p.Add(x => x.Visible, true).Add(x => x.Target, "B"));
|
||||
var currentGeneration = Convert.ToInt64(JSInterop.Invocations["otDialog.activate"].Last().Arguments[2]);
|
||||
await cut.Instance.RequestCloseFromDom(oldGeneration);
|
||||
closes.ShouldBe(0);
|
||||
await cut.Instance.RequestCloseFromDom(currentGeneration);
|
||||
closes.ShouldBe(1);
|
||||
}
|
||||
|
||||
/// <summary>Busy-only updates preserve the presentation and never move focus back to its start.</summary>
|
||||
[Fact]
|
||||
public void Frame_BusyOnlyRenderUpdatesWithoutReactivation()
|
||||
{
|
||||
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
|
||||
var cut = Render<ModalFrame>(p => p.Add(x => x.Visible, true).Add(x => x.Target, "A"));
|
||||
cut.Render(p => p.Add(x => x.Busy, true));
|
||||
cut.Render(p => p.Add(x => x.Busy, false));
|
||||
JSInterop.Invocations["otDialog.activate"].Count.ShouldBe(1);
|
||||
JSInterop.Invocations["otDialog.update"].Count.ShouldBe(2);
|
||||
}
|
||||
|
||||
/// <summary>An old operation cannot close or clear the busy state of a replacement confirmation.</summary>
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task ConfirmDialog_ReplacedTargetIgnoresOldCompletionUi(bool sharedCaption)
|
||||
{
|
||||
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
|
||||
var enteredA = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var enteredB = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var doneA = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var doneB = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var callsA = 0; var callsB = 0;
|
||||
var cut = Render<RawConfirmDialog>(p => p.Add(x => x.Visible, true).Add(x => x.Title, sharedCaption ? "Delete selected row" : "Delete A").Add(x => x.Message, sharedCaption ? "Confirm selected target" : "Target A").Add(x => x.Target, "A")
|
||||
.Add(x => x.OnConfirm, async () => { callsA++; enteredA.TrySetResult(true); await doneA.Task; }));
|
||||
var first = cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
||||
await enteredA.Task;
|
||||
Task? second = null;
|
||||
try
|
||||
{
|
||||
cut.Render(p => p.Add(x => x.Title, sharedCaption ? "Delete selected row" : "Delete B").Add(x => x.Message, sharedCaption ? "Confirm selected target" : "Target B").Add(x => x.Target, "B")
|
||||
.Add(x => x.OnConfirm, async () => { callsB++; enteredB.TrySetResult(true); await doneB.Task; }));
|
||||
cut.Find(".modal-footer .btn-danger").HasAttribute("disabled").ShouldBeFalse();
|
||||
second = cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
||||
await enteredB.Task;
|
||||
await cut.InvokeAsync(() => doneA.TrySetResult(true));
|
||||
await first;
|
||||
cut.Find("[role=dialog]").TextContent.ShouldContain(sharedCaption ? "Delete selected row" : "Delete B");
|
||||
cut.Find(".modal-footer .btn-danger").HasAttribute("disabled").ShouldBeTrue();
|
||||
callsA.ShouldBe(1); callsB.ShouldBe(1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await cut.InvokeAsync(() => { doneA.TrySetResult(true); doneB.TrySetResult(true); });
|
||||
await first;
|
||||
if (second is not null) await second;
|
||||
}
|
||||
cut.FindAll("[role=dialog]").ShouldBeEmpty();
|
||||
}
|
||||
|
||||
/// <summary>A queued control from an earlier render never invokes a newer target's callback.</summary>
|
||||
[Fact]
|
||||
public async Task ConfirmDialog_StaleRenderedActionCannotInvokeReplacementCallback()
|
||||
{
|
||||
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
|
||||
var callsA = 0; var callsB = 0;
|
||||
var cut = Render<RawConfirmDialog>(p => p.Add(x => x.Visible, true).Add(x => x.Title, "Delete A").Add(x => x.OnConfirm, () => callsA++));
|
||||
var staleFooter = cut.FindComponent<ModalFrame>().Instance.Footer!;
|
||||
cut.Render(p => p.Add(x => x.Title, "Delete B").Add(x => x.OnConfirm, () => callsB++));
|
||||
var stale = Render<CascadingValue<object>>(p => p.Add(x => x.Value, new object()).Add(x => x.ChildContent, staleFooter));
|
||||
await stale.Find(".btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
||||
callsA.ShouldBe(0); callsB.ShouldBe(0);
|
||||
cut.Find("[role=dialog]").TextContent.ShouldContain("Delete B");
|
||||
}
|
||||
|
||||
/// <summary>A dispatched operation continues after disposal without invoking stale visibility UI.</summary>
|
||||
[Fact]
|
||||
public async Task ConfirmDialog_DisposalDoesNotCancelOperationOrCloseAnotherUi()
|
||||
{
|
||||
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
|
||||
var entered = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var done = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var calls = 0; var closes = 0;
|
||||
var cut = Render<RawConfirmDialog>(p => p.Add(x => x.Visible, true).Add(x => x.OnConfirm, async () =>
|
||||
{ calls++; entered.TrySetResult(true); await done.Task; }).Add(x => x.VisibleChanged, _ => closes++));
|
||||
var confirming = cut.Find(".modal-footer .btn-danger").TriggerEventAsync("onclick", new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
||||
await entered.Task;
|
||||
await DisposeComponentsAsync();
|
||||
done.TrySetResult(true);
|
||||
await confirming;
|
||||
calls.ShouldBe(1);
|
||||
closes.ShouldBe(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Bunit;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Uns;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
|
||||
|
||||
/// <summary>Tree controls expose the rendered node state and retain caller-owned lazy behavior.</summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class TreeAccessibilityTests : BunitContext
|
||||
{
|
||||
/// <summary>The expansion button follows the real node and raises the original callback.</summary>
|
||||
[Fact]
|
||||
public void UnsBranch_ExposesNodeNameExpansionAndOriginalCallback()
|
||||
{
|
||||
var node = new UnsNode { Kind = UnsNodeKind.Equipment, Key = "eq:1", DisplayName = "press-01", HasLazyChildren = true };
|
||||
UnsNode? toggled = null;
|
||||
var cut = Render<UnsTree>(p => p.Add(x => x.Roots, new[] { node }).Add(x => x.OnToggleExpand, n => toggled = n));
|
||||
var button = cut.Find("button[aria-expanded]");
|
||||
button.GetAttribute("aria-expanded").ShouldBe("false");
|
||||
button.GetAttribute("aria-label")!.ShouldContain("press-01");
|
||||
button.Click();
|
||||
toggled.ShouldBeSameAs(node);
|
||||
node.Expanded.ShouldBeFalse(); // Host, not renderer, owns lazy expansion.
|
||||
node.Expanded = true; cut.Render();
|
||||
cut.Find("button[aria-expanded]").GetAttribute("aria-expanded").ShouldBe("true");
|
||||
}
|
||||
|
||||
/// <summary>Filter-open ancestors expose their displayed state without mutating stored expansion.</summary>
|
||||
[Fact]
|
||||
public void UnsFilter_ExpansionStateMatchesDisplayedLoadedPath()
|
||||
{
|
||||
var root = new UnsNode { Kind = UnsNodeKind.Area, Key = "area:1", DisplayName = "Area", Expanded = false };
|
||||
root.Children.Add(new UnsNode { Kind = UnsNodeKind.Equipment, Key = "eq:1", DisplayName = "matched press" });
|
||||
var cut = Render<UnsTree>(p => p.Add(x => x.Roots, new[] { root }).Add(x => x.Filter, "matched"));
|
||||
cut.Find("button[aria-expanded]").GetAttribute("aria-expanded").ShouldBe("true");
|
||||
root.Expanded.ShouldBeFalse();
|
||||
cut.Markup.ShouldContain("matched press");
|
||||
}
|
||||
|
||||
/// <summary>Repeated row actions name the owning row and retain its exact callback object.</summary>
|
||||
[Fact]
|
||||
public void UnsActions_UseContextualNamesAndRetainSelectedNode()
|
||||
{
|
||||
var first = new UnsNode { Kind = UnsNodeKind.Area, Key = "area:1", DisplayName = "press-01" };
|
||||
var second = new UnsNode { Kind = UnsNodeKind.Area, Key = "area:2", DisplayName = "press-02" };
|
||||
UnsNode? edited = null;
|
||||
var cut = Render<UnsTree>(p => p.Add(x => x.Roots, new[] { first, second }).Add(x => x.OnEdit, n => edited = n));
|
||||
cut.Find("button[aria-label='Edit press-02']").Click();
|
||||
edited.ShouldBeSameAs(second);
|
||||
cut.Find("button[aria-label='Delete press-01']").TextContent.ShouldContain("Delete");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user