|
| 1 | +import { beforeEach, afterEach, describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { JSDOM } from 'jsdom'; |
| 4 | +import { getInitialTheme, applyTheme, setupThemeToggle } from '../../frontend/theme.js'; |
| 5 | + |
| 6 | +let dom; |
| 7 | +let cleanupGlobals = []; |
| 8 | + |
| 9 | +function installDom(html = '<!doctype html><html><body></body></html>') { |
| 10 | + dom = new JSDOM(html, { url: 'https://example.test' }); |
| 11 | + const map = { |
| 12 | + window: dom.window, |
| 13 | + document: dom.window.document, |
| 14 | + localStorage: dom.window.localStorage, |
| 15 | + CustomEvent: dom.window.CustomEvent, |
| 16 | + getComputedStyle: dom.window.getComputedStyle |
| 17 | + }; |
| 18 | + |
| 19 | + cleanupGlobals = Object.entries(map).map(([key, value]) => { |
| 20 | + const previous = globalThis[key]; |
| 21 | + globalThis[key] = value; |
| 22 | + return [key, previous]; |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +function uninstallDom() { |
| 27 | + for (const [key, previous] of cleanupGlobals) { |
| 28 | + if (previous === undefined) { |
| 29 | + delete globalThis[key]; |
| 30 | + } else { |
| 31 | + globalThis[key] = previous; |
| 32 | + } |
| 33 | + } |
| 34 | + cleanupGlobals = []; |
| 35 | + dom?.window?.close(); |
| 36 | +} |
| 37 | + |
| 38 | +describe('theme.js', () => { |
| 39 | + beforeEach(() => { |
| 40 | + installDom(`<!doctype html><html><body> |
| 41 | + <button id="themeToggleBtn" aria-label="" title=""> |
| 42 | + <span class="theme-icon theme-icon-moon"></span> |
| 43 | + <span class="theme-icon theme-icon-sun hidden"></span> |
| 44 | + </button> |
| 45 | + </body></html>`); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + uninstallDom(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('getInitialTheme should handle stored light/dark/invalid/missing values', async () => { |
| 53 | + localStorage.setItem('grades-theme', 'light'); |
| 54 | + assert.equal(getInitialTheme(), 'light'); |
| 55 | + |
| 56 | + localStorage.setItem('grades-theme', 'dark'); |
| 57 | + assert.equal(getInitialTheme(), 'dark'); |
| 58 | + |
| 59 | + localStorage.setItem('grades-theme', 'unexpected'); |
| 60 | + assert.equal(getInitialTheme(), 'dark'); |
| 61 | + |
| 62 | + localStorage.removeItem('grades-theme'); |
| 63 | + assert.equal(getInitialTheme(), 'dark'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('applyTheme should update data-theme, icon states, aria-label and title', async () => { |
| 67 | + const root = document.documentElement; |
| 68 | + const btn = document.getElementById('themeToggleBtn'); |
| 69 | + const moon = btn.querySelector('.theme-icon-moon'); |
| 70 | + const sun = btn.querySelector('.theme-icon-sun'); |
| 71 | + |
| 72 | + applyTheme('light'); |
| 73 | + assert.equal(root.getAttribute('data-theme'), 'light'); |
| 74 | + assert.equal(sun.classList.contains('hidden'), false); |
| 75 | + assert.equal(moon.classList.contains('hidden'), true); |
| 76 | + assert.equal(btn.getAttribute('aria-label'), '切換至深色模式'); |
| 77 | + assert.equal(btn.getAttribute('title'), '切換至深色模式'); |
| 78 | + |
| 79 | + applyTheme('dark'); |
| 80 | + assert.equal(root.hasAttribute('data-theme'), false); |
| 81 | + assert.equal(sun.classList.contains('hidden'), true); |
| 82 | + assert.equal(moon.classList.contains('hidden'), false); |
| 83 | + assert.equal(btn.getAttribute('aria-label'), '切換至淺色模式'); |
| 84 | + assert.equal(btn.getAttribute('title'), '切換至淺色模式'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('setupThemeToggle should toggle theme, persist localStorage and dispatch themechange', async () => { |
| 88 | + localStorage.setItem('grades-theme', 'dark'); |
| 89 | + |
| 90 | + const events = []; |
| 91 | + document.addEventListener('themechange', (event) => events.push(event.detail.theme)); |
| 92 | + |
| 93 | + setupThemeToggle(); |
| 94 | + const btn = document.getElementById('themeToggleBtn'); |
| 95 | + |
| 96 | + btn.click(); |
| 97 | + assert.equal(document.documentElement.getAttribute('data-theme'), 'light'); |
| 98 | + assert.equal(localStorage.getItem('grades-theme'), 'light'); |
| 99 | + |
| 100 | + btn.click(); |
| 101 | + assert.equal(document.documentElement.hasAttribute('data-theme'), false); |
| 102 | + assert.equal(localStorage.getItem('grades-theme'), 'dark'); |
| 103 | + |
| 104 | + assert.deepEqual(events, ['dark', 'light', 'dark']); |
| 105 | + }); |
| 106 | + |
| 107 | + it('chart integration should re-apply chart palette on themechange', async () => { |
| 108 | + document.documentElement.style.setProperty('--color-text-main', 'rgb(1, 2, 3)'); |
| 109 | + document.documentElement.style.setProperty('--color-text-secondary', 'rgb(10, 20, 30)'); |
| 110 | + document.documentElement.style.setProperty('--color-text-muted', 'rgb(40, 50, 60)'); |
| 111 | + document.documentElement.style.setProperty('--color-surface-elevated', 'rgb(70, 80, 90)'); |
| 112 | + document.documentElement.style.setProperty('--color-border-subtle', 'rgba(1,1,1,0.3)'); |
| 113 | + document.documentElement.style.setProperty('--color-border-extra-subtle', 'rgba(2,2,2,0.2)'); |
| 114 | + |
| 115 | + const charts = await import('../../frontend/charts.js'); |
| 116 | + |
| 117 | + const radar = { |
| 118 | + options: { |
| 119 | + scales: { r: { ticks: {}, grid: {}, angleLines: {}, pointLabels: {} } }, |
| 120 | + plugins: { legend: { labels: {} } } |
| 121 | + }, |
| 122 | + data: { datasets: [{}, {}] }, |
| 123 | + updateCalls: 0, |
| 124 | + update() { this.updateCalls += 1; } |
| 125 | + }; |
| 126 | + |
| 127 | + const bar = { |
| 128 | + options: { |
| 129 | + scales: { x: { ticks: {}, grid: {} }, y: { ticks: {}, grid: {} } }, |
| 130 | + plugins: { legend: { labels: {} } } |
| 131 | + }, |
| 132 | + data: { datasets: [] }, |
| 133 | + updateCalls: 0, |
| 134 | + update() { this.updateCalls += 1; } |
| 135 | + }; |
| 136 | + |
| 137 | + charts.__setChartInstancesForTest({ radar, bar }); |
| 138 | + applyTheme('light'); |
| 139 | + |
| 140 | + assert.equal(radar.options.scales.r.grid.color, 'rgba(1,1,1,0.3)'); |
| 141 | + assert.equal(bar.options.scales.x.grid.color, 'rgba(2,2,2,0.2)'); |
| 142 | + assert.equal(radar.data.datasets[0].pointBorderColor, 'rgb(70, 80, 90)'); |
| 143 | + assert.equal(radar.updateCalls, 1); |
| 144 | + assert.equal(bar.updateCalls, 1); |
| 145 | + |
| 146 | + document.documentElement.style.setProperty('--color-border-subtle', 'rgba(9,9,9,0.9)'); |
| 147 | + document.documentElement.style.setProperty('--color-border-extra-subtle', 'rgba(8,8,8,0.8)'); |
| 148 | + document.documentElement.style.setProperty('--color-surface-elevated', 'rgb(7, 8, 9)'); |
| 149 | + |
| 150 | + applyTheme('dark'); |
| 151 | + |
| 152 | + assert.equal(radar.options.scales.r.grid.color, 'rgba(9,9,9,0.9)'); |
| 153 | + assert.equal(bar.options.scales.x.grid.color, 'rgba(8,8,8,0.8)'); |
| 154 | + assert.equal(radar.data.datasets[0].pointBorderColor, 'rgb(7, 8, 9)'); |
| 155 | + assert.equal(radar.updateCalls, 2); |
| 156 | + assert.equal(bar.updateCalls, 2); |
| 157 | + }); |
| 158 | +}); |
0 commit comments