forked from bootstrap-vue/bootstrap-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten-on-document.spec.js
117 lines (101 loc) · 3.16 KB
/
listen-on-document.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
116
117
import { mount } from '@vue/test-utils'
import { listenOnDocumentMixin } from './listen-on-document'
describe('mixins/listen-on-document', () => {
it('works', async () => {
const spyClick1 = jest.fn()
const spyClick2 = jest.fn()
const spyFocusin = jest.fn()
const TestComponent = {
mixins: [listenOnDocumentMixin],
props: {
offClickOne: {
type: Boolean,
default: false
}
},
mounted() {
this.listenOnDocument('click', spyClick1)
this.listenOnDocument('focusin', spyFocusin)
this.listenOnDocument('click', spyClick2)
},
watch: {
offClickOne(newValue) {
if (newValue) {
this.listenOffDocument('click', spyClick1)
}
}
},
render(h) {
return h('div', this.$slots.default)
}
}
const App = {
components: { TestComponent },
props: {
offClickOne: {
type: Boolean,
default: false
},
destroy: {
type: Boolean,
default: false
}
},
render(h) {
const props = {
offClickOne: this.offClickOne
}
return h('div', [
h('span', ''),
h('input', { type: 'text' }),
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(spyClick1).not.toHaveBeenCalled()
expect(spyClick2).not.toHaveBeenCalled()
expect(spyFocusin).not.toHaveBeenCalled()
const $span = wrapper.find('span')
expect($span.exists()).toBe(true)
const $input = wrapper.find('input')
expect($input.exists()).toBe(true)
await $input.trigger('focusin')
expect(spyClick1).not.toHaveBeenCalled()
expect(spyClick2).not.toHaveBeenCalled()
expect(spyFocusin).toHaveBeenCalledTimes(1)
await $span.trigger('click')
expect(spyClick1).toHaveBeenCalledTimes(1)
expect(spyClick2).toHaveBeenCalledTimes(1)
expect(spyFocusin).toHaveBeenCalledTimes(1)
await wrapper.setProps({ offClickOne: true })
await $span.trigger('click')
expect(spyClick1).toHaveBeenCalledTimes(1)
expect(spyClick2).toHaveBeenCalledTimes(2)
expect(spyFocusin).toHaveBeenCalledTimes(1)
await $input.trigger('focusin')
expect(spyClick1).toHaveBeenCalledTimes(1)
expect(spyClick2).toHaveBeenCalledTimes(2)
expect(spyFocusin).toHaveBeenCalledTimes(2)
await wrapper.setProps({ destroy: true })
expect(spyClick1).toHaveBeenCalledTimes(1)
expect(spyClick2).toHaveBeenCalledTimes(2)
expect(spyFocusin).toHaveBeenCalledTimes(2)
await $input.trigger('focusin')
expect(spyClick1).toHaveBeenCalledTimes(1)
expect(spyClick2).toHaveBeenCalledTimes(2)
expect(spyFocusin).toHaveBeenCalledTimes(2)
await $span.trigger('click')
expect(spyClick1).toHaveBeenCalledTimes(1)
expect(spyClick2).toHaveBeenCalledTimes(2)
expect(spyFocusin).toHaveBeenCalledTimes(2)
wrapper.destroy()
})
})