forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseTheme.test.ts
More file actions
109 lines (87 loc) · 3.22 KB
/
Copy pathuseTheme.test.ts
File metadata and controls
109 lines (87 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { renderHook, act } from '@testing-library/react'
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { useTheme } from './useTheme'
function createMockMatchMedia(initialMatches: boolean) {
let matches = initialMatches
const listeners = new Set<(e: MediaQueryListEvent) => void>()
const mql = {
get matches() {
return matches
},
media: '(prefers-color-scheme: dark)',
onchange: null,
addEventListener: (_: 'change', handler: (e: MediaQueryListEvent) => void) => {
listeners.add(handler)
},
removeEventListener: (_: 'change', handler: (e: MediaQueryListEvent) => void) => {
listeners.delete(handler)
},
addListener: () => {},
removeListener: () => {},
dispatchEvent: () => false,
}
return {
matchMedia: () => mql as unknown as MediaQueryList,
fireChange: (nextMatches: boolean) => {
matches = nextMatches
const event = { matches: nextMatches } as MediaQueryListEvent
listeners.forEach((handler) => handler(event))
},
}
}
describe('useTheme', () => {
const originalMatchMedia = window.matchMedia
beforeEach(() => {
localStorage.clear()
document.documentElement.classList.remove('dark')
})
afterEach(() => {
window.matchMedia = originalMatchMedia
localStorage.clear()
document.documentElement.classList.remove('dark')
})
it('reacts live to an OS theme change while theme is "system", with no change to theme itself', () => {
const mock = createMockMatchMedia(false)
window.matchMedia = mock.matchMedia
const { result } = renderHook(() => useTheme())
expect(result.current.theme).toBe('system')
expect(document.documentElement.classList.contains('dark')).toBe(false)
act(() => {
mock.fireChange(true)
})
expect(document.documentElement.classList.contains('dark')).toBe(true)
expect(result.current.theme).toBe('system')
})
it('toggles the dark class back off when the OS switches back to light', () => {
const mock = createMockMatchMedia(true)
window.matchMedia = mock.matchMedia
renderHook(() => useTheme())
expect(document.documentElement.classList.contains('dark')).toBe(true)
act(() => {
mock.fireChange(false)
})
expect(document.documentElement.classList.contains('dark')).toBe(false)
})
it('does not react to OS theme changes when explicitly set to "light"', () => {
localStorage.setItem('ss-theme', JSON.stringify('light'))
const mock = createMockMatchMedia(false)
window.matchMedia = mock.matchMedia
renderHook(() => useTheme())
expect(document.documentElement.classList.contains('dark')).toBe(false)
act(() => {
mock.fireChange(true)
})
expect(document.documentElement.classList.contains('dark')).toBe(false)
})
it('does not react to OS theme changes when explicitly set to "dark"', () => {
localStorage.setItem('ss-theme', JSON.stringify('dark'))
const mock = createMockMatchMedia(true)
window.matchMedia = mock.matchMedia
renderHook(() => useTheme())
expect(document.documentElement.classList.contains('dark')).toBe(true)
act(() => {
mock.fireChange(false)
})
expect(document.documentElement.classList.contains('dark')).toBe(true)
})
})