158 lines
6.0 KiB
JavaScript
158 lines
6.0 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 = `
|
|
<fieldset class="chrnx-theme-toggle" data-chrnx-theme-toggle
|
|
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">
|
|
<legend>Color theme</legend>
|
|
<label><input type="radio" name="theme-a" value="light">Light</label>
|
|
<label><input type="radio" name="theme-a" value="dark">Dark</label>
|
|
<label><input type="radio" name="theme-a" value="system" checked>System</label>
|
|
<output data-chrnx-theme-status aria-live="polite">Following system theme</output>
|
|
</fieldset>`;
|
|
}
|
|
|
|
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( 'input[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( 'input[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( 'input[name="theme-a"][value="light"]' ).click();
|
|
expect(
|
|
[ ...document.querySelectorAll( 'input[value="light"]' ) ].every(
|
|
( input ) => input.checked,
|
|
),
|
|
).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( 'fieldset' ).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;
|
|
} );
|
|
} );
|