Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import { afterEach, describe, expect, it, vi } from 'vitest'import { THEME_KEY, applyTheme, isThemeChoice, readThemeChoice, resolveTheme, setTheme,} from './theme'
afterEach(() => { vi.unstubAllGlobals()})
function stubStorage(initial: Record<string, string> = {}) { const values = new Map(Object.entries(initial)) vi.stubGlobal('localStorage', { getItem: (key: string) => values.get(key) ?? null, setItem: (key: string, value: string) => values.set(key, value), }) return values}
function stubSystem(dark: boolean) { vi.stubGlobal('matchMedia', (query: string) => ({ matches: dark, media: query, addEventListener: () => {}, removeEventListener: () => {}, }))}
const root = () => ({ dataset: {}, style: {} }) as unknown as HTMLElement
describe('isThemeChoice', () => { it('accepts exactly the three choices', () => { expect(isThemeChoice('system')).toBe(true) expect(isThemeChoice('light')).toBe(true) expect(isThemeChoice('dark')).toBe(true) expect(isThemeChoice('auto')).toBe(false) expect(isThemeChoice(undefined)).toBe(false) })})
describe('resolveTheme', () => { it('follows the system only for the system choice', () => { stubSystem(true) expect(resolveTheme('system')).toBe('dark') expect(resolveTheme('light')).toBe('light') stubSystem(false) expect(resolveTheme('system')).toBe('light') expect(resolveTheme('dark')).toBe('dark') })})
describe('readThemeChoice', () => { it('reads a stored choice and falls back to system', () => { stubStorage({ [THEME_KEY]: 'light' }) expect(readThemeChoice()).toBe('light') stubStorage({ [THEME_KEY]: 'nonsense' }) expect(readThemeChoice()).toBe('system') stubStorage() expect(readThemeChoice()).toBe('system') })})
describe('applyTheme', () => { it('writes the resolved theme, the choice and color-scheme', () => { vi.stubGlobal('matchMedia', () => ({ matches: true, addEventListener: () => {}, removeEventListener: () => {}, })) const element = root() applyTheme('system', element) expect(element.dataset.theme).toBe('dark') expect(element.dataset.choice).toBe('system') expect(element.style.colorScheme).toBe('dark')
const light = root() applyTheme('light', light) expect(light.dataset.theme).toBe('light') expect(light.dataset.choice).toBe('light') })})
describe('setTheme', () => { it('persists the choice and applies it', () => { const values = stubStorage() const element = root() vi.stubGlobal('document', { documentElement: element }) setTheme('dark') expect(values.get(THEME_KEY)).toBe('dark') expect(element.dataset.theme).toBe('dark') expect(element.dataset.choice).toBe('dark') })})