-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.test.ts
49 lines (35 loc) · 1.32 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
import { nextTick } from 'vue'
import { useWindowSize } from '.'
describe('useWindowSize', () => {
const addEventListenerSpy = vitest.spyOn(window, 'addEventListener')
beforeEach(() => {
addEventListenerSpy.mockReset()
})
afterAll(() => {
addEventListenerSpy.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('sets handler for window "resize" event', async () => {
useWindowSize({ initialWidth: 100, initialHeight: 200 })
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 "orientationchange" event', async () => {
useWindowSize({ initialWidth: 100, initialHeight: 200, listenOrientation: true })
await nextTick()
expect(addEventListenerSpy).toHaveBeenCalledTimes(2)
const call = addEventListenerSpy.mock.calls[1] as any
expect(call[0]).toEqual('orientationchange')
expect(call[2]).toEqual({ passive: true })
})
})