Skip to content

Commit 6332fec

Browse files
committed
Replace Simulate with fireEvent
1 parent 43bd04a commit 6332fec

File tree

10 files changed

+32
-56
lines changed

10 files changed

+32
-56
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ The `vue-testing-library` is a an adapter that enables Vue testing using the fra
2121
* [Installation](#installation)
2222
* [Usage](#usage)
2323
* [`render`](#render)
24-
* [`Simulate`](#simulate)
24+
* [`fireEvent`](#fireEvent)
2525
* [`wait`](#wait)
26-
* [Examples](#examples)
26+
* [Examples](#examples)
2727
* [LICENSE](#license)
2828

2929
## Installation
@@ -120,9 +120,9 @@ The `render` function takes up to 3 parameters and returns an object with some h
120120
* routes - A set of routes, if present render will configure VueRouter and pass to mount.
121121
3. configurationCb - A callback to be called passing the Vue instance when created. This allows 3rd party plugins to be installed prior to mount.
122122

123-
### Simulate
123+
### fireEvent
124124

125-
Lightweight wrapper around DOM element methods
125+
Lightweight wrapper around DOM element events and methods
126126

127127
### wait
128128

src/Simulate.js

-29
This file was deleted.

src/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import {
22
createLocalVue,
33
mount
44
} from '@vue/test-utils'
5-
import Simulate from './Simulate'
5+
66
import {
77
getQueriesForElement,
88
prettyDOM,
9-
wait
9+
wait,
10+
fireEvent
1011
} from 'dom-testing-library'
1112

1213
const mountedWrappers = new Set()
@@ -74,9 +75,13 @@ function cleanupAtWrapper (wrapper) {
7475
mountedWrappers.delete(wrapper)
7576
}
7677

78+
fireEvent.touch = (elem) => {
79+
fireEvent.focus(elem)
80+
fireEvent.blur(elem)
81+
}
82+
7783
export * from 'dom-testing-library'
7884
export {
7985
cleanup,
80-
render,
81-
Simulate
86+
render
8287
}

tests/__tests__/components/EndToEnd.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<script>
1313
const fetchAMessage = () =>
1414
new Promise(resolve => {
15-
// we are using random timeout here to simulate a real-time example
15+
// we are using random timeout here to fireEvent a real-time example
1616
// of an async operation calling a callback at a non-deterministic time
1717
const randomTimeout = Math.floor(Math.random() * 100)
1818
setTimeout(() => {

tests/__tests__/fetch.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axiosMock from 'axios'
2-
import { render, Simulate, wait } from '../../src'
2+
import { render, fireEvent, wait } from '../../src'
33
import Fetch from './components/Fetch.vue'
44

55
test('Fetch makes an API call and displays the greeting when load-greeting is clicked', async () => {
@@ -12,7 +12,7 @@ test('Fetch makes an API call and displays the greeting when load-greeting is cl
1212
const { html, getByText } = render(Fetch, { props: { url: '/greeting' } })
1313

1414
// Act
15-
Simulate.click(getByText('Fetch'))
15+
fireEvent.click(getByText('Fetch'))
1616

1717
await wait()
1818

tests/__tests__/form.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, Simulate } from '../../src'
1+
import { render, fireEvent } from '../../src'
22
import Login from './components/Login'
33

44
test('login form submits', async () => {
@@ -16,9 +16,9 @@ test('login form submits', async () => {
1616

1717
// NOTE: in jsdom, it's not possible to trigger a form submission
1818
// by clicking on the submit button. This is really unfortunate.
19-
// So the next best thing is to simulate a submit on the form itself
19+
// So the next best thing is to fireEvent a submit on the form itself
2020
// then ensure that there's a submit button.
21-
Simulate.click(submitButtonNode)
21+
fireEvent.click(submitButtonNode)
2222

2323
// Assert
2424
expect(handleSubmit).toHaveBeenCalledTimes(1)

tests/__tests__/stopwatch.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import StopWatch from './components/StopWatch.vue'
2-
import { render, wait, Simulate } from '../../src'
2+
import { render, wait, fireEvent } from '../../src'
33

44
test('unmounts a component', async () => {
55
jest.spyOn(console, 'error').mockImplementation(() => {})
66

77
const { unmount, isUnmounted, getByText } = render(StopWatch)
8-
Simulate.click(getByText('Start'))
8+
fireEvent.click(getByText('Start'))
99

1010
await wait()
1111

@@ -24,9 +24,9 @@ test('updates component state', async () => {
2424

2525
expect(elapsedTime.textContent).toBe('0ms')
2626

27-
Simulate.click(startButton)
27+
fireEvent.click(startButton)
2828
await wait()
29-
Simulate.click(startButton)
29+
fireEvent.click(startButton)
3030

3131
expect(elapsedTime.textContent).not.toBe('0ms')
3232
})

tests/__tests__/validate-plugin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import VeeValidate from 'vee-validate'
22

3-
import { render, Simulate, wait } from '../../src'
3+
import { render, fireEvent, wait } from '../../src'
44
import Validate from './components/Validate'
55

66
test('can validate using plugin', async () => {
77
const { getByPlaceholderText, queryByTestId } = render(Validate, {},
88
vue => vue.use(VeeValidate, { events: 'blur' }))
99

1010
const usernameInput = getByPlaceholderText('Username...')
11-
Simulate.touch(usernameInput)
11+
fireEvent.touch(usernameInput)
1212

1313
await wait()
1414

tests/__tests__/vue-router.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import App from './components/Router/App.vue'
22
import Home from './components/Router/Home.vue'
33
import About from './components/Router/About.vue'
44

5-
import { render, Simulate, wait } from '../../src'
5+
import { render, fireEvent, wait } from '../../src'
66

77
const routes = [
88
{ path: '/', component: Home },
@@ -14,7 +14,7 @@ test('full app rendering/navigating', async () => {
1414
const { queryByTestId } = render(App, { routes })
1515
// normally I'd use a data-testid, but just wanted to show this is also possible
1616
expect(queryByTestId('location-display').textContent).toBe('/')
17-
Simulate.click(queryByTestId('about-link'))
17+
fireEvent.click(queryByTestId('about-link'))
1818
await wait()
1919
// normally I'd use a data-testid, but just wanted to show this is also possible
2020
expect(queryByTestId('location-display').textContent).toBe('/about')

tests/__tests__/vuex.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import VuexTest from './components/VuexTest'
2-
import { render, Simulate, wait } from '../../src'
2+
import { render, fireEvent, wait } from '../../src'
33

44
const store = {
55
state: {
@@ -16,15 +16,15 @@ const store = {
1616

1717
test('can render with vuex with defaults', async () => {
1818
const { getByTestId, getByText } = render(VuexTest, { store })
19-
Simulate.click(getByText('+'))
19+
fireEvent.click(getByText('+'))
2020
await wait()
2121
expect(getByTestId('count-value').textContent).toBe('1')
2222
})
2323

2424
test('can render with vuex with custom initial state', async () => {
2525
store.state.count = 3
2626
const { getByTestId, getByText } = render(VuexTest, { store })
27-
Simulate.click(getByText('-'))
27+
fireEvent.click(getByText('-'))
2828
await wait()
2929
expect(getByTestId('count-value').textContent).toBe('2')
3030
})
@@ -35,10 +35,10 @@ test('can render with vuex with custom store', async () => {
3535

3636
const store = { state: { count: 1000 } }
3737
const { getByTestId, getByText } = render(VuexTest, { store })
38-
Simulate.click(getByText('+'))
38+
fireEvent.click(getByText('+'))
3939
await wait()
4040
expect(getByTestId('count-value').textContent).toBe('1000')
41-
Simulate.click(getByText('-'))
41+
fireEvent.click(getByText('-'))
4242
await wait()
4343
expect(getByTestId('count-value').textContent).toBe('1000')
4444

0 commit comments

Comments
 (0)