-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.test.ts
69 lines (53 loc) · 2.07 KB
/
index.test.ts
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
import { nextTick } from 'vue'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { useWindowSize } from '.'
describe('useWindowSize', () => {
const addEventListenerSpy = vi.spyOn(window, 'addEventListener')
const matchMediaSpy = vi.spyOn(window, 'matchMedia').mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
}))
beforeEach(() => {
addEventListenerSpy.mockClear()
matchMediaSpy.mockClear()
})
afterAll(() => {
addEventListenerSpy.mockRestore()
matchMediaSpy.mockRestore()
})
it('should be defined', () => {
expect(useWindowSize).toBeDefined()
})
it('should work', () => {
const { width, height } = useWindowSize({ initialWidth: 100, initialHeight: 200 })
expect(width.value).toBe(window.innerWidth)
expect(height.value).toBe(window.innerHeight)
})
it('should exclude scrollbar', () => {
const { width, height } = useWindowSize({ initialWidth: 100, initialHeight: 200, includeScrollbar: false })
expect(width.value).toBe(window.document.documentElement.clientWidth)
expect(height.value).toBe(window.document.documentElement.clientHeight)
})
it('sets handler for window "resize" event', async () => {
useWindowSize({ initialWidth: 100, initialHeight: 200, listenOrientation: false })
await nextTick()
expect(addEventListenerSpy).toHaveBeenCalledOnce()
const call = addEventListenerSpy.mock.calls[0] as any
expect(call[0]).toEqual('resize')
expect(call[2]).toEqual({ passive: true })
})
it('sets handler for window.matchMedia("(orientation: portrait)") change event', async () => {
useWindowSize({ initialWidth: 100, initialHeight: 200 })
await nextTick()
expect(addEventListenerSpy).toHaveBeenCalledTimes(1)
expect(matchMediaSpy).toHaveBeenCalledTimes(1)
const call = matchMediaSpy.mock.calls[0] as any
expect(call[0]).toEqual('(orientation: portrait)')
})
})