theme.test.ts
typescript
sha256:0997d6250ae6476362f6fe2025af7789f46d03df3e9f34356d5e8ee79b201923
fix(issues): use issue number as pagination cursor, not cre…
Sonnet 4.6
patch
8 days ago
| 1 | import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | import { applyStoredTheme, toggleTheme } from './theme'; |
| 3 | |
| 4 | // Minimal DOM shim — vitest runs in node |
| 5 | const makeHtml = () => ({ dataset: {} as Record<string, string> }); |
| 6 | |
| 7 | let html: ReturnType<typeof makeHtml>; |
| 8 | let store: Record<string, string>; |
| 9 | |
| 10 | beforeEach(() => { |
| 11 | html = makeHtml(); |
| 12 | store = {}; |
| 13 | vi.stubGlobal('document', { documentElement: html }); |
| 14 | vi.stubGlobal('localStorage', { |
| 15 | getItem: (k: string) => store[k] ?? null, |
| 16 | setItem: (k: string, v: string) => { store[k] = v; }, |
| 17 | removeItem: (k: string) => { delete store[k]; }, |
| 18 | }); |
| 19 | }); |
| 20 | |
| 21 | describe('applyStoredTheme', () => { |
| 22 | it('sets data-theme=light when localStorage has light', () => { |
| 23 | store['musehub-theme'] = 'light'; |
| 24 | applyStoredTheme(); |
| 25 | expect(html.dataset['theme']).toBe('light'); |
| 26 | }); |
| 27 | |
| 28 | it('does nothing when localStorage has dark', () => { |
| 29 | store['musehub-theme'] = 'dark'; |
| 30 | applyStoredTheme(); |
| 31 | expect(html.dataset['theme']).toBeUndefined(); |
| 32 | }); |
| 33 | |
| 34 | it('does nothing when localStorage is empty', () => { |
| 35 | applyStoredTheme(); |
| 36 | expect(html.dataset['theme']).toBeUndefined(); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | describe('toggleTheme', () => { |
| 41 | it('dark → light: sets data-theme and saves light', () => { |
| 42 | toggleTheme(); |
| 43 | expect(html.dataset['theme']).toBe('light'); |
| 44 | expect(store['musehub-theme']).toBe('light'); |
| 45 | }); |
| 46 | |
| 47 | it('light → dark: removes data-theme and saves dark', () => { |
| 48 | html.dataset['theme'] = 'light'; |
| 49 | toggleTheme(); |
| 50 | expect(html.dataset['theme']).toBeUndefined(); |
| 51 | expect(store['musehub-theme']).toBe('dark'); |
| 52 | }); |
| 53 | |
| 54 | it('round-trips correctly', () => { |
| 55 | toggleTheme(); // → light |
| 56 | toggleTheme(); // → dark |
| 57 | expect(html.dataset['theme']).toBeUndefined(); |
| 58 | expect(store['musehub-theme']).toBe('dark'); |
| 59 | }); |
| 60 | }); |
File History
1 commit
sha256:0997d6250ae6476362f6fe2025af7789f46d03df3e9f34356d5e8ee79b201923
fix(issues): use issue number as pagination cursor, not cre…
Sonnet 4.6
patch
8 days ago