-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathuser-event.js
75 lines (59 loc) · 2.33 KB
/
user-event.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
import '@testing-library/jest-dom'
import userEvent from '@testing-library/user-event'
import {render, waitFor} from '..'
import Form from './components/Form'
import Select from './components/Select'
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {})
})
afterEach(() => {
console.warn.mockRestore()
})
test('User events in a form', async () => {
const fakeReview = {
title: 'An Awesome Movie',
review: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
rating: '3',
}
const user = userEvent.setup()
const {getByText, getByLabelText, emitted} = render(Form)
const submitButton = getByText('Submit')
expect(submitButton).toBeDisabled()
const titleInput = getByLabelText(/title of the movie/i)
await user.type(titleInput, fakeReview.title)
expect(titleInput).toHaveValue(fakeReview.title)
const textArea = getByLabelText(/Your review/i)
await user.type(textArea, 'The t-rex went insane!')
expect(textArea).toHaveValue('The t-rex went insane!')
await user.clear(textArea)
expect(textArea).toHaveValue('')
await user.type(textArea, fakeReview.review)
expect(textArea).toHaveValue(fakeReview.review)
const initialSelectedRating = getByLabelText(/Awful/i)
const wonderfulRadioInput = getByLabelText(/Wonderful/i)
expect(initialSelectedRating).toBeChecked()
expect(wonderfulRadioInput).not.toBeChecked()
await user.click(wonderfulRadioInput)
expect(wonderfulRadioInput).toBeChecked()
await waitFor(() => expect(initialSelectedRating).not.toBeChecked())
const recommendInput = getByLabelText(/Would you recommend this movie?/i)
await user.click(recommendInput)
expect(recommendInput).toBeChecked()
await user.tab()
expect(submitButton).toHaveFocus()
expect(submitButton).toBeEnabled()
await user.type(submitButton, '{enter}')
expect(emitted('submit')[0][0]).toMatchObject(fakeReview)
expect(console.warn).not.toHaveBeenCalled()
})
test('selecting option with user events', async () => {
const user = userEvent.setup()
const {getByDisplayValue} = render(Select)
const select = getByDisplayValue('Tyrannosaurus')
expect(select).toHaveValue('dino1')
await user.selectOptions(select, 'dino2')
expect(select).toHaveValue('dino2')
await user.selectOptions(select, 'dino3')
expect(select).not.toHaveValue('dino2')
expect(select).toHaveValue('dino3')
})