@@ -2,6 +2,7 @@ import React from 'react';
2
2
import { render , cleanup , fireEvent } from '@testing-library/react' ;
3
3
import '@testing-library/jest-dom/extend-expect' ;
4
4
import App from './App' ;
5
+ import { validInputs , inValidInputs } from '../../test/fixtures/app' ;
5
6
6
7
7
8
afterEach ( cleanup ) ;
@@ -15,10 +16,10 @@ describe('App Component', () => {
15
16
16
17
it ( 'should render <p data-test=username /> on wrong data input' , ( ) => {
17
18
const { getByTestId, getByPlaceholderText } = render ( < App /> ) ;
18
- fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : 'abc@de123com' } } ) ;
19
+ fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : inValidInputs . username } } ) ;
19
20
20
21
expect ( getByTestId ( 'username' ) ) . toHaveTextContent ( 'The userName field must contain only alphabetic characters.' ) ;
21
- expect ( getByPlaceholderText ( 'Username' ) ) . toHaveValue ( 'abc@de123com' ) ;
22
+ expect ( getByPlaceholderText ( 'Username' ) ) . toHaveValue ( inValidInputs . username ) ;
22
23
} ) ;
23
24
24
25
it ( 'should render <p data-test=username /> on empty data input' , ( ) => {
@@ -31,10 +32,10 @@ describe('App Component', () => {
31
32
32
33
it ( 'should render <p data-test=age /> on wrong data input' , ( ) => {
33
34
const { getByTestId, getByPlaceholderText } = render ( < App /> ) ;
34
- fireEvent . change ( getByPlaceholderText ( 'Age' ) , { target : { value : 'abc@de123com' } } ) ;
35
+ fireEvent . change ( getByPlaceholderText ( 'Age' ) , { target : { value : inValidInputs . age } } ) ;
35
36
36
37
expect ( getByTestId ( 'age' ) ) . toHaveTextContent ( 'The age must be a number.' ) ;
37
- expect ( getByPlaceholderText ( 'Age' ) ) . toHaveValue ( 'abc@de123com' ) ;
38
+ expect ( getByPlaceholderText ( 'Age' ) ) . toHaveValue ( inValidInputs . age ) ;
38
39
} ) ;
39
40
40
41
it ( 'should not render <p data-test=age /> on empty data input' , ( ) => {
@@ -54,17 +55,17 @@ describe('App Component', () => {
54
55
55
56
it ( 'should remove error message upon data input correction' , ( ) => {
56
57
const { getByTestId, getByPlaceholderText } = render ( < App /> ) ;
57
- fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : 'abc@de123com' } } ) ;
58
+ fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : inValidInputs . username } } ) ;
58
59
59
60
expect ( getByTestId ( 'username' ) ) . toHaveTextContent ( 'The userName field must contain only alphabetic characters.' ) ;
60
- expect ( getByPlaceholderText ( 'Username' ) ) . toHaveValue ( 'abc@de123com' ) ;
61
+ expect ( getByPlaceholderText ( 'Username' ) ) . toHaveValue ( inValidInputs . username ) ;
61
62
62
63
fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : ' ' } } ) ;
63
64
64
65
expect ( getByTestId ( 'username' ) ) . toHaveTextContent ( 'The userName field cannot be empty.' ) ;
65
66
expect ( getByPlaceholderText ( 'Username' ) ) . toHaveValue ( ' ' ) ;
66
67
67
- fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : 'Eazybee' } } ) ;
68
+ fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : validInputs . username } } ) ;
68
69
69
70
let element ;
70
71
@@ -76,41 +77,71 @@ describe('App Component', () => {
76
77
expect ( error . message . includes ( 'Unable to find an element by: [data-testid="username"]' ) ) . toBeTruthy ( ) ;
77
78
}
78
79
79
- expect ( getByPlaceholderText ( 'Username' ) ) . toHaveValue ( 'Eazybee' ) ;
80
+ expect ( getByPlaceholderText ( 'Username' ) ) . toHaveValue ( validInputs . username ) ;
80
81
} ) ;
81
82
82
- it ( 'should submit and render table on valid data input' , ( ) => {
83
- const {
84
- getByTestId, getByPlaceholderText, getByText,
85
- } = render ( < App /> ) ;
86
-
87
- fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : 'EazyBee' } } ) ;
88
- fireEvent . change ( getByPlaceholderText ( 'Age' ) , { target : { value : '12' } } ) ;
89
- fireEvent . click ( getByText ( 'Submit' ) ) ;
90
-
91
- expect ( getByTestId ( 'table' ) ) . toBeTruthy ( ) ;
92
- expect ( getByTestId ( 'table' ) ) . toContainElement ( getByTestId ( 'userName0' ) ) ;
93
- expect ( getByTestId ( 'table' ) ) . toContainElement ( getByTestId ( 'age1' ) ) ;
83
+ describe ( 'Submit Button' , ( ) => {
84
+ it ( 'should submit and render table on valid data input' , ( ) => {
85
+ const {
86
+ getByTestId, getByPlaceholderText, getByText,
87
+ } = render ( < App /> ) ;
88
+
89
+ fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : validInputs . username } } ) ;
90
+ fireEvent . change ( getByPlaceholderText ( 'Age' ) , { target : { value : validInputs . age } } ) ;
91
+ fireEvent . click ( getByText ( 'Submit' ) ) ;
92
+
93
+ expect ( getByTestId ( 'table' ) ) . toBeTruthy ( ) ;
94
+ expect ( getByTestId ( 'table' ) ) . toContainElement ( getByTestId ( 'userName0' ) ) ;
95
+ expect ( getByTestId ( 'table' ) ) . toContainElement ( getByTestId ( 'age1' ) ) ;
96
+ } ) ;
97
+
98
+ it ( 'should not submit nor render table on invalid data input' , ( ) => {
99
+ const {
100
+ getByTestId, getByPlaceholderText, getByText,
101
+ } = render ( < App /> ) ;
102
+
103
+ fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : inValidInputs . username } } ) ;
104
+ fireEvent . change ( getByPlaceholderText ( 'Age' ) , { target : { value : validInputs . age } } ) ;
105
+ fireEvent . click ( getByText ( 'Submit' ) ) ;
106
+
107
+ let element ;
108
+
109
+ try {
110
+ element = getByTestId ( 'table' ) ;
111
+ } catch ( error ) {
112
+ expect ( element ) . toBe ( undefined ) ;
113
+ expect ( error ) . toBeTruthy ( ) ;
114
+ expect ( error . message . includes ( 'Unable to find an element by: [data-testid="table"]' ) ) . toBeTruthy ( ) ;
115
+ expect ( getByTestId ( 'username' ) ) . toHaveTextContent ( 'The userName field must contain only alphabetic characters.' ) ;
116
+ }
117
+ } ) ;
94
118
} ) ;
95
119
96
- it ( 'should not submit and render table on valid data input' , ( ) => {
97
- const {
98
- getByTestId, getByPlaceholderText, getByText,
99
- } = render ( < App /> ) ;
100
-
101
- fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : '1Eazyb' } } ) ;
102
- fireEvent . change ( getByPlaceholderText ( 'Age' ) , { target : { value : '12' } } ) ;
103
- fireEvent . click ( getByText ( 'Submit' ) ) ;
104
-
105
- let element ;
106
-
107
- try {
108
- element = getByTestId ( 'table' ) ;
109
- } catch ( error ) {
110
- expect ( element ) . toBe ( undefined ) ;
111
- expect ( error ) . toBeTruthy ( ) ;
112
- expect ( error . message . includes ( 'Unable to find an element by: [data-testid="table"]' ) ) . toBeTruthy ( ) ;
113
- expect ( getByTestId ( 'username' ) ) . toHaveTextContent ( 'The userName field must contain only alphabetic characters.' ) ;
114
- }
120
+ describe ( 'Reset Button' , ( ) => {
121
+ it ( 'should clear all inputs field' , ( ) => {
122
+ const { getByText, getByPlaceholderText } = render ( < App /> ) ;
123
+ fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : validInputs . username } } ) ;
124
+ fireEvent . change ( getByPlaceholderText ( 'Age' ) , { target : { value : validInputs . age } } ) ;
125
+ fireEvent . click ( getByText ( 'Reset' ) ) ;
126
+
127
+ expect ( getByPlaceholderText ( 'Username' ) ) . toHaveValue ( '' ) ;
128
+ expect ( getByPlaceholderText ( 'Age' ) ) . toHaveValue ( '' ) ;
129
+ } ) ;
130
+
131
+ it ( 'should clear all error message' , ( ) => {
132
+ const { getByText, getByTestId, getByPlaceholderText } = render ( < App /> ) ;
133
+ fireEvent . change ( getByPlaceholderText ( 'Username' ) , { target : { value : inValidInputs . username } } ) ;
134
+ fireEvent . click ( getByText ( 'Reset' ) ) ;
135
+
136
+ let element ;
137
+
138
+ try {
139
+ element = getByTestId ( 'username' ) ;
140
+ } catch ( error ) {
141
+ expect ( element ) . toBe ( undefined ) ;
142
+ expect ( error ) . toBeTruthy ( ) ;
143
+ expect ( error . message . includes ( 'Unable to find an element by: [data-testid="username"]' ) ) . toBeTruthy ( ) ;
144
+ }
145
+ } ) ;
115
146
} ) ;
116
147
} ) ;
0 commit comments