Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/CommonControlAccessibilityTests.cs
T
2026-11-13 21:38:19 -05:00

59 lines
2.9 KiB
C#

using AngleSharp.Dom;
using Bunit;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Pickers;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
/// <summary>Common editor bodies retain native names and stable repeated-instance associations.</summary>
[Trait("Category", "Unit")]
public sealed class CommonControlAccessibilityTests : BunitContext
{
[Theory]
[InlineData(typeof(ModbusAddressPickerBody))]
[InlineData(typeof(AbLegacyAddressPickerBody))]
[InlineData(typeof(S7AddressPickerBody))]
[InlineData(typeof(DriverIdentitySection))]
[InlineData(typeof(DriverResilienceSection))]
public void CommonFields_TwoInstancesHaveResolvedNames(Type type)
{
var cut = Render<CascadingValue<EditContext>>(p => p.Add(x => x.Value,new EditContext(new object()))
.Add(x => x.ChildContent,(RenderFragment)(builder =>
{
for (int i=0;i<2;i++)
{
builder.OpenComponent(0,type);builder.SetKey(i);
if(type==typeof(DriverIdentitySection))
{
builder.AddAttribute(1,"Model",new DriverIdentitySection.DriverIdentityModel());
builder.AddAttribute(2,"IsNew",true);builder.AddAttribute(3,"ShowDriverType",true);
}
builder.CloseComponent();
}
})));
AssertRelationships(cut);
}
private static void AssertRelationships(IRenderedComponent<CascadingValue<EditContext>> cut)
{
var ids=cut.FindAll("[id]").Select(e=>e.Id).ToArray();
ids.Distinct(StringComparer.Ordinal).Count().ShouldBe(ids.Length);
var inputs=cut.FindAll("input,select,textarea");inputs.ShouldNotBeEmpty();
foreach(var input in inputs)
{
var name=input.GetAttribute("aria-label") ?? string.Join(" ",cut.FindAll("label").Where(e=>e.GetAttribute("for")==input.Id&&!string.IsNullOrEmpty(input.Id)).Select(e=>e.TextContent));
if(string.IsNullOrWhiteSpace(name)&&input.GetAttribute("aria-labelledby") is {} labelledBy)
name=string.Join(" ",labelledBy.Split(' ',StringSplitOptions.RemoveEmptyEntries).Select(id=>Find(cut,id).TextContent));
if(string.IsNullOrWhiteSpace(name)) name=input.Closest("label")?.TextContent;
name.ShouldNotBeNullOrWhiteSpace(input.OuterHtml);
foreach(var id in (input.GetAttribute("aria-describedby")??"").Split(' ',StringSplitOptions.RemoveEmptyEntries)) Find(cut,id).ShouldNotBeNull();
}
foreach(var label in cut.FindAll("label[for]"))Find(cut,label.GetAttribute("for")!).ShouldNotBeNull();
}
private static IElement Find(IRenderedComponent<CascadingValue<EditContext>> cut,string id)=>cut.FindAll("[id]").Single(e=>e.Id==id);
}