Files
chrnx-dev-wp/tests/js/theme-preference.test.js
T
chrnxandClaude Opus 5 a142ece51d feat: rebuild the header controls as icon buttons
The theme switch was three labelled radios that wrapped the header onto
a second line with long translations. It is three SVG buttons now, with
search and menu sharing the same language: no resting outline, a filled
chip for the active choice.

Radios gave arrow-key roving for free, so the group reimplements it
rather than losing it. The first chip colour put accent on accent and
failed WCAG AA; the label carries full contrast instead. Mobile targets
sit at 45px because a floor of exactly 44 rounds to 43.99997 in Firefox.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-13 19:41:24 -05:00

161 lines
6.3 KiB
JavaScript

const modulePath = '../../assets/js/theme-toggle.js';
function createMedia( matches = false ) {
const listeners = new Set();
return {
matches,
addEventListener: jest.fn( ( _type, listener ) => listeners.add( listener ) ),
removeEventListener: jest.fn( ( _type, listener ) => listeners.delete( listener ) ),
change( nextMatches ) {
this.matches = nextMatches;
listeners.forEach( ( listener ) => listener( { matches: nextMatches } ) );
},
listenerCount: () => listeners.size,
};
}
function toggleMarkup() {
document.body.innerHTML = `
<div class="chrnx-theme-toggle" role="group" aria-label="Color theme" data-chrnx-theme-toggle
data-group="theme-a"
data-status-light="Light theme active"
data-status-dark="Dark theme active"
data-status-system-light="Light theme active, following system preference"
data-status-system-dark="Dark theme active, following system preference">
<span class="chrnx-theme-toggle__legend">Color theme</span>
<button type="button" data-chrnx-theme-value="light" aria-pressed="false">Light</button>
<button type="button" data-chrnx-theme-value="dark" aria-pressed="false">Dark</button>
<button type="button" data-chrnx-theme-value="system" aria-pressed="true">System</button>
<output data-chrnx-theme-status aria-live="polite">Following system theme</output>
</div>`;
}
describe( 'theme preference helpers', () => {
beforeEach( () => {
jest.resetModules();
document.documentElement.removeAttribute( 'data-theme' );
document.documentElement.style.colorScheme = '';
document.body.innerHTML = '';
} );
test( 'saved explicit preference wins, then system, then dark fallback', () => {
const { resolveTheme } = require( modulePath );
expect( resolveTheme( 'light', true ) ).toBe( 'light' );
expect( resolveTheme( 'dark', false ) ).toBe( 'dark' );
expect( resolveTheme( null, true ) ).toBe( 'dark' );
expect( resolveTheme( null, false ) ).toBe( 'light' );
expect( resolveTheme( null, undefined ) ).toBe( 'dark' );
} );
test.each( [ 'system', null, '{broken', 'unexpected', 3, {}, true ] )(
'treats %p as no explicit preference',
( stored ) => {
const { normalizePreference } = require( modulePath );
expect( normalizePreference( stored ) ).toBeNull();
},
);
test( 'only light and dark persist while system removes the single key', () => {
const { STORAGE_KEY, writePreference } = require( modulePath );
const storage = { setItem: jest.fn(), removeItem: jest.fn() };
writePreference( storage, 'light' );
writePreference( storage, 'dark' );
writePreference( storage, 'system' );
expect( storage.setItem.mock.calls ).toEqual( [
[ STORAGE_KEY, 'light' ],
[ STORAGE_KEY, 'dark' ],
] );
expect( storage.removeItem ).toHaveBeenCalledWith( STORAGE_KEY );
expect( STORAGE_KEY ).toBe( 'chrnx-theme' );
} );
test( 'storage denial never breaks initialization and falls back to system', () => {
const { initialize } = require( modulePath );
toggleMarkup();
const denied = {
getItem: () => {
throw new Error( 'denied' );
},
setItem: () => {
throw new Error( 'denied' );
},
removeItem: () => {
throw new Error( 'denied' );
},
};
const media = createMedia( true );
expect( () => initialize( { document, storage: denied, media } ) ).not.toThrow();
expect( document.documentElement.dataset.theme ).toBe( 'dark' );
const light = document.querySelector( '[data-chrnx-theme-value="light"]' );
expect( () => light.click() ).not.toThrow();
expect( document.documentElement.dataset.theme ).toBe( 'light' );
} );
test( 'system media changes update only without an explicit choice', () => {
const { initialize } = require( modulePath );
toggleMarkup();
const media = createMedia( false );
const storage = { getItem: () => null, setItem: jest.fn(), removeItem: jest.fn() };
initialize( { document, storage, media } );
expect( document.documentElement.dataset.theme ).toBe( 'light' );
media.change( true );
expect( document.documentElement.dataset.theme ).toBe( 'dark' );
document.querySelector( '[data-chrnx-theme-value="light"]' ).click();
media.change( false );
expect( document.documentElement.dataset.theme ).toBe( 'light' );
} );
test( 'synchronizes all native controls and reports the resolved state', () => {
const { initialize } = require( modulePath );
toggleMarkup();
document.body.insertAdjacentHTML(
'beforeend',
document.body.innerHTML.replaceAll( 'theme-a', 'theme-b' ),
);
const media = createMedia( true );
initialize( {
document,
storage: { getItem: () => null, setItem: jest.fn(), removeItem: jest.fn() },
media,
} );
document.querySelector( '[data-group="theme-a"] [data-chrnx-theme-value="light"]' ).click();
expect(
[ ...document.querySelectorAll( '[data-chrnx-theme-value="light"]' ) ].every(
( button ) => button.getAttribute( 'aria-pressed' ) === 'true',
),
).toBe( true );
expect(
[ ...document.querySelectorAll( '[data-chrnx-theme-status]' ) ].every( ( status ) =>
status.textContent.includes( 'Light' ),
),
).toBe( true );
expect( document.querySelector( '[data-chrnx-theme-status]' ).textContent ).toBe(
'Light theme active',
);
expect( document.querySelector( '[data-chrnx-theme-toggle]' ).textContent ).toContain(
'Color theme',
);
} );
test( 'applies data-theme and color-scheme together', () => {
const { applyTheme } = require( modulePath );
applyTheme( document, 'dark' );
expect( document.documentElement.dataset.theme ).toBe( 'dark' );
expect( document.documentElement.style.colorScheme ).toBe( 'dark' );
} );
test( 'reinitialization replaces listeners and never makes remote calls', () => {
const { initialize } = require( modulePath );
toggleMarkup();
const media = createMedia();
global.fetch = jest.fn();
const fetchSpy = jest.spyOn( global, 'fetch' );
initialize( { document, storage: null, media } );
initialize( { document, storage: null, media } );
expect( media.listenerCount() ).toBe( 1 );
expect( fetchSpy ).not.toHaveBeenCalled();
fetchSpy.mockRestore();
delete global.fetch;
} );
} );