Skip to content

Commit bce3867

Browse files
committed
Fix fire-event tests
1 parent 82ba9c6 commit bce3867

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/__tests__/fire-event.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {h} from 'vue'
12
import {render, fireEvent} from '@testing-library/vue'
23
import Button from './components/Button'
34

@@ -120,6 +121,8 @@ const eventTypes = [
120121
},
121122
]
122123

124+
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)
125+
123126
// For each event type, we assert that the right events are being triggered
124127
// when the associated fireEvent method is called.
125128
eventTypes.forEach(({type, events, elementType = 'input', init}) => {
@@ -128,21 +131,20 @@ eventTypes.forEach(({type, events, elementType = 'input', init}) => {
128131
it(`triggers ${eventName}`, async () => {
129132
const testId = `${type}-${eventName}`
130133
const spy = jest.fn()
134+
const eventNameHandler = `on${capitalize(eventName)}`
131135

132-
// Render an element with a listener of the event under testing and a
133-
// test-id attribute, so that we can get the DOM node afterwards.
134-
const {getByTestId} = render({
135-
render(h) {
136+
const componentWithEvent = {
137+
render() {
136138
return h(elementType, {
137-
on: {
138-
[eventName.toLowerCase()]: spy,
139-
},
140-
attrs: {
141-
'data-testid': testId,
142-
},
139+
[eventNameHandler]: spy,
140+
'data-testid': testId,
143141
})
144142
},
145-
})
143+
}
144+
145+
// Render an element with a listener of the event under testing and a
146+
// test-id attribute, so that we can get the DOM node afterwards.
147+
const {getByTestId} = render(componentWithEvent)
146148

147149
const elem = getByTestId(testId)
148150

@@ -157,13 +159,13 @@ eventTypes.forEach(({type, events, elementType = 'input', init}) => {
157159
test('triggers dblclick on doubleClick', async () => {
158160
const spy = jest.fn()
159161

160-
const {getByRole} = render({
161-
render(h) {
162-
return h('button', {
163-
on: {dblclick: spy},
164-
})
162+
const componentWithDblClick = {
163+
render() {
164+
return h('button', {onDblClick: spy}, 'Click me')
165165
},
166-
})
166+
}
167+
168+
const {getByRole} = render(componentWithDblClick)
167169

168170
const elem = getByRole('button')
169171

0 commit comments

Comments
 (0)