254 lines
15 KiB
C#
254 lines
15 KiB
C#
using System.Reflection;
|
|
using Bunit;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Raw;
|
|
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>Simple wrapper names, field relationships and draft/pending ownership.</summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class SimpleModalAccessibilityTests : BunitContext
|
|
{
|
|
public SimpleModalAccessibilityTests()
|
|
{
|
|
JSInterop.Setup<bool>("otDialog.activate", _ => true).SetResult(true);
|
|
JSInterop.SetupVoid("otDialog.update", _ => true).SetVoidResult();
|
|
JSInterop.SetupVoid("otDialog.deactivate", _ => true).SetVoidResult();
|
|
Services.AddSingleton(DispatchProxy.Create<IUnsTreeService, RefuseOperations>());
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("name")]
|
|
[InlineData("driver")]
|
|
[InlineData("message")]
|
|
[InlineData("area")]
|
|
[InlineData("line")]
|
|
public void Wrapper_IsNamed_WithSafeCancel_AndUniqueFields(string kind)
|
|
{
|
|
var first = RenderWrapper(kind);
|
|
var second = RenderWrapper(kind);
|
|
foreach (var cut in new[] { first, second })
|
|
{
|
|
var dialog = cut.Find("[role=dialog]");
|
|
dialog.GetAttribute("aria-modal").ShouldBe("true");
|
|
var titleId = dialog.GetAttribute("aria-labelledby");
|
|
titleId.ShouldNotBeNullOrWhiteSpace();
|
|
cut.Find("#" + titleId).TextContent.ShouldNotBeNullOrWhiteSpace();
|
|
cut.Find(".modal-footer [data-initial-focus]").TextContent.Trim().ShouldBe(kind == "message" ? "Close" : "Cancel");
|
|
foreach (var input in cut.FindAll("input:not([type=hidden]),select,textarea"))
|
|
{
|
|
input.Id.ShouldNotBeNullOrWhiteSpace();
|
|
cut.FindAll("label").ShouldContain(label => label.GetAttribute("for") == input.Id);
|
|
foreach (var id in (input.GetAttribute("aria-describedby") ?? "").Split(' ',StringSplitOptions.RemoveEmptyEntries))
|
|
cut.Find("#" + id).ShouldNotBeNull();
|
|
}
|
|
}
|
|
var ids = first.FindAll("[id]").Concat(second.FindAll("[id]")).Select(n => n.Id).ToArray();
|
|
ids.Distinct(StringComparer.Ordinal).Count().ShouldBe(ids.Length);
|
|
}
|
|
|
|
[Fact]
|
|
public void Area_UnrelatedRenderPreservesDraft()
|
|
{
|
|
var cut = Render<AreaModal>(p => p.Add(x => x.Visible,true).Add(x => x.IsNew,true).Add(x => x.ClusterId,"C1"));
|
|
cut.FindAll("input").Single(x => x.Id?.Contains("area-name",StringComparison.Ordinal) == true).Change("local-draft");
|
|
cut.Render(p => p.Add(x => x.Clusters,new[] {("C1","Updated label")}));
|
|
cut.FindAll("input").Single(x => x.Id?.Contains("area-name",StringComparison.Ordinal) == true).GetAttribute("value").ShouldBe("local-draft");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Name_PendingSubmitBlocksClose_AndCannotCloseNewTarget()
|
|
{
|
|
var pending = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var calls=0; var closes=0;
|
|
var cut = Render<RawNameDialog>(p => p.Add(x => x.Visible,true).Add(x => x.Title,"Rename A").Add(x => x.Value,"A")
|
|
.Add(x => x.VisibleChanged, _ => closes++).Add(x => x.OnSubmit,async _ => {calls++;entered.SetResult();await pending.Task;}));
|
|
var submit = cut.Find(".modal-footer .btn-primary").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
|
await entered.Task.WaitAsync(TimeSpan.FromSeconds(10), Xunit.TestContext.Current.CancellationToken);
|
|
try
|
|
{
|
|
cut.Find(".btn-close").HasAttribute("disabled").ShouldBeTrue();
|
|
cut.Find("fieldset").HasAttribute("disabled").ShouldBeTrue();
|
|
cut.Render(p => p.Add(x => x.Title,"Rename B").Add(x => x.Value,"B").Add(x => x.VisibleChanged,_ => closes+=100));
|
|
}
|
|
finally {pending.SetResult();await submit;}
|
|
calls.ShouldBe(1);closes.ShouldBe(0);
|
|
cut.Find("input").GetAttribute("value").ShouldBe("B");
|
|
cut.FindAll("[role=dialog]").Count.ShouldBe(1);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false, false)]
|
|
[InlineData(false, true)]
|
|
[InlineData(true, false)]
|
|
[InlineData(true, true)]
|
|
public async Task AreaAndLine_PendingSnapshotCannotAffectNewPresentation(bool line, bool failure)
|
|
{
|
|
Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.RemoveAll<IUnsTreeService>(Services);
|
|
var service = DispatchProxy.Create<IUnsTreeService, DelayedOperations>();
|
|
var delayed = (DelayedOperations)(object)service;
|
|
Services.AddSingleton(service);
|
|
var version = new byte[] { 1, 2 };
|
|
var saves = 0; var cancels = 0;
|
|
Dictionary<string,object> Parameters(bool second) => new() {
|
|
["Visible"] = true, ["IsNew"] = false,
|
|
["Existing"] = line
|
|
? new LineEditDto(second ? "B" : "A", "AREA1", second ? "beta" : "alpha", second ? "new notes" : "old notes", second ? [3] : version)
|
|
: (object)new AreaEditDto(second ? "B" : "A", second ? "beta" : "alpha", second ? "new notes" : "old notes", "C1", second ? [3] : version),
|
|
["OnSaved"] = EventCallback.Factory.Create(this, () => saves += second ? 100 : 1),
|
|
["OnCancel"] = EventCallback.Factory.Create(this, () => cancels++)
|
|
};
|
|
var cut = Render<DynamicComponent>(p => p.Add(x => x.Type,line ? typeof(LineModal) : typeof(AreaModal)).Add(x => x.Parameters, Parameters(false)));
|
|
var submitting = cut.Find("form").TriggerEventAsync("onsubmit",EventArgs.Empty);
|
|
await delayed.Entered.Task.WaitAsync(TimeSpan.FromSeconds(10),Xunit.TestContext.Current.CancellationToken);
|
|
try
|
|
{
|
|
cut.Find("fieldset").HasAttribute("disabled").ShouldBeTrue();
|
|
cut.Find(".btn-close").HasAttribute("disabled").ShouldBeTrue();
|
|
await cut.Find(".modal-footer .btn-outline-secondary").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
|
await cut.Find("form").TriggerEventAsync("onsubmit",EventArgs.Empty);
|
|
delayed.Calls.ShouldBe(1);cancels.ShouldBe(0);
|
|
version[0] = 9;
|
|
cut.Render(p => p.Add(x => x.Parameters,Parameters(true)));
|
|
cut.FindAll("input").Single(x => x.Id?.EndsWith("-name",StringComparison.Ordinal)==true).GetAttribute("value").ShouldBe("beta");
|
|
}
|
|
finally { delayed.Pending.SetResult(new UnsMutationResult(!failure,failure ? "Old target failure" : null)); await submitting; }
|
|
delayed.Method.ShouldBe(line ? "UpdateLineAsync" : "UpdateAreaAsync");
|
|
delayed.Arguments![0].ShouldBe("A");delayed.Arguments[1].ShouldBe("alpha");delayed.Arguments[2].ShouldBe("old notes");
|
|
((byte[])delayed.Arguments[4]!).ShouldBe(new byte[] { 1,2 });
|
|
saves.ShouldBe(0);cancels.ShouldBe(0);
|
|
cut.Markup.ShouldNotContain("Old target failure");
|
|
cut.FindAll("[role=dialog]").Count.ShouldBe(1);
|
|
cut.FindAll("input").Single(x => x.Id?.EndsWith("-name",StringComparison.Ordinal)==true).GetAttribute("value").ShouldBe("beta");
|
|
}
|
|
|
|
public class DelayedOperations : DispatchProxy
|
|
{
|
|
public readonly TaskCompletionSource Entered = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
public readonly TaskCompletionSource<UnsMutationResult> Pending = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
public int Calls;
|
|
public string? Method;
|
|
public object?[]? Arguments;
|
|
protected override object? Invoke(MethodInfo? method,object?[]? args)
|
|
{
|
|
if (method?.Name is not ("UpdateAreaAsync" or "UpdateLineAsync")) throw new InvalidOperationException("Unexpected operation: "+method?.Name);
|
|
Calls++;Method=method.Name;Arguments=args;Entered.TrySetResult();return Pending.Task;
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Driver_PendingTypedPayloadAndCallbackStayWithOriginalTarget()
|
|
{
|
|
var pending = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
(string DriverType,string Name) submitted=default;
|
|
var closes=0;var newerCalls=0;
|
|
var cut=Render<RawDriverTypeDialog>(p=>p.Add(x=>x.Visible,true).Add(x=>x.Target,"A")
|
|
.Add(x=>x.VisibleChanged,_=>closes++).Add(x=>x.OnSubmit,async value=>{submitted=value;entered.SetResult();await pending.Task;}));
|
|
cut.Find("select").Change("S7");
|
|
await cut.Find("input").TriggerEventAsync("oninput",new ChangeEventArgs{Value="alpha"});
|
|
var saving=cut.Find(".modal-footer .btn-primary").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
|
await entered.Task.WaitAsync(TimeSpan.FromSeconds(10),Xunit.TestContext.Current.CancellationToken);
|
|
try
|
|
{
|
|
await cut.Find(".modal-footer .btn-outline-secondary").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
|
await cut.Find(".modal-footer .btn-primary").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
|
cut.Render(p=>p.Add(x=>x.Target,"B").Add(x=>x.OnSubmit,_=>newerCalls++).Add(x=>x.VisibleChanged,_=>closes+=100));
|
|
}
|
|
finally {pending.SetResult();await saving;}
|
|
submitted.ShouldBe(("S7","alpha"));closes.ShouldBe(0);newerCalls.ShouldBe(0);
|
|
cut.Find("input").GetAttribute("value").ShouldBe("");
|
|
cut.Find("select").GetAttribute("value").ShouldBe("Modbus");
|
|
cut.FindAll("[role=dialog]").Count.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Name_DisposalInvalidatesPendingCloseWithoutCancellingSubmittedAction()
|
|
{
|
|
var pending = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var completed=false;var closes=0;
|
|
var cut=Render<RawNameDialog>(p=>p.Add(x=>x.Visible,true).Add(x=>x.Value,"alpha")
|
|
.Add(x=>x.VisibleChanged,_=>closes++).Add(x=>x.OnSubmit,async _=>{entered.SetResult();await pending.Task;completed=true;}));
|
|
var saving=cut.Find(".modal-footer .btn-primary").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
|
await entered.Task.WaitAsync(TimeSpan.FromSeconds(10),Xunit.TestContext.Current.CancellationToken);
|
|
await DisposeComponentsAsync();pending.SetResult();await saving;
|
|
completed.ShouldBeTrue();closes.ShouldBe(0);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public async Task AreaAndLine_PendingCancelDispatchesOnlyOnce(bool line)
|
|
{
|
|
var pending=new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var entered=new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var calls=0;
|
|
var parameters=new Dictionary<string,object>{["Visible"]=true,["IsNew"]=true,
|
|
["OnCancel"]=EventCallback.Factory.Create(this,async()=>{calls++;entered.TrySetResult();await pending.Task;})};
|
|
var cut=Render<DynamicComponent>(p=>p.Add(x=>x.Type,line?typeof(LineModal):typeof(AreaModal)).Add(x=>x.Parameters,parameters));
|
|
var first=cut.Find(".modal-footer .btn-outline-secondary").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
|
await entered.Task.WaitAsync(TimeSpan.FromSeconds(10),Xunit.TestContext.Current.CancellationToken);
|
|
Task? second=null;
|
|
try
|
|
{
|
|
second=cut.Find(".modal-footer .btn-outline-secondary").TriggerEventAsync("onclick",new Microsoft.AspNetCore.Components.Web.MouseEventArgs());
|
|
calls.ShouldBe(1);
|
|
}
|
|
finally {pending.SetResult();await first;if(second is not null)await second;}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public async Task EditWithoutLoadedTargetCannotSubmitPreviousDraft(bool line)
|
|
{
|
|
Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.RemoveAll<IUnsTreeService>(Services);
|
|
var service=DispatchProxy.Create<IUnsTreeService, RecordingOperations>();
|
|
Services.AddSingleton(service);
|
|
var parameters=new Dictionary<string,object>{["Visible"]=true,["IsNew"]=false,["Existing"]=line
|
|
? new LineEditDto("A","AREA1","alpha",null,[1])
|
|
: (object)new AreaEditDto("A","alpha",null,"C1",[1])};
|
|
var cut=Render<DynamicComponent>(p=>p.Add(x=>x.Type,line?typeof(LineModal):typeof(AreaModal)).Add(x=>x.Parameters,parameters));
|
|
parameters=new Dictionary<string,object>(parameters){["Existing"]=null!};
|
|
cut.Render(p=>p.Add(x=>x.Parameters,parameters));
|
|
await cut.Find("form").TriggerEventAsync("onsubmit",EventArgs.Empty);
|
|
((RecordingOperations)(object)service).Calls.ShouldBe(0);
|
|
}
|
|
|
|
public class RecordingOperations:DispatchProxy
|
|
{
|
|
public int Calls;
|
|
protected override object? Invoke(MethodInfo? method,object?[]? args)
|
|
{
|
|
if(method?.Name is not ("UpdateAreaAsync" or "UpdateLineAsync"))throw new InvalidOperationException("Unexpected operation");
|
|
Calls++;return Task.FromResult(new UnsMutationResult(false,"Not dispatched to a real provider"));
|
|
}
|
|
}
|
|
|
|
private IRenderedComponent<DynamicComponent> RenderWrapper(string kind)
|
|
{
|
|
var type = kind switch { "name" => typeof(RawNameDialog), "driver" => typeof(RawDriverTypeDialog),
|
|
"message" => typeof(RawStubModal), "area" => typeof(AreaModal), "line" => typeof(LineModal),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(kind)) };
|
|
var parameters = new Dictionary<string, object> { ["Visible"] = true };
|
|
if (kind == "name") parameters["Title"] = "New folder";
|
|
if (kind == "message") { parameters["Title"] = "Rename warning"; parameters["Message"] = "Synthetic read-only warning"; }
|
|
if (kind is "area" or "line") parameters["IsNew"] = true;
|
|
if (kind == "area") parameters["ClusterId"] = "C1";
|
|
if (kind == "line") parameters["UnsAreaId"] = "A1";
|
|
return Render<DynamicComponent>(p => p.Add(x => x.Type,type).Add(x => x.Parameters,parameters));
|
|
}
|
|
|
|
public class RefuseOperations : DispatchProxy
|
|
{
|
|
protected override object? Invoke(MethodInfo? method,object?[]? args) => throw new InvalidOperationException("No service operation allowed: "+method?.Name);
|
|
}
|
|
}
|