using Bunit;
using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Shared;
///
/// bUnit tests for the Create Instance template picker: templates are the only
/// selectable nodes, Select stays disabled until one is picked, double-click
/// selects, Cancel resolves null, and an InitialTemplateId opens the tree on
/// that template.
///
public class TemplatePickerDialogTests : BunitContext
{
private readonly DialogService _service = new();
/// Registers the dialog service and loose JS interop the picker dialog needs.
public TemplatePickerDialogTests()
{
Services.AddSingleton(_service);
JSInterop.Mode = JSRuntimeMode.Loose;
}
private static (List, List) Catalogue()
{
var a = new TemplateFolder("A") { Id = 1 };
var b = new TemplateFolder("B") { Id = 2, ParentFolderId = 1 };
var other = new TemplateFolder("Other") { Id = 3 };
var t7 = new Template("Deep") { Id = 7, FolderId = 2 };
var t8 = new Template("Elsewhere") { Id = 8, FolderId = 3 };
return (new() { a, b, other }, new() { t7, t8 });
}
private async Task<(IRenderedComponent cut, Task pending)> Open(int? initial = null)
{
var (f, t) = Catalogue();
var cut = Render();
Task pending = null!;
// Block body, not an expression body: an expression-bodied lambda returning the
// pending Task binds to InvokeAsync(Func) and would await the dialog itself.
await cut.InvokeAsync(() =>
{
pending = _service.ShowAsync(
"Choose template",
ctx => b =>
{
b.OpenComponent(0);
b.AddAttribute(1, nameof(TemplatePickerDialog.Context), ctx);
b.AddAttribute(2, nameof(TemplatePickerDialog.Folders), f);
b.AddAttribute(3, nameof(TemplatePickerDialog.Templates), t);
b.AddAttribute(4, nameof(TemplatePickerDialog.InitialTemplateId), initial);
b.CloseComponent();
},
size: "modal-lg");
});
cut.Render();
return (cut, pending);
}
private static void ExpandFolder(IRenderedComponent cut, string label) =>
cut.FindAll("li[role='treeitem']")
.First(li => li.QuerySelector(".tv-label")!.TextContent.Trim() == label)
.QuerySelector(".tv-toggle")!.Click();
private static AngleSharp.Dom.IElement Label(IRenderedComponent cut, string text) =>
cut.FindAll(".tv-label").First(l => l.TextContent.Trim() == text);
/// Verifies that the Select button is disabled until a template is clicked.
/// A task that represents the asynchronous operation.
[Fact]
public async Task Select_IsDisabled_UntilATemplateIsClicked()
{
var (cut, _) = await Open();
Assert.True(cut.Find("button[data-test='picker-select']").HasAttribute("disabled"));
Assert.Contains("Select a template", cut.Find("[data-test='picker-status']").TextContent);
}
/// Verifies that clicking a template and then Select resolves the dialog with that template's id.
/// A task that represents the asynchronous operation.
[Fact]
public async Task ClickingTemplate_ThenSelect_ResolvesItsId()
{
var (cut, pending) = await Open();
ExpandFolder(cut, "A");
ExpandFolder(cut, "B");
Label(cut, "Deep").Click();
Assert.False(cut.Find("button[data-test='picker-select']").HasAttribute("disabled"));
Assert.Contains("A / B / Deep", cut.Find("[data-test='picker-status']").TextContent);
cut.Find("button[data-test='picker-select']").Click();
Assert.Equal(7, (await pending)!.TemplateId);
}
/// Verifies that double-clicking a template resolves the dialog with that template.
/// A task that represents the asynchronous operation.
[Fact]
public async Task DoubleClickingTemplate_Resolves()
{
var (cut, pending) = await Open();
ExpandFolder(cut, "A");
ExpandFolder(cut, "B");
Label(cut, "Deep").DoubleClick();
Assert.Equal(7, (await pending)!.TemplateId);
}
/// Verifies that clicking a folder selects nothing.
/// A task that represents the asynchronous operation.
[Fact]
public async Task ClickingFolder_SelectsNothing()
{
var (cut, _) = await Open();
Label(cut, "A").Click();
Assert.True(cut.Find("button[data-test='picker-select']").HasAttribute("disabled"));
}
/// Verifies that Cancel resolves the dialog with null.
/// A task that represents the asynchronous operation.
[Fact]
public async Task Cancel_ResolvesNull()
{
var (cut, pending) = await Open();
cut.Find("button[data-test='picker-cancel']").Click();
Assert.Null(await pending);
}
/// Verifies that the initial template id is revealed in the tree and highlighted.
/// A task that represents the asynchronous operation.
[Fact]
public async Task InitialTemplateId_IsRevealedAndHighlighted()
{
var (cut, _) = await Open(initial: 7);
Assert.Contains("Deep", cut.Markup);
Assert.DoesNotContain("Elsewhere", cut.Markup);
Assert.False(cut.Find("button[data-test='picker-select']").HasAttribute("disabled"));
Assert.Contains("A / B / Deep", cut.Find("[data-test='picker-status']").TextContent);
Assert.Contains("text-primary", Label(cut, "Deep").ClassName);
}
/// Verifies that typing in the search box filters the tree.
/// A task that represents the asynchronous operation.
[Fact]
public async Task Search_FiltersTree()
{
var (cut, _) = await Open();
cut.Find("input[data-test='picker-search']").Input("Else");
Assert.Contains("Elsewhere", cut.Markup);
Assert.DoesNotContain("Deep", cut.Markup);
}
}