using Bunit;
using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Shared;
///
/// Tests for T34b's . The component is pure
/// JS-interop over window.sbTheme (theme.js): sbTheme.get reflects
/// the persisted theme on first render and sbTheme.toggle flips it on
/// click. The toggle carries the required a11y attributes
/// (aria-label="Toggle dark mode" + aria-pressed).
///
public class DarkModeToggleTests : BunitContext
{
public DarkModeToggleTests()
{
// Loose mode: unconfigured interop returns default. We configure the two
// calls the component actually makes (get on first render, toggle on click).
JSInterop.Mode = JSRuntimeMode.Loose;
}
[Fact]
public void Renders_Button_WithToggleDarkModeAriaLabel()
{
JSInterop.Setup("sbTheme.get").SetResult("light");
var cut = Render();
var button = cut.Find("button[aria-label='Toggle dark mode']");
Assert.NotNull(button);
}
[Fact]
public void OnFirstRender_PersistedLight_AriaPressedFalse()
{
// sbTheme.get returns light → button reflects light (aria-pressed=false,
// moon glyph offering "switch to dark").
JSInterop.Setup("sbTheme.get").SetResult("light");
var cut = Render();
var button = cut.Find("button.sb-theme-toggle");
Assert.Equal("false", button.GetAttribute("aria-pressed"));
Assert.NotNull(cut.Find("i.bi-moon-stars"));
}
[Fact]
public void OnFirstRender_PersistedDark_AriaPressedTrue()
{
// sbTheme.get returns dark → button reflects dark (aria-pressed=true,
// sun glyph offering "switch to light").
JSInterop.Setup("sbTheme.get").SetResult("dark");
var cut = Render();
cut.WaitForAssertion(() =>
{
var button = cut.Find("button.sb-theme-toggle");
Assert.Equal("true", button.GetAttribute("aria-pressed"));
Assert.NotNull(cut.Find("i.bi-sun"));
});
}
[Fact]
public void Click_InvokesToggle_AndReflectsNewState()
{
// Start persisted-light; toggling returns "dark". The click handler must
// invoke sbTheme.toggle and flip aria-pressed/glyph to the dark state.
JSInterop.Setup("sbTheme.get").SetResult("light");
var toggle = JSInterop.Setup("sbTheme.toggle").SetResult("dark");
var cut = Render();
cut.Find("button.sb-theme-toggle").Click();
cut.WaitForAssertion(() =>
{
// The handler invoked sbTheme.toggle.
Assert.Single(toggle.Invocations);
var button = cut.Find("button.sb-theme-toggle");
Assert.Equal("true", button.GetAttribute("aria-pressed"));
Assert.NotNull(cut.Find("i.bi-sun"));
});
}
}