|
1 | 1 | import { render, fireEvent } from '@testing-library/vue' |
2 | | -import Login from './components/Login' |
| 2 | +import 'jest-dom/extend-expect' |
| 3 | +import Signup from './components/Signup' |
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('signup form submits', async () => { |
| 6 | + const fakeUser = { |
| 7 | + username: 'jackiechan', |
| 8 | + password: 'hiya! 🥋', |
| 9 | + about: 'Lorem ipsum dolor sit amet', |
| 10 | + selected: 'None of the above', |
| 11 | + rememberMe: true |
| 12 | + } |
10 | 13 |
|
11 | | - const submitButtonNode = getByText('Submit') |
| 14 | + const { getByLabelText, getByText, emitted } = render(Signup) |
| 15 | + |
| 16 | + const submitButton = getByText('Submit') |
| 17 | + |
| 18 | + // Initially the form should be disabled |
| 19 | + expect(submitButton).toBeDisabled() |
12 | 20 |
|
13 | 21 | const userNameInput = getByLabelText('Username') |
14 | 22 | await fireEvent.update(userNameInput, fakeUser.username) |
15 | 23 |
|
16 | 24 | const passwordInput = getByLabelText('Password') |
17 | 25 | await fireEvent.update(passwordInput, fakeUser.password) |
18 | 26 |
|
| 27 | + const aboutMeTextarea = getByLabelText('About Me') |
| 28 | + await fireEvent.update(aboutMeTextarea, fakeUser.about) |
| 29 | + |
19 | 30 | const rememberMeInput = getByLabelText('Remember Me') |
20 | | - await fireEvent.update(rememberMeInput, true) |
| 31 | + await fireEvent.update(rememberMeInput, fakeUser.rememberMe) |
| 32 | + |
| 33 | + const preferenceSelect = getByLabelText('I prefer...') |
| 34 | + await fireEvent.update(preferenceSelect, fakeUser.selected) |
21 | 35 |
|
22 | 36 | // NOTE: in jsdom, it's not possible to trigger a form submission |
23 | 37 | // by clicking on the submit button. This is really unfortunate. |
24 | 38 | // So the next best thing is to fireEvent a submit on the form itself |
25 | 39 | // then ensure that there's a submit button. |
26 | | - await fireEvent.click(submitButtonNode) |
| 40 | + expect(submitButton).toBeEnabled() |
| 41 | + expect(submitButton).toHaveAttribute('type', 'submit') |
| 42 | + await fireEvent.click(submitButton) |
27 | 43 |
|
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') |
| 44 | + // Assert event has been emitted. |
| 45 | + expect(emitted().submit).toHaveLength(1) |
| 46 | + expect(emitted().submit[0]).toEqual([ fakeUser ]) |
33 | 47 | }) |
0 commit comments