Skip to content

Commit 22b3132

Browse files
committed
added form reset after a note is created so that users can easily enter a new note
1 parent de88b0e commit 22b3132

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,4 +944,62 @@ test('should add a new note when name and description are provided', () => {
944944
- Rerun you Cypress tests.
945945
- Commit!
946946

947-
[Code for this section](https://github.com/pairing4good/tdd-amplify-react/commit/959bafeba3080065bbaa161825d1371b739a3973)
947+
[Code for this section](https://github.com/pairing4good/tdd-amplify-react/commit/959bafeba3080065bbaa161825d1371b739a3973)
948+
949+
## Reset Form
950+
One a note is saved the name and description fields should be reset to empty strings.
951+
952+
- Add a test to `NoteForm.test.js`
953+
```js
954+
test('should add a new note when name and description are provided', () => {
955+
formData.name = "test name";
956+
formData.description = "test description";
957+
958+
const button = screen.getByTestId('note-form-submit');
959+
960+
fireEvent.click(button);
961+
962+
expect(formData.name).toBe("");
963+
expect(formData.description).toBe("");
964+
});
965+
```
966+
- Make this failing test go Green
967+
```js
968+
function createNote() {
969+
if (!props.formData.name || !props.formData.description) return;
970+
props.setNotesCallback([ ...props.notes, props.formData ]);
971+
props.formData.name = '';
972+
props.formData.description = '';
973+
}
974+
```
975+
- Green
976+
- Run the Cypress tests and it's **Red**.
977+
978+
What happened? Well while this approach worked for a lower level component test it does not work when React is managing its own [state](https://reactjs.org/docs/state-and-lifecycle.html). React clearly states that you should [not modify state directly](https://reactjs.org/docs/state-and-lifecycle.html#do-not-modify-state-directly). Instead you should use the [setState](https://reactjs.org/docs/hooks-state.html) callback hook.
979+
980+
- Let's update the test to use the `setFormDataCallback` callback.
981+
```js
982+
test('should add a new note when name and description are provided', () => {
983+
formData.name = "test name";
984+
formData.description = "test description";
985+
986+
const button = screen.getByTestId('note-form-submit');
987+
988+
fireEvent.click(button);
989+
990+
expect(setFormDataCallback).toHaveBeenCalledWith({name: '', description: ''});
991+
});
992+
```
993+
- This red test drives these code changes
994+
```js
995+
function createNote() {
996+
if (!props.formData.name || !props.formData.description) return;
997+
props.setNotesCallback([ ...props.notes, props.formData ]);
998+
props.setFormDataCallback({name: '', description: ''});
999+
}
1000+
```
1001+
- Green!
1002+
- Cypress test is now Green!
1003+
- Commit
1004+
1005+
[Code for this section]()

src/NoteForm.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
function NoteForm(props) {
2-
2+
33
function createNote() {
4-
if (!props.formData.name || !props.formData.description) return;
5-
props.setNotesCallback([ ...props.notes, props.formData ]);
4+
if (!props.formData.name || !props.formData.description) return;
5+
props.setNotesCallback([ ...props.notes, props.formData ]);
6+
props.setFormDataCallback({name: '', description: ''});
67
}
78

89
return (

src/test/NoteForm.test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { render, screen, fireEvent } from '@testing-library/react';
22
import NoteForm from "../NoteForm";
33

44
const setNotesCallback = jest.fn();
5+
const setFormDataCallback = jest.fn();
56
const formData = {name: '', description: ''}
67

78
beforeEach(() => {
89
render(<NoteForm notes={[]}
910
setNotesCallback={setNotesCallback}
11+
setFormDataCallback={setFormDataCallback}
1012
formData={formData}/>)
1113
});
1214

@@ -29,8 +31,8 @@ test('should display the description placeholder', () => {
2931
});
3032

3133
test('should require name and description', () => {
32-
formData.description = "";
3334
formData.name = "";
35+
formData.description = "";
3436

3537
const button = screen.getByTestId('note-form-submit');
3638

@@ -40,8 +42,8 @@ test('should require name and description', () => {
4042
});
4143

4244
test('should require name when description provided', () => {
43-
formData.description = "test description";
4445
formData.name = "";
46+
formData.description = "test description";
4547

4648
const button = screen.getByTestId('note-form-submit');
4749

@@ -51,8 +53,8 @@ test('should require name when description provided', () => {
5153
});
5254

5355
test('should require description when name provided', () => {
54-
formData.description = "";
5556
formData.name = "test name";
57+
formData.description = "";
5658

5759
const button = screen.getByTestId('note-form-submit');
5860

@@ -62,12 +64,23 @@ test('should require description when name provided', () => {
6264
});
6365

6466
test('should add a new note when name and description are provided', () => {
65-
formData.description = "test description";
6667
formData.name = "test name";
68+
formData.description = "test description";
6769

6870
const button = screen.getByTestId('note-form-submit');
6971

7072
fireEvent.click(button);
7173

7274
expect(setNotesCallback.mock.calls.length).toBe(1);
75+
});
76+
77+
test('should add a new note when name and description are provided', () => {
78+
formData.name = "test name";
79+
formData.description = "test description";
80+
81+
const button = screen.getByTestId('note-form-submit');
82+
83+
fireEvent.click(button);
84+
85+
expect(setFormDataCallback).toHaveBeenCalledWith({name: '', description: ''});
7386
});

0 commit comments

Comments
 (0)