Skip to content

Commit addd109

Browse files
committed
feat(reset): create handle reset
- create handle reset to useForm - add reset button to form - write tests for handle reset [Finishes reset]
1 parent 62c1525 commit addd109

File tree

9 files changed

+117
-42
lines changed

9 files changed

+117
-42
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.codeclimate.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exclude_patterns:
2+
- "**/*.spec.js"

.eslintrc

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"parser": "babel-eslint",
33
"extends": ["airbnb-base", "plugin:react/recommended"],
4+
"parserOptions": {
5+
"ecmaVersion": 10,
6+
"ecmaFeatures": { "jsx": true }
7+
},
48
"env": {
59
"node": true,
610
"browser": true,

.hound.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eslint:
2+
enabled: true
3+
config_file: .eslintrc

src/components/App.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const App = () => {
1414
};
1515

1616
const {
17-
values, handleChange, handleSubmit, errors,
17+
values, handleChange, handleSubmit, errors, handleReset,
1818
} = useForm({ callback: login, rules });
1919

2020
const { userName, age } = values;
@@ -46,6 +46,7 @@ const App = () => {
4646
{ageErr && <p data-testid='age'>{ageErr}</p>}
4747
<br />
4848
<button type='submit'>Submit</button>
49+
<button type='reset' onClick={handleReset}>Reset</button>
4950
</form>
5051
{submittedValues
5152
&& <div className='table' data-testid='table'>

src/components/App.spec.js

+70-39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { render, cleanup, fireEvent } from '@testing-library/react';
33
import '@testing-library/jest-dom/extend-expect';
44
import App from './App';
5+
import { validInputs, inValidInputs } from '../../test/fixtures/app';
56

67

78
afterEach(cleanup);
@@ -15,10 +16,10 @@ describe('App Component', () => {
1516

1617
it('should render <p data-test=username /> on wrong data input', () => {
1718
const { getByTestId, getByPlaceholderText } = render(<App/>);
18-
fireEvent.change(getByPlaceholderText('Username'), { target: { value: 'abc@de123com' } });
19+
fireEvent.change(getByPlaceholderText('Username'), { target: { value: inValidInputs.username } });
1920

2021
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);
2223
});
2324

2425
it('should render <p data-test=username /> on empty data input', () => {
@@ -31,10 +32,10 @@ describe('App Component', () => {
3132

3233
it('should render <p data-test=age /> on wrong data input', () => {
3334
const { getByTestId, getByPlaceholderText } = render(<App/>);
34-
fireEvent.change(getByPlaceholderText('Age'), { target: { value: 'abc@de123com' } });
35+
fireEvent.change(getByPlaceholderText('Age'), { target: { value: inValidInputs.age } });
3536

3637
expect(getByTestId('age')).toHaveTextContent('The age must be a number.');
37-
expect(getByPlaceholderText('Age')).toHaveValue('abc@de123com');
38+
expect(getByPlaceholderText('Age')).toHaveValue(inValidInputs.age);
3839
});
3940

4041
it('should not render <p data-test=age /> on empty data input', () => {
@@ -54,17 +55,17 @@ describe('App Component', () => {
5455

5556
it('should remove error message upon data input correction', () => {
5657
const { getByTestId, getByPlaceholderText } = render(<App/>);
57-
fireEvent.change(getByPlaceholderText('Username'), { target: { value: 'abc@de123com' } });
58+
fireEvent.change(getByPlaceholderText('Username'), { target: { value: inValidInputs.username } });
5859

5960
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);
6162

6263
fireEvent.change(getByPlaceholderText('Username'), { target: { value: ' ' } });
6364

6465
expect(getByTestId('username')).toHaveTextContent('The userName field cannot be empty.');
6566
expect(getByPlaceholderText('Username')).toHaveValue(' ');
6667

67-
fireEvent.change(getByPlaceholderText('Username'), { target: { value: 'Eazybee' } });
68+
fireEvent.change(getByPlaceholderText('Username'), { target: { value: validInputs.username } });
6869

6970
let element;
7071

@@ -76,41 +77,71 @@ describe('App Component', () => {
7677
expect(error.message.includes('Unable to find an element by: [data-testid="username"]')).toBeTruthy();
7778
}
7879

79-
expect(getByPlaceholderText('Username')).toHaveValue('Eazybee');
80+
expect(getByPlaceholderText('Username')).toHaveValue(validInputs.username);
8081
});
8182

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+
});
94118
});
95119

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+
});
115146
});
116147
});

src/hooks/useForm.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ const useForm = ({ callback, rules }) => {
101101
return false;
102102
};
103103

104+
const handleReset = () => {
105+
setValues(prepareInitialState());
106+
setErrors({});
107+
};
108+
104109
return {
105-
values, handleChange, handleSubmit, errors,
110+
values, handleChange, handleSubmit, errors, handleReset,
106111
};
107112
};
108113

src/hooks/useForm.spec.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('userForm hook', () => {
99
it(' should render', () => {
1010
const { result } = renderHook(() => useForm({ callback, rules }));
1111
const {
12-
values, errors, handleChange, handleSubmit,
12+
values, errors, handleChange, handleSubmit, handleReset,
1313
} = result.current;
1414

1515
expect(values.age).toBe('');
@@ -18,6 +18,7 @@ describe('userForm hook', () => {
1818
expect(errors.ausername).toBe(undefined);
1919
expect(typeof handleChange).toBe('function');
2020
expect(typeof handleSubmit).toBe('function');
21+
expect(typeof handleReset).toBe('function');
2122
});
2223

2324
describe('handleChange function', () => {
@@ -102,4 +103,23 @@ describe('userForm hook', () => {
102103
expect(errors.username).toBe('The username field is required.');
103104
});
104105
});
106+
107+
describe('handleReset function', () => {
108+
it('should clear all inputs field when called', () => {
109+
const { result } = renderHook(() => useForm({ callback, rules }));
110+
111+
const { handleReset } = result.current;
112+
113+
act(() => {
114+
handleReset();
115+
});
116+
117+
const {
118+
values: { username }, errors,
119+
} = result.current;
120+
121+
expect(username).toBe('');
122+
expect(Object.keys(errors).length).toBe(0);
123+
});
124+
});
105125
});

test/fixtures/app.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const validInputs = {
2+
username: 'Eazybee',
3+
age: 12,
4+
};
5+
6+
export const inValidInputs = {
7+
username: 'Eazybee123',
8+
age: '12A',
9+
};

0 commit comments

Comments
 (0)