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; /// Pending caller state updates the same native editor without resetting its current draft. [Trait("Category", "Unit")] public sealed class MonacoReadOnlyTests : BunitContext { public MonacoReadOnlyTests() { Services.AddLogging(); JSInterop.SetupVoid("MonacoBlazor.dispose", _ => true).SetVoidResult(); JSInterop.SetupVoid("MonacoBlazor.setEditorOption", _ => true).SetVoidResult(); } [Fact] public void PendingAndRecoveryToggleNativeReadOnlyWithoutRecreatingOrReplacingText() { JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true).SetVoidResult(); var cut = Render(p => p.Add(x => x.Value, "return 1;").Add(x => x.EquipmentId, "synthetic-equipment")); var created = Assert.Single(JSInterop.Invocations["MonacoBlazor.createEditor"]); var options = created.Arguments[2]!; options.GetType().GetProperty("readOnly")!.GetValue(options).ShouldBe(false); cut.Render(p => p.Add(x => x.ReadOnly, true)); var locked = Assert.Single(JSInterop.Invocations["MonacoBlazor.setEditorOption"]); locked.Arguments[0].ShouldBe(created.Arguments[0]); locked.Arguments[1].ShouldBe("readOnly"); locked.Arguments[2].ShouldBe(true); cut.Render(p => p.Add(x => x.Height, "400px")); JSInterop.Invocations["MonacoBlazor.setEditorOption"].Count.ShouldBe(1); cut.Render(p => p.Add(x => x.ReadOnly, false)); var recovered = JSInterop.Invocations["MonacoBlazor.setEditorOption"].Last(); recovered.Arguments[0].ShouldBe(created.Arguments[0]); recovered.Arguments[1].ShouldBe("readOnly"); recovered.Arguments[2].ShouldBe(false); JSInterop.Invocations["MonacoBlazor.createEditor"].Count.ShouldBe(1); JSInterop.Invocations["MonacoBlazor.setValue"].ShouldBeEmpty(); } [Fact] public void PendingChangedDuringCreationIsAppliedAfterTheOriginalEditorExists() { var create = JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true); var cut = Render(p => p.Add(x => x.ReadOnly, false)); cut.Render(p => p.Add(x => x.ReadOnly, true)); JSInterop.Invocations["MonacoBlazor.setEditorOption"].ShouldBeEmpty(); create.SetVoidResult(); cut.WaitForAssertion(() => { var update = Assert.Single(JSInterop.Invocations["MonacoBlazor.setEditorOption"]); update.Arguments[1].ShouldBe("readOnly"); update.Arguments[2].ShouldBe(true); }); JSInterop.Invocations["MonacoBlazor.createEditor"].Count.ShouldBe(1); } [Fact] public async Task ReadOnlyStillReadsTheCurrentNativeDraftAndReportsItThroughTheExistingCallback() { JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true).SetVoidResult(); JSInterop.Setup("MonacoBlazor.getValue", _ => true).SetResult("return 2;"); string? reported = null; var cut = Render(p => p.Add(x => x.Value, "return 1;") .Add(x => x.ValueChanged, value => reported = value)); cut.Render(p => p.Add(x => x.ReadOnly, true)); var result = await cut.InvokeAsync(() => cut.Instance.GetValueAsync()); result.Ok.ShouldBeTrue(); result.Value.ShouldBe("return 2;"); reported.ShouldBe("return 2;"); JSInterop.Invocations["MonacoBlazor.setValue"].ShouldBeEmpty(); JSInterop.Invocations["MonacoBlazor.createEditor"].Count.ShouldBe(1); } }