返回 slidev
dark.test.ts
根目录 / packages / client / logic / dark.test.ts
1 import type { SlidevConfig } from '@slidev/types'
2 import type { Ref } from 'vue'
3 import { beforeEach, describe, expect, it, vi } from 'vitest'
4 import { isColorSchemaConfigured, isDark, toggleDark } from './dark'
5
6 const mocks = vi.hoisted(() => ({
7 configs: undefined as Pick<SlidevConfig, 'colorSchema'> | undefined,
8 preferredDark: undefined as Ref<boolean> | undefined,
9 store: undefined as Ref<string> | undefined,
10 }))
11
12 vi.mock('../env', async () => {
13 const { reactive } = await import('vue')
14 const configs = reactive({ colorSchema: 'auto' as SlidevConfig['colorSchema'] })
15 mocks.configs = configs
16 return { configs }
17 })
18
19 vi.mock('@vueuse/core', async () => {
20 const { ref } = await import('vue')
21 mocks.preferredDark = ref(false)
22 mocks.store = ref('auto')
23 return {
24 isClient: false,
25 useLocalStorage: () => mocks.store,
26 usePreferredDark: () => mocks.preferredDark,
27 useToggle: (target: Ref<boolean>) => () => target.value = !target.value,
28 }
29 })
30
31 describe('dark mode configuration', () => {
32 beforeEach(() => {
33 mocks.configs!.colorSchema = 'auto'
34 mocks.preferredDark!.value = false
35 mocks.store!.value = 'auto'
36 })
37
38 it.each([
39 ['dark light', true],
40 ['light dark', false],
41 ] as const)('uses the first scheme as the default for "%s"', (colorSchema, expected) => {
42 mocks.configs!.colorSchema = colorSchema
43
44 expect(isDark.value).toBe(expected)
45 expect(isColorSchemaConfigured.value).toBe(false)
46 })
47
48 it.each([
49 ['dark light', 'light', false],
50 ['light dark', 'dark', true],
51 ] as const)('uses a saved manual preference for "%s"', (colorSchema, savedPreference, expected) => {
52 mocks.configs!.colorSchema = colorSchema
53 mocks.store!.value = savedPreference
54
55 expect(isDark.value).toBe(expected)
56 })
57
58 it.each([
59 ['dark light', false, 'light'],
60 ['light dark', true, 'dark'],
61 ] as const)('persists manual changes for "%s"', (colorSchema, selection, storedValue) => {
62 mocks.configs!.colorSchema = colorSchema
63
64 isDark.value = selection
65
66 expect(mocks.store!.value).toBe(storedValue)
67 expect(isDark.value).toBe(selection)
68 })
69
70 it('uses the system preference for auto and preserves its existing storage behavior', () => {
71 mocks.preferredDark!.value = true
72 expect(isDark.value).toBe(true)
73
74 isDark.value = false
75 expect(mocks.store!.value).toBe('light')
76 expect(isDark.value).toBe(false)
77
78 isDark.value = true
79 expect(mocks.store!.value).toBe('auto')
80 expect(isDark.value).toBe(true)
81 })
82
83 it.each([
84 ['dark', true],
85 ['light', false],
86 ] as const)('keeps "%s" forced and non-switchable', (colorSchema, expected) => {
87 mocks.configs!.colorSchema = colorSchema
88
89 expect(isColorSchemaConfigured.value).toBe(true)
90 expect(isDark.value).toBe(expected)
91
92 toggleDark()
93
94 expect(isDark.value).toBe(expected)
95 expect(mocks.store!.value).toBe('auto')
96 })
97 })
98
98 lines TYPESCRIPT