diff --git a/tests/__tests__/components/Form.vue b/tests/__tests__/components/Form.vue index f5702ebd..616af3d4 100644 --- a/tests/__tests__/components/Form.vue +++ b/tests/__tests__/components/Form.vue @@ -1,22 +1,105 @@ - - - - Search - - - - - - - + + + Movie Review + + Title of the movie + + + Your review + + + + + Wonderful + + + + Average + + + + Awful + + + Movie genre + + Comedy + Action + Romance + None of the above + + + Would you recommend this movie? + + + + Submit + + + + + + diff --git a/tests/__tests__/components/Login.vue b/tests/__tests__/components/Login.vue deleted file mode 100644 index 1f841f6f..00000000 --- a/tests/__tests__/components/Login.vue +++ /dev/null @@ -1,57 +0,0 @@ - - - - Username - - Password - - Remember Me - - Submit - - - - - diff --git a/tests/__tests__/components/Stubs.vue b/tests/__tests__/components/Stubs.vue new file mode 100644 index 00000000..f5702ebd --- /dev/null +++ b/tests/__tests__/components/Stubs.vue @@ -0,0 +1,22 @@ + + + + Search + + + + + + + diff --git a/tests/__tests__/form.js b/tests/__tests__/form.js index a5a215b6..5b4a3898 100644 --- a/tests/__tests__/form.js +++ b/tests/__tests__/form.js @@ -1,33 +1,62 @@ import { render, fireEvent } from '@testing-library/vue' -import Login from './components/Login' +import 'jest-dom/extend-expect' +import Form from './components/Form' -test('login form submits', async () => { - const fakeUser = { username: 'jackiechan', password: 'hiya! 🥋', rememberMe: true } - const handleSubmit = jest.fn() - const { getByLabelText, getByText } = render( - Login, { props: { onSubmit: handleSubmit } } - ) +test('Review form submits', async () => { + const fakeReview = { + title: 'An Awesome Movie', + review: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', + rating: '3', + genre: 'Action', + recommend: true + } - const submitButtonNode = getByText('Submit') + const { + getByLabelText, + getByText, + getByTestId, + getByDisplayValue, + getByPlaceholderText, + emitted + } = render(Form) - const userNameInput = getByLabelText('Username') - await fireEvent.update(userNameInput, fakeUser.username) + const submitButton = getByText('Submit') - const passwordInput = getByLabelText('Password') - await fireEvent.update(passwordInput, fakeUser.password) + // Initially the submit button should be disabled + expect(submitButton).toBeDisabled() - const rememberMeInput = getByLabelText('Remember Me') - await fireEvent.update(rememberMeInput, true) + // In this test we showcase several ways of targetting DOM elements. + // However, `getByLabelText` should be your top preference when handling + // form elements. + // Read 'What queries should I use?' for additional context: + // https://testing-library.com/docs/guide-which-query + + const titleInput = getByLabelText(/title of the movie/i) + await fireEvent.update(titleInput, fakeReview.title) + + const reviewTextarea = getByPlaceholderText('Write an awesome review') + await fireEvent.update(reviewTextarea, fakeReview.review) + + const ratingSelect = getByLabelText('Wonderful') + await fireEvent.update(ratingSelect, fakeReview.rating) + + // Get the Select element by using the initially displayed value. + const genreSelect = getByDisplayValue('Comedy') + await fireEvent.update(genreSelect, fakeReview.genre) + + const recommendInput = getByTestId('recommend-checkbox') + await fireEvent.update(recommendInput, fakeReview.recommend) // NOTE: in jsdom, it's not possible to trigger a form submission // by clicking on the submit button. This is really unfortunate. // So the next best thing is to fireEvent a submit on the form itself // then ensure that there's a submit button. - await fireEvent.click(submitButtonNode) + expect(submitButton).toBeEnabled() + expect(submitButton).toHaveAttribute('type', 'submit') + + await fireEvent.click(submitButton) - // Assert - expect(handleSubmit).toHaveBeenCalledTimes(1) - expect(handleSubmit).toHaveBeenCalledWith(fakeUser) - // make sure the form is submittable - expect(submitButtonNode.type).toBe('submit') + // Assert event has been emitted. + expect(emitted().submit).toHaveLength(1) + expect(emitted().submit[0]).toEqual([ fakeReview ]) }) diff --git a/tests/__tests__/integration-with-stubs.js b/tests/__tests__/stubs.js similarity index 70% rename from tests/__tests__/integration-with-stubs.js rename to tests/__tests__/stubs.js index b78077d6..c2af92db 100644 --- a/tests/__tests__/integration-with-stubs.js +++ b/tests/__tests__/stubs.js @@ -1,10 +1,10 @@ import { render, cleanup } from '@testing-library/vue' -import Form from './components/Form' +import Stubs from './components/Stubs' afterEach(cleanup) test('Form contains search button', () => { - const { getByText } = render(Form, { + const { getByText } = render(Stubs, { stubs: ['FontAwesomeIcon'] }) getByText('Search now')