button-close.spec.js
4.92 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { mount } from '@vue/test-utils'
import { BButtonClose } from './button-close'
describe('button-close', () => {
it('has root element "button"', async () => {
const wrapper = mount(BButtonClose)
expect(wrapper.element.tagName).toBe('BUTTON')
wrapper.destroy()
})
it('has class "close"', async () => {
const wrapper = mount(BButtonClose)
expect(wrapper.classes()).toContain('close')
expect(wrapper.classes().length).toBe(1)
wrapper.destroy()
})
it('has attribute type="button"', async () => {
const wrapper = mount(BButtonClose)
expect(wrapper.attributes('type')).toBe('button')
wrapper.destroy()
})
it('does not have attribute "disabled" by default', async () => {
const wrapper = mount(BButtonClose)
expect(wrapper.attributes('disabled')).toBeUndefined()
wrapper.destroy()
})
it('has attribute "disabled" when prop "disabled" is set', async () => {
const wrapper = mount(BButtonClose, {
context: {
props: { disabled: true }
}
})
expect(wrapper.attributes('disabled')).toBeDefined()
wrapper.destroy()
})
it('has attribute aria-label="Close" by default', async () => {
const wrapper = mount(BButtonClose)
expect(wrapper.attributes('aria-label')).toBe('Close')
wrapper.destroy()
})
it('has custom attribute "aria-label" when prop "aria-label" set', async () => {
const wrapper = mount(BButtonClose, {
context: {
props: { ariaLabel: 'foobar' }
}
})
expect(wrapper.attributes('aria-label')).toBe('foobar')
wrapper.destroy()
})
it('has text variant class when "variant" prop set', async () => {
const wrapper = mount(BButtonClose, {
context: {
props: { textVariant: 'primary' }
}
})
expect(wrapper.classes()).toContain('close')
expect(wrapper.classes()).toContain('text-primary')
expect(wrapper.classes().length).toBe(2)
wrapper.destroy()
})
it('should have default content', async () => {
const wrapper = mount(BButtonClose)
// '×' gets converted to '×'
expect(wrapper.text()).toContain('×')
wrapper.destroy()
})
it('should have custom content from "content" prop', async () => {
const wrapper = mount(BButtonClose, {
context: {
props: { content: 'Close' }
}
})
expect(wrapper.text()).toContain('Close')
wrapper.destroy()
})
it('should have custom content from default slot', async () => {
const wrapper = mount(BButtonClose, {
slots: {
default: '<i>foobar</i>'
}
})
expect(wrapper.text()).toContain('foobar')
wrapper.destroy()
})
it('should emit "click" event when clicked', async () => {
let event = null
const spy1 = jest.fn(e => {
event = e
})
const wrapper = mount(BButtonClose, {
context: {
on: { click: spy1 }
},
slots: {
default: '<i>some <span>text</span></i>'
}
})
expect(spy1).not.toHaveBeenCalled()
const btn = wrapper.find('button')
expect(btn).toBeDefined()
await btn.trigger('click')
expect(spy1).toHaveBeenCalled()
expect(spy1.mock.calls.length).toBe(1)
expect(event).toBeInstanceOf(MouseEvent)
// Works when clicking on an inner element
const span = wrapper.find('span')
expect(span).toBeDefined()
await span.trigger('click')
expect(spy1.mock.calls.length).toBe(2)
wrapper.destroy()
})
it('should not emit "click" event when disabled and clicked', async () => {
const spy1 = jest.fn()
const wrapper = mount(BButtonClose, {
context: {
props: {
disabled: true
},
on: { click: spy1 }
},
slots: {
default: '<i>some <span>text</span></i>'
}
})
expect(spy1).not.toHaveBeenCalled()
const btn = wrapper.find('button')
expect(btn).toBeDefined()
await btn.trigger('click')
expect(spy1).not.toHaveBeenCalled()
// For some reason, JSDOM emits a click on button when clicking inner element
// Although testing in docs, this click is not emitted when disabled
// Appears to be a bug in JSDOM
//
// // Does not emit click on inner element clicks
// const span = wrapper.find('span')
// expect(span).toBeDefined()
// await span.trigger('click')
//
// expect(spy1).not.toHaveBeenCalled()
wrapper.destroy()
})
it('handles multiple click listeners', async () => {
const spy1 = jest.fn()
const spy2 = jest.fn()
const wrapper = mount(BButtonClose, {
context: {
on: { click: [spy1, spy2] }
}
})
expect(spy1).not.toHaveBeenCalled()
expect(spy2).not.toHaveBeenCalled()
const btn = wrapper.find('button')
expect(btn).toBeDefined()
await btn.trigger('click')
expect(spy1).toHaveBeenCalled()
expect(spy2).toHaveBeenCalled()
expect(spy1.mock.calls.length).toBe(1)
expect(spy2.mock.calls.length).toBe(1)
wrapper.destroy()
})
})