using Bunit; using Microsoft.Extensions.DependencyInjection; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.ScriptAnalysis; /// Editor names reach the real Monaco creation/update contract without changing content. [Trait("Category", "Unit")] public sealed class MonacoAccessibilityTests : BunitContext { public MonacoAccessibilityTests() { Services.AddLogging(); JSInterop.SetupVoid("MonacoBlazor.dispose", _ => true).SetVoidResult(); JSInterop.SetupVoid("MonacoBlazor.setEditorOption", _ => true).SetVoidResult(); } /// The fallback name is sent to Monaco, not only placed on an outer wrapper. [Fact] public void DefaultName_IsPartOfNativeEditorCreationOptions() { JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true).SetVoidResult(); Render(); var options = JSInterop.Invocations["MonacoBlazor.createEditor"].Single().Arguments[2]!; options.GetType().GetProperty("ariaLabel").ShouldNotBeNull(); options.GetType().GetProperty("ariaLabel")!.GetValue(options).ShouldBe("Code editor"); } /// Renaming the editor uses existing updateOptions without changing its text/context/language. [Fact] public void CallerName_UpdatesWithoutReplacingContentOrLanguage() { JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true).SetVoidResult(); var cut = Render(p => p.Add(x => x.AriaLabel, "Calculation script source") .Add(x => x.Value, "return 1;").Add(x => x.Language, "csharp").Add(x => x.EquipmentId, "equipment-local")); var options = JSInterop.Invocations["MonacoBlazor.createEditor"].Single().Arguments[2]!; options.GetType().GetProperty("ariaLabel")!.GetValue(options).ShouldBe("Calculation script source"); options.GetType().GetProperty("value")!.GetValue(options).ShouldBe("return 1;"); options.GetType().GetProperty("language")!.GetValue(options).ShouldBe("csharp"); options.GetType().GetProperty("equipmentId")!.GetValue(options).ShouldBe("equipment-local"); cut.Render(p => p.Add(x => x.AriaLabel, "Updated script source")); var update = JSInterop.Invocations["MonacoBlazor.setEditorOption"].Single(); update.Arguments[1].ShouldBe("ariaLabel"); update.Arguments[2].ShouldBe("Updated script source"); JSInterop.Invocations["MonacoBlazor.setValue"].ShouldBeEmpty(); } /// A name changed during a delayed creation is synchronized after creation completes. [Fact] public void CallerName_ChangedWhileCreationWaits_IsNotLost() { var create = JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true); var cut = Render(p => p.Add(x => x.AriaLabel, "First name")); cut.Render(p => p.Add(x => x.AriaLabel, "Current name")); create.SetVoidResult(); cut.WaitForAssertion(() => JSInterop.Invocations["MonacoBlazor.setEditorOption"].Last().Arguments[2].ShouldBe("Current name")); } }