Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/ScriptAnalysis/MonacoAccessibilityTests.cs
T

61 lines
3.1 KiB
C#

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>Editor names reach the real Monaco creation/update contract without changing content.</summary>
[Trait("Category", "Unit")]
public sealed class MonacoAccessibilityTests : BunitContext
{
public MonacoAccessibilityTests()
{
Services.AddLogging();
JSInterop.SetupVoid("MonacoBlazor.dispose", _ => true).SetVoidResult();
JSInterop.SetupVoid("MonacoBlazor.setEditorOption", _ => true).SetVoidResult();
}
/// <summary>The fallback name is sent to Monaco, not only placed on an outer wrapper.</summary>
[Fact]
public void DefaultName_IsPartOfNativeEditorCreationOptions()
{
JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true).SetVoidResult();
Render<MonacoEditor>();
var options = JSInterop.Invocations["MonacoBlazor.createEditor"].Single().Arguments[2]!;
options.GetType().GetProperty("ariaLabel").ShouldNotBeNull();
options.GetType().GetProperty("ariaLabel")!.GetValue(options).ShouldBe("Code editor");
}
/// <summary>Renaming the editor uses existing updateOptions without changing its text/context/language.</summary>
[Fact]
public void CallerName_UpdatesWithoutReplacingContentOrLanguage()
{
JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true).SetVoidResult();
var cut = Render<MonacoEditor>(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();
}
/// <summary>A name changed during a delayed creation is synchronized after creation completes.</summary>
[Fact]
public void CallerName_ChangedWhileCreationWaits_IsNotLost()
{
var create = JSInterop.SetupVoid("MonacoBlazor.createEditor", _ => true);
var cut = Render<MonacoEditor>(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"));
}
}