@@ -6,126 +6,36 @@ import userEvent from '@testing-library/user-event';
6
6
import ContactForm from './ContactForm' ;
7
7
8
8
test ( 'renders without errors' , ( ) => {
9
- render ( < ContactForm /> ) ;
9
+
10
10
} ) ;
11
11
12
12
test ( 'renders the contact form header' , ( ) => {
13
- render ( < ContactForm /> ) ;
14
-
15
- const headerElement = screen . queryByText ( / c o n t a c t f o r m / i) ;
16
- expect ( headerElement ) . toBeInTheDocument ( ) ;
17
- expect ( headerElement ) . toBeTruthy ( ) ;
18
- expect ( headerElement ) . toHaveTextContent ( / c o n t a c t f o r m / i) ;
13
+
19
14
} ) ;
20
15
21
16
test ( 'renders ONE error message if user enters less then 5 characters into firstname.' , async ( ) => {
22
- render ( < ContactForm /> ) ;
23
-
24
- const firstNameField = screen . getByLabelText ( / F i r s t N a m e * / i) ;
25
- userEvent . type ( firstNameField , "123" ) ;
26
-
27
- const errorMessages = await screen . findAllByTestId ( 'error' ) ;
28
- expect ( errorMessages ) . toHaveLength ( 1 ) ;
17
+
29
18
} ) ;
30
19
31
20
test ( 'renders THREE error messages if user enters no values into any fields.' , async ( ) => {
32
- render ( < ContactForm /> ) ;
33
-
34
- const submitButton = screen . getByRole ( "button" ) ;
35
- userEvent . click ( submitButton ) ;
36
-
37
- await waitFor ( ( ) => {
38
- const errorMessages = screen . queryAllByTestId ( "error" ) ;
39
- expect ( errorMessages ) . toHaveLength ( 3 ) ;
40
- } ) ;
21
+
41
22
} ) ;
42
23
43
24
test ( 'renders ONE error message if user enters a valid first name and last name but no email.' , async ( ) => {
44
- render ( < ContactForm /> ) ;
45
-
46
- const firstNameField = screen . getByLabelText ( / f i r s t n a m e * / i) ;
47
- userEvent . type ( firstNameField , "warren" ) ;
48
-
49
- const lastNameField = screen . getByLabelText ( / l a s t n a m e * / i) ;
50
- userEvent . type ( lastNameField , "longmire" ) ;
51
-
52
- const button = screen . getByRole ( "button" ) ;
53
- userEvent . click ( button ) ;
54
-
55
- const errorMessages = await screen . findAllByTestId ( "error" ) ;
56
- expect ( errorMessages ) . toHaveLength ( 1 ) ;
25
+
57
26
} ) ;
58
27
59
28
test ( 'renders "email must be a valid email address" if an invalid email is entered' , async ( ) => {
60
- render ( < ContactForm /> ) ;
61
- const emailField = screen . getByLabelText ( / e m a i l * / i) ;
62
- userEvent . type ( emailField , "warren@gmail" ) ;
63
-
64
- const errorMessage = await screen . findByText ( / e m a i l m u s t b e a v a l i d e m a i l a d d r e s s / i) ;
65
- expect ( errorMessage ) . toBeInTheDocument ( ) ;
29
+
66
30
} ) ;
67
31
68
32
test ( 'renders "lastName is a required field" if an last name is not entered and the submit button is clicked' , async ( ) => {
69
- render ( < ContactForm /> ) ;
70
- const submitButton = screen . getByRole ( "button" ) ;
71
- userEvent . click ( submitButton ) ;
72
-
73
- const errorMessage = await screen . findByText ( / l a s t N a m e i s a r e q u i r e d f i e l d / i) ;
74
- expect ( errorMessage ) . toBeInTheDocument ( ) ;
33
+
75
34
} ) ;
76
35
77
36
test ( 'renders all firstName, lastName and email text when submitted. Does NOT render message if message is not submitted.' , async ( ) => {
78
- render ( < ContactForm /> ) ;
79
-
80
- const firstNameField = screen . getByLabelText ( / f i r s t n a m e * / i) ;
81
- const lastNameField = screen . getByLabelText ( / l a s t n a m e * / i) ;
82
- const emailField = screen . getByLabelText ( / e m a i l * / i) ;
83
-
84
- userEvent . type ( firstNameField , "warren" ) ;
85
- userEvent . type ( lastNameField , "longmire" ) ;
86
- userEvent . type ( emailField , "longmire@email.com" ) ;
87
-
88
- const button = screen . getByRole ( "button" ) ;
89
- userEvent . click ( button ) ;
90
-
91
- await waitFor ( ( ) => {
92
- const firstnameDisplay = screen . queryByText ( "warren" ) ;
93
- const lastnameDisplay = screen . queryByText ( "longmire" ) ;
94
- const emailDisplay = screen . queryByText ( "longmire@email.com" ) ;
95
- const messageDisplay = screen . queryByTestId ( "messageDisplay" ) ;
96
-
97
- expect ( firstnameDisplay ) . toBeInTheDocument ( ) ;
98
- expect ( lastnameDisplay ) . toBeInTheDocument ( ) ;
99
- expect ( emailDisplay ) . toBeInTheDocument ( ) ;
100
- expect ( messageDisplay ) . not . toBeInTheDocument ( ) ;
101
- } ) ;
37
+
102
38
} ) ;
103
39
104
40
test ( 'renders all fields text when all fields are submitted.' , async ( ) => {
105
- render ( < ContactForm /> ) ;
106
-
107
- const firstNameField = screen . getByLabelText ( / F i r s t N a m e * / i) ;
108
- const lastNameField = screen . getByLabelText ( / L a s t N a m e * / i) ;
109
- const emailField = screen . getByLabelText ( / E m a i l * / i) ;
110
- const messageField = screen . getByLabelText ( / M e s s a g e / i) ;
111
-
112
- userEvent . type ( firstNameField , "Johnny" ) ;
113
- userEvent . type ( lastNameField , "Doe" ) ;
114
- userEvent . type ( emailField , "address@gmail.com" ) ;
115
- userEvent . type ( messageField , "this is a message" ) ;
116
-
117
- const submitButton = await screen . findByRole ( "button" ) ;
118
- userEvent . click ( submitButton ) ;
119
-
120
- await waitFor ( ( ) => {
121
- const firstnameDisplay = screen . queryByText ( / J o h n / i) ;
122
- const lastnameDisplay = screen . queryByText ( / D o e / i) ;
123
- const emailDisplay = screen . queryByText ( / a d d r e s s @ g m a i l .c o m / i) ;
124
- const messageDisplay = screen . queryByText ( / t h i s i s a m e s s a g e / i) ;
125
-
126
- expect ( firstnameDisplay ) . toBeInTheDocument ( ) ;
127
- expect ( lastnameDisplay ) . toBeInTheDocument ( ) ;
128
- expect ( emailDisplay ) . toBeInTheDocument ( ) ;
129
- expect ( messageDisplay ) . toBeInTheDocument ( ) ;
130
- } ) ;
131
41
} ) ;
0 commit comments