fix(adminui): refresh script dropdown label after inline create
After inline "New script" creates an SC-… id, the entry is now added to _scripts BEFORE _form.ScriptId is set so the <InputSelect> has a matching <option> on first render and the displayed label is correct. Extracts VirtualTagModalHelpers.ResolveScriptLabel as a testable pure helper (5 new unit tests in VirtualTagScriptDropdownTests).
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Uns;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for <see cref="VirtualTagModalHelpers.ResolveScriptLabel"/> — the pure helper that
|
||||
/// resolves a script's display label from the in-memory options list. The bug scenario: after an
|
||||
/// inline "New script" create, the dropdown label drifted because the new entry was appended to the
|
||||
/// options list AFTER the selected value was set, so the rendered select had no matching option.
|
||||
/// The fix is to add the entry first, then set the selected value; this helper captures that
|
||||
/// invariant.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class VirtualTagScriptDropdownTests
|
||||
{
|
||||
// --- ResolveScriptLabel ---
|
||||
|
||||
[Fact]
|
||||
public void ResolveScriptLabel_ReturnsNull_WhenOptionsDoNotContainId()
|
||||
{
|
||||
// Represents the bug scenario: options list does NOT yet include the newly-created id.
|
||||
var options = new List<(string Id, string Display)>
|
||||
{
|
||||
("SC-existing001", "OldScript (CSharp)"),
|
||||
};
|
||||
|
||||
var label = VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-newscript00");
|
||||
|
||||
label.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResolveScriptLabel_ReturnsDisplay_WhenOptionsContainId()
|
||||
{
|
||||
// Represents the fixed scenario: the new entry is in the options before label is resolved.
|
||||
var options = new List<(string Id, string Display)>
|
||||
{
|
||||
("SC-existing001", "OldScript (CSharp)"),
|
||||
("SC-newscript00", "MyTag script (CSharp)"),
|
||||
};
|
||||
|
||||
var label = VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-newscript00");
|
||||
|
||||
label.ShouldBe("MyTag script (CSharp)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResolveScriptLabel_ReturnsNull_WhenSelectedIdIsEmpty()
|
||||
{
|
||||
var options = new List<(string Id, string Display)>
|
||||
{
|
||||
("SC-existing001", "OldScript (CSharp)"),
|
||||
};
|
||||
|
||||
var label = VirtualTagModalHelpers.ResolveScriptLabel(options, "");
|
||||
|
||||
label.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResolveScriptLabel_ReturnsNull_WhenOptionsIsEmpty()
|
||||
{
|
||||
var label = VirtualTagModalHelpers.ResolveScriptLabel(
|
||||
Array.Empty<(string Id, string Display)>(), "SC-newscript00");
|
||||
|
||||
label.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResolveScriptLabel_ReturnsCorrectLabel_WhenMultipleOptionsExist()
|
||||
{
|
||||
var options = new List<(string Id, string Display)>
|
||||
{
|
||||
("SC-aaa", "Alpha (CSharp)"),
|
||||
("SC-bbb", "Beta (CSharp)"),
|
||||
("SC-ccc", "Gamma (CSharp)"),
|
||||
};
|
||||
|
||||
VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-aaa").ShouldBe("Alpha (CSharp)");
|
||||
VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-bbb").ShouldBe("Beta (CSharp)");
|
||||
VirtualTagModalHelpers.ResolveScriptLabel(options, "SC-ccc").ShouldBe("Gamma (CSharp)");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user