@@ -5,32 +5,42 @@ import Signup from './components/Signup'
55test ( 'signup form submits' , async ( ) => {
66 const fakeUser = {
77 username : 'jackiechan' ,
8- password : 'hiya! 🥋' ,
98 about : 'Lorem ipsum dolor sit amet' ,
109 selected : 'None of the above' ,
1110 rememberMe : true
1211 }
1312
14- const { getByLabelText, getByText, emitted } = render ( Signup )
13+ const {
14+ getByLabelText,
15+ getByText,
16+ getByTestId,
17+ getByDisplayValue,
18+ getByPlaceholderText,
19+ emitted
20+ } = render ( Signup )
1521
1622 const submitButton = getByText ( 'Submit' )
1723
1824 // Initially the form should be disabled
1925 expect ( submitButton ) . toBeDisabled ( )
2026
21- const userNameInput = getByLabelText ( 'Username' )
27+ // We are gonna showcase several ways of targetting DOM elements.
28+ // However, `getByLabelText` should be your top preference when handling
29+ // form elements.
30+ //
31+ // Read 'What queries should I use?' for additional context:
32+ // https://testing-library.com/docs/guide-which-query
33+ const userNameInput = getByLabelText ( / u s e r n a m e / i)
2234 await fireEvent . update ( userNameInput , fakeUser . username )
2335
24- const passwordInput = getByLabelText ( 'Password' )
25- await fireEvent . update ( passwordInput , fakeUser . password )
26-
27- const aboutMeTextarea = getByLabelText ( 'About Me' )
36+ const aboutMeTextarea = getByPlaceholderText ( 'I was born in...' )
2837 await fireEvent . update ( aboutMeTextarea , fakeUser . about )
2938
30- const rememberMeInput = getByLabelText ( 'Remember Me ')
39+ const rememberMeInput = getByTestId ( 'remember-input ')
3140 await fireEvent . update ( rememberMeInput , fakeUser . rememberMe )
3241
33- const preferenceSelect = getByLabelText ( 'I prefer...' )
42+ // Get the Select element by using the initially displayed value.
43+ const preferenceSelect = getByDisplayValue ( 'Dogs' )
3444 await fireEvent . update ( preferenceSelect , fakeUser . selected )
3545
3646 // NOTE: in jsdom, it's not possible to trigger a form submission
@@ -39,6 +49,7 @@ test('signup form submits', async () => {
3949 // then ensure that there's a submit button.
4050 expect ( submitButton ) . toBeEnabled ( )
4151 expect ( submitButton ) . toHaveAttribute ( 'type' , 'submit' )
52+
4253 await fireEvent . click ( submitButton )
4354
4455 // Assert event has been emitted.
0 commit comments