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; /// Common editor bodies retain native names and stable repeated-instance associations. [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>(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> 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> cut,string id)=>cut.FindAll("[id]").Single(e=>e.Id==id); }