@@ -5,32 +5,42 @@ import Signup from './components/Signup'
5
5
test ( 'signup form submits' , async ( ) => {
6
6
const fakeUser = {
7
7
username : 'jackiechan' ,
8
- password : 'hiya! 🥋' ,
9
8
about : 'Lorem ipsum dolor sit amet' ,
10
9
selected : 'None of the above' ,
11
10
rememberMe : true
12
11
}
13
12
14
- const { getByLabelText, getByText, emitted } = render ( Signup )
13
+ const {
14
+ getByLabelText,
15
+ getByText,
16
+ getByTestId,
17
+ getByDisplayValue,
18
+ getByPlaceholderText,
19
+ emitted
20
+ } = render ( Signup )
15
21
16
22
const submitButton = getByText ( 'Submit' )
17
23
18
24
// Initially the form should be disabled
19
25
expect ( submitButton ) . toBeDisabled ( )
20
26
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)
22
34
await fireEvent . update ( userNameInput , fakeUser . username )
23
35
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...' )
28
37
await fireEvent . update ( aboutMeTextarea , fakeUser . about )
29
38
30
- const rememberMeInput = getByLabelText ( 'Remember Me ')
39
+ const rememberMeInput = getByTestId ( 'remember-input ')
31
40
await fireEvent . update ( rememberMeInput , fakeUser . rememberMe )
32
41
33
- const preferenceSelect = getByLabelText ( 'I prefer...' )
42
+ // Get the Select element by using the initially displayed value.
43
+ const preferenceSelect = getByDisplayValue ( 'Dogs' )
34
44
await fireEvent . update ( preferenceSelect , fakeUser . selected )
35
45
36
46
// NOTE: in jsdom, it's not possible to trigger a form submission
@@ -39,6 +49,7 @@ test('signup form submits', async () => {
39
49
// then ensure that there's a submit button.
40
50
expect ( submitButton ) . toBeEnabled ( )
41
51
expect ( submitButton ) . toHaveAttribute ( 'type' , 'submit' )
52
+
42
53
await fireEvent . click ( submitButton )
43
54
44
55
// Assert event has been emitted.
0 commit comments