forked from bootstrap-vue/bootstrap-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten-on-window.spec.js
115 lines (93 loc) · 3 KB
/
listen-on-window.spec.js
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
import { mount } from '@vue/test-utils'
import { listenOnWindowMixin } from './listen-on-window'
describe('mixins/listen-on-window', () => {
it('works', async () => {
const spyResize1 = jest.fn()
const spyResize2 = jest.fn()
const spyScroll = jest.fn()
const TestComponent = {
mixins: [listenOnWindowMixin],
props: {
offResizeOne: {
type: Boolean,
default: false
}
},
mounted() {
this.listenOnWindow('resize', spyResize1)
this.listenOnWindow('scroll', spyScroll)
this.listenOnWindow('resize', spyResize2)
},
watch: {
offResizeOne(newValue) {
if (newValue) {
this.listenOffWindow('resize', spyResize1)
}
}
},
render(h) {
return h('div', this.$slots.default)
}
}
const App = {
components: { TestComponent },
props: {
offResizeOne: {
type: Boolean,
default: false
},
destroy: {
type: Boolean,
default: false
}
},
render(h) {
const props = {
offResizeOne: this.offResizeOne
}
return h('div', [this.destroy ? h() : h(TestComponent, { props }, 'test-component')])
}
}
const wrapper = mount(App, {
attachTo: document.body,
propsData: {
destroy: false
}
})
expect(wrapper.vm).toBeDefined()
expect(wrapper.text()).toEqual('test-component')
expect(spyResize1).not.toHaveBeenCalled()
expect(spyResize2).not.toHaveBeenCalled()
expect(spyScroll).not.toHaveBeenCalled()
window.dispatchEvent(new Event('scroll'))
expect(spyResize1).not.toHaveBeenCalled()
expect(spyResize2).not.toHaveBeenCalled()
expect(spyScroll).toHaveBeenCalledTimes(1)
window.dispatchEvent(new Event('resize'))
expect(spyResize1).toHaveBeenCalledTimes(1)
expect(spyResize2).toHaveBeenCalledTimes(1)
expect(spyScroll).toHaveBeenCalledTimes(1)
await wrapper.setProps({ offResizeOne: true })
window.dispatchEvent(new Event('resize'))
expect(spyResize1).toHaveBeenCalledTimes(1)
expect(spyResize2).toHaveBeenCalledTimes(2)
expect(spyScroll).toHaveBeenCalledTimes(1)
window.dispatchEvent(new Event('scroll'))
expect(spyResize1).toHaveBeenCalledTimes(1)
expect(spyResize2).toHaveBeenCalledTimes(2)
expect(spyScroll).toHaveBeenCalledTimes(2)
await wrapper.setProps({ destroy: true })
expect(spyResize1).toHaveBeenCalledTimes(1)
expect(spyResize2).toHaveBeenCalledTimes(2)
expect(spyScroll).toHaveBeenCalledTimes(2)
window.dispatchEvent(new Event('scroll'))
expect(spyResize1).toHaveBeenCalledTimes(1)
expect(spyResize2).toHaveBeenCalledTimes(2)
expect(spyScroll).toHaveBeenCalledTimes(2)
window.dispatchEvent(new Event('resize'))
expect(spyResize1).toHaveBeenCalledTimes(1)
expect(spyResize2).toHaveBeenCalledTimes(2)
expect(spyScroll).toHaveBeenCalledTimes(2)
wrapper.destroy()
})
})