Synchronize shared Monaco read-only state without replacing drafts

This commit is contained in:
Joseph Doherty
2026-11-13 23:29:35 -05:00
parent 88be7734b3
commit b4f7672d2a
7 changed files with 627 additions and 1 deletions
@@ -0,0 +1,77 @@
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;
/// <summary>Pending caller state updates the same native editor without resetting its current draft.</summary>
[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<MonacoEditor>(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<MonacoEditor>(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<string?>("MonacoBlazor.getValue", _ => true).SetResult("return 2;");
string? reported = null;
var cut = Render<MonacoEditor>(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);
}
}