|
1 | 1 | import { render, fireEvent } from '@testing-library/vue'
|
2 |
| -import Login from './components/Login' |
| 2 | +import 'jest-dom/extend-expect' |
| 3 | +import Form from './components/Form' |
3 | 4 |
|
4 |
| -test('login form submits', async () => { |
5 |
| - const fakeUser = { username: 'jackiechan', password: 'hiya! 🥋', rememberMe: true } |
6 |
| - const handleSubmit = jest.fn() |
7 |
| - const { getByLabelText, getByText } = render( |
8 |
| - Login, { props: { onSubmit: handleSubmit } } |
9 |
| - ) |
| 5 | +test('Review form submits', async () => { |
| 6 | + const fakeReview = { |
| 7 | + title: 'An Awesome Movie', |
| 8 | + review: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', |
| 9 | + rating: '3', |
| 10 | + genre: 'Action', |
| 11 | + recommend: true |
| 12 | + } |
10 | 13 |
|
11 |
| - const submitButtonNode = getByText('Submit') |
| 14 | + const { |
| 15 | + getByLabelText, |
| 16 | + getByText, |
| 17 | + getByTestId, |
| 18 | + getByDisplayValue, |
| 19 | + getByPlaceholderText, |
| 20 | + emitted |
| 21 | + } = render(Form) |
12 | 22 |
|
13 |
| - const userNameInput = getByLabelText('Username') |
14 |
| - await fireEvent.update(userNameInput, fakeUser.username) |
| 23 | + const submitButton = getByText('Submit') |
15 | 24 |
|
16 |
| - const passwordInput = getByLabelText('Password') |
17 |
| - await fireEvent.update(passwordInput, fakeUser.password) |
| 25 | + // Initially the submit button should be disabled |
| 26 | + expect(submitButton).toBeDisabled() |
18 | 27 |
|
19 |
| - const rememberMeInput = getByLabelText('Remember Me') |
20 |
| - await fireEvent.update(rememberMeInput, true) |
| 28 | + // In this test we showcase several ways of targetting DOM elements. |
| 29 | + // However, `getByLabelText` should be your top preference when handling |
| 30 | + // form elements. |
| 31 | + // Read 'What queries should I use?' for additional context: |
| 32 | + // https://testing-library.com/docs/guide-which-query |
| 33 | + |
| 34 | + const titleInput = getByLabelText(/title of the movie/i) |
| 35 | + await fireEvent.update(titleInput, fakeReview.title) |
| 36 | + |
| 37 | + const reviewTextarea = getByPlaceholderText('Write an awesome review') |
| 38 | + await fireEvent.update(reviewTextarea, fakeReview.review) |
| 39 | + |
| 40 | + const ratingSelect = getByLabelText('Wonderful') |
| 41 | + await fireEvent.update(ratingSelect, fakeReview.rating) |
| 42 | + |
| 43 | + // Get the Select element by using the initially displayed value. |
| 44 | + const genreSelect = getByDisplayValue('Comedy') |
| 45 | + await fireEvent.update(genreSelect, fakeReview.genre) |
| 46 | + |
| 47 | + const recommendInput = getByTestId('recommend-checkbox') |
| 48 | + await fireEvent.update(recommendInput, fakeReview.recommend) |
21 | 49 |
|
22 | 50 | // NOTE: in jsdom, it's not possible to trigger a form submission
|
23 | 51 | // by clicking on the submit button. This is really unfortunate.
|
24 | 52 | // So the next best thing is to fireEvent a submit on the form itself
|
25 | 53 | // then ensure that there's a submit button.
|
26 |
| - await fireEvent.click(submitButtonNode) |
| 54 | + expect(submitButton).toBeEnabled() |
| 55 | + expect(submitButton).toHaveAttribute('type', 'submit') |
| 56 | + |
| 57 | + await fireEvent.click(submitButton) |
27 | 58 |
|
28 |
| - // Assert |
29 |
| - expect(handleSubmit).toHaveBeenCalledTimes(1) |
30 |
| - expect(handleSubmit).toHaveBeenCalledWith(fakeUser) |
31 |
| - // make sure the form is submittable |
32 |
| - expect(submitButtonNode.type).toBe('submit') |
| 59 | + // Assert event has been emitted. |
| 60 | + expect(emitted().submit).toHaveLength(1) |
| 61 | + expect(emitted().submit[0]).toEqual([ fakeReview ]) |
33 | 62 | })
|
0 commit comments