ray-template/__test__/hooks/useTheme.spec.ts
XiaoDaiGua-Ray 403fdd589a feat!: rebuild Ray Template as a monorepo
Share workspace packages between Ray Admin and the interactive docs.

BREAKING CHANGE: v6 is a new major line. v5 stays on legacy/v5.
2026-11-04 14:01:28 +08:00

64 lines
1.5 KiB
TypeScript

import { useTheme } from '../../apps/admin/src/hooks/template/useTheme'
import { useSettingGetters } from '../../apps/admin/src/store'
import setupMiniApp from '../utils/setupMiniApp'
describe('useTheme', async () => {
await setupMiniApp()
const { darkTheme, lightTheme, toggleTheme, getAppTheme, syncSystemTheme } =
useTheme()
it('should change to dark theme', () => {
darkTheme()
expect(getAppTheme().theme).toBe(true)
})
it('should change to light theme', () => {
lightTheme()
expect(getAppTheme().theme).toBe(false)
})
it('should toggle theme', () => {
lightTheme()
expect(getAppTheme().theme).toBe(false)
toggleTheme()
expect(getAppTheme().theme).toBe(true)
})
it('should return current theme', () => {
darkTheme()
const { theme: _darkTheme, themeLabel: _darkThemeLabel } = getAppTheme()
expect(_darkTheme).toBe(true)
expect(_darkThemeLabel).toBe('Dark')
lightTheme()
const { theme: __lightTheme, themeLabel: __lightThemeLabel } = getAppTheme()
expect(__lightTheme).toBe(false)
expect(__lightThemeLabel).toBe('Light')
})
it('selects auto mode and applies the system color preference', () => {
const spy = vi
.spyOn(window, 'matchMedia')
.mockReturnValue({ matches: true } as MediaQueryList)
try {
syncSystemTheme()
expect(getAppTheme()).toMatchObject({ theme: true, themeLabel: 'Dark' })
expect(useSettingGetters().getAppThemeStr.value).toBe('auto')
} finally {
lightTheme()
spy.mockRestore()
}
})
})