-
-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy pathuse-colors.spec.ts
More file actions
141 lines (121 loc) · 4.4 KB
/
Copy pathuse-colors.spec.ts
File metadata and controls
141 lines (121 loc) · 4.4 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { computed, nextTick, shallowRef, ref, type Ref } from 'vue'
import { useColors } from '~/composables/useColors'
const useSupportedMock = vi.hoisted(() => vi.fn())
const useMutationObserverMock = vi.hoisted(() => vi.fn())
const useResizeObserverMock = vi.hoisted(() => vi.fn())
const vueUseMockState = vi.hoisted(() => ({
preferredDark: undefined as unknown as Ref<boolean>,
}))
vi.mock('@vueuse/core', () => {
vueUseMockState.preferredDark = ref(false)
return {
useSupported: (callback: () => boolean) => {
useSupportedMock(callback)
return computed(() => callback())
},
useMutationObserver: useMutationObserverMock,
useResizeObserver: useResizeObserverMock,
usePreferredDark: () => vueUseMockState.preferredDark,
}
})
describe('useColors', () => {
beforeEach(() => {
vi.clearAllMocks()
vueUseMockState.preferredDark.value = false
})
afterEach(() => {
vi.unstubAllGlobals()
})
it('does not attach an html mutation observer when client is not supported', () => {
vi.stubGlobal('window', undefined)
const elementReference = shallowRef<HTMLElement | null>(null)
useColors(elementReference, { watchHtmlAttributes: true })
expect(useMutationObserverMock).not.toHaveBeenCalled()
})
it('attaches a resize observer when enabled', () => {
const elementReference = shallowRef<HTMLElement | null>(null)
useColors(elementReference, { watchResize: true })
expect(useResizeObserverMock).toHaveBeenCalledTimes(1)
expect(useResizeObserverMock).toHaveBeenCalledWith(expect.any(Object), expect.any(Function))
const resizeCallback = useResizeObserverMock.mock.calls?.[0]?.[1]
expect(resizeCallback).toBeDefined()
expect(() => resizeCallback()).not.toThrow()
})
it('does not attach observers by default', () => {
const elementReference = shallowRef<HTMLElement | null>(null)
useColors(elementReference)
expect(useMutationObserverMock).not.toHaveBeenCalled()
expect(useResizeObserverMock).not.toHaveBeenCalled()
})
it('returns an empty color object when window or document is unavailable', () => {
vi.stubGlobal('window', undefined)
vi.stubGlobal('document', undefined)
const elementReference = shallowRef<HTMLElement | null>(null)
const { colors } = useColors(elementReference)
expect(colors.value).toEqual({})
})
it('recomputes colors when preferred dark mode changes', async () => {
const styleValues = {
accent: '#FF0000',
}
vi.stubGlobal('window', {})
vi.stubGlobal('document', {
documentElement: {},
})
vi.stubGlobal('getComputedStyle', () => ({
getPropertyValue: (variableName: string) => {
if (variableName === '--accent') {
return styleValues.accent
}
return ''
},
}))
const elementReference = shallowRef<HTMLElement | null>({} as HTMLElement)
const { colors } = useColors(elementReference)
expect(colors.value.accent).toBe('#FF0000')
styleValues.accent = '#00FF00'
vueUseMockState.preferredDark.value = true
await nextTick()
expect(colors.value.accent).toBe('#00FF00')
})
it('attaches an html mutation observer when enabled', () => {
vi.stubGlobal('window', {})
vi.stubGlobal('document', {
documentElement: {},
})
const elementReference = shallowRef<HTMLElement | null>(null)
useColors(elementReference, {
watchHtmlAttributes: true,
})
expect(useMutationObserverMock).toHaveBeenCalledTimes(1)
expect(useMutationObserverMock).toHaveBeenCalledWith(
document.documentElement,
expect.any(Function),
{
attributes: true,
attributeFilter: ['class', 'style', 'data-theme', 'data-bg-theme'],
},
)
})
it('falls back to document element when no element is provided', () => {
vi.stubGlobal('window', {})
vi.stubGlobal('document', {
documentElement: {},
})
vi.stubGlobal('getComputedStyle', (element: HTMLElement) => {
expect(element).toBe(document.documentElement)
return {
getPropertyValue: (variableName: string) => {
if (variableName === '--accent') {
return 'red'
}
return ''
},
}
})
const elementReference = shallowRef<HTMLElement | null>(null)
const { colors } = useColors(elementReference)
expect(colors.value.accent).toBe('red')
})
})