Files
Toiminnanohjaus/tests/Browser/ThemeToggleTest.php
T
petriandClaude Opus 5.5 7b6a909677 Add light/dark theme toggle remembered in the browser
- New partial resources/views/partials/theme-toggle.blade.php: a Flux
  button that flips $flux.dark. Flux stores the choice in localStorage
  (flux.appearance) and applies it before the page paints; until the
  user chooses, the system theme is followed.
- Shown in the header for signed-in users and at the top right of the
  guest pages.
- Accessible: stable name "Tumma teema" with a tooltip, state in
  aria-pressed, decorative moon/sun icons.
- Browser tests cover toggling, persistence across pages, overriding a
  dark system theme, keyboard use and axe checks in both themes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-22 11:25:01 +02:00

49 lines
2.1 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
const THEME_TOGGLE = '[data-theme-toggle]';
it('switches to the dark theme and back, and remembers the choice', function () {
$page = visit(route('login'))->on()->desktop()->inLightMode();
$page->assertScript('document.documentElement.classList.contains("dark")', false)
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'false')
->assertAttribute(THEME_TOGGLE, 'aria-label', 'Tumma teema')
->click(THEME_TOGGLE)
->assertScript('document.documentElement.classList.contains("dark")', true)
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'true')
->assertAttribute(THEME_TOGGLE, 'aria-label', 'Tumma teema')
->assertScript('localStorage.getItem("flux.appearance")', 'dark')
->assertNoAccessibilityIssues(level: 2);
$page->navigate(route('password.request'))
->assertScript('document.documentElement.classList.contains("dark")', true)
->click(THEME_TOGGLE)
->assertScript('document.documentElement.classList.contains("dark")', false)
->assertScript('localStorage.getItem("flux.appearance")', 'light')
->assertNoAccessibilityIssues(level: 2);
});
it('overrides a dark system theme', function () {
visit(route('login'))->inDarkMode()
->assertScript('document.documentElement.classList.contains("dark")', true)
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'true')
->click(THEME_TOGGLE)
->assertScript('document.documentElement.classList.contains("dark")', false)
->assertScript('localStorage.getItem("flux.appearance")', 'light');
});
it('is in the header for signed-in users and works with the keyboard', function () {
$this->actingAs(User::factory()->create());
visit(route('customers.index'))->inLightMode()
->assertPresent('header '.THEME_TOGGLE)
->keys(THEME_TOGGLE, 'Enter')
->assertScript('document.documentElement.classList.contains("dark")', true)
->assertScript('localStorage.getItem("flux.appearance")', 'dark');
});