Skip to content

Commit f9c9155

Browse files
committed
added testing for the note list
1 parent 6e6e923 commit f9c9155

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ test('should require name and description', () => {
975975
expect(setNotesCallback.mock.calls.length).toBe(0);
976976
});
977977
```
978+
- **When `...` is on a line by itself in a code example it means that I hav not provided all of the code from that file. Please be careful to copy each section that is separated by `...`'s and use them in the appropriate part of your files.**
978979

979980
- This test checks to see if the jest [mock function](https://jestjs.io/docs/mock-functions) was called. In this test the note's name and description are blank so a new note should not be created and added to the list of notes.
980981
- We have a failing test.
@@ -1757,4 +1758,69 @@ async function deleteNoteCallback( id ) {
17571758
17581759
[Code for this section](https://github.com/pairing4good/tdd-amplify-react/commit/c17100754bf3a9edfebfeb8219b87766fb1cde00)
17591760
1761+
</details>
1762+
1763+
<details>
1764+
<summary>Note List Component Testing</summary>
1765+
1766+
## Note List Component Testing
1767+
Since we started at the top of the testing pyramid we need to make sure, once we are on green, that we work our way down to lower level tests too.
1768+
1769+
- Add a test to `NoteList.test.js` to verify the deletion behavior of the `NoteList` component.
1770+
```js
1771+
import { render, screen, fireEvent } from '@testing-library/react';
1772+
import NoteList from '../NoteList';
1773+
1774+
const deleteNoteCallback = jest.fn();
1775+
1776+
const defaultProps = {
1777+
notes: [],
1778+
deleteNoteCallback: deleteNoteCallback
1779+
};
1780+
1781+
const setup = (props = {}) => {
1782+
const setupProps = { ...defaultProps, ...props};
1783+
return render(<NoteList {...setupProps}/>);
1784+
};
1785+
1786+
test('should display nothing when no notes are provided', () => {
1787+
setup();
1788+
...
1789+
});
1790+
1791+
test('should display one note when one notes is provided', () => {
1792+
const note = {name: 'test name', description: 'test description'}
1793+
setup({notes: [note]});
1794+
...
1795+
});
1796+
1797+
test('should display one note when one notes is provided', () => {
1798+
const firstNote = {name: 'test name 1', description: 'test description 1'}
1799+
const secondNote = {name: 'test name 1', description: 'test description 1'}
1800+
setup({notes: [firstNote, secondNote]});
1801+
...
1802+
});
1803+
1804+
test('should delete note when clicked', () => {
1805+
const note = {
1806+
id: 1,
1807+
name: 'test name 1',
1808+
description: 'test description 1'
1809+
}
1810+
const notes = [ note ]
1811+
setup({notes: notes});
1812+
const button = screen.getByTestId('test-button-0');
1813+
1814+
fireEvent.click(button)
1815+
1816+
expect(deleteNoteCallback.mock.calls.length).toBe(1);
1817+
expect(deleteNoteCallback.mock.calls[0][0]).toStrictEqual(1);
1818+
});
1819+
```
1820+
- I added a mock function for the `deleteNoteCallback` and a `setup` function that has properties that can be overridden for specific test cases. This is a pattern that is often used in this style of tests.
1821+
1822+
- Run all of the tests
1823+
- Green
1824+
- Commit
1825+
17601826
</details>

src/test/NoteList.test.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
import { render, screen, getByTestId } from '@testing-library/react';
1+
import { render, screen, fireEvent } from '@testing-library/react';
22
import NoteList from '../NoteList';
33

4+
const deleteNoteCallback = jest.fn();
5+
6+
const defaultProps = {
7+
notes: [],
8+
deleteNoteCallback: deleteNoteCallback
9+
};
10+
11+
const setup = (props = {}) => {
12+
const setupProps = { ...defaultProps, ...props};
13+
return render(<NoteList {...setupProps}/>);
14+
};
15+
416
test('should display nothing when no notes are provided', () => {
5-
render(<NoteList notes={[]} />)
17+
setup();
618
const firstNoteName = screen.queryByTestId('test-name-0');
719

820
expect(firstNoteName).toBeNull()
921
});
1022

1123
test('should display one note when one notes is provided', () => {
1224
const note = {name: 'test name', description: 'test description'}
13-
render(<NoteList notes={[note]} />)
25+
setup({notes: [note]});
1426

1527
const firstNoteName = screen.queryByTestId('test-name-0');
1628
expect(firstNoteName).toHaveTextContent("test name");
@@ -22,7 +34,7 @@ test('should display one note when one notes is provided', () => {
2234
test('should display one note when one notes is provided', () => {
2335
const firstNote = {name: 'test name 1', description: 'test description 1'}
2436
const secondNote = {name: 'test name 1', description: 'test description 1'}
25-
render(<NoteList notes={[firstNote, secondNote]} />)
37+
setup({notes: [firstNote, secondNote]});
2638

2739
const firstNoteName = screen.queryByTestId('test-name-0');
2840
expect(firstNoteName).toHaveTextContent("test name");
@@ -38,6 +50,22 @@ test('should display one note when one notes is provided', () => {
3850
expect(secondNoteDescription).toHaveTextContent("test description");
3951
});
4052

53+
test('should delete note when clicked', () => {
54+
const note = {
55+
id: 1,
56+
name: 'test name 1',
57+
description: 'test description 1'
58+
}
59+
const notes = [ note ]
60+
setup({notes: notes});
61+
const button = screen.getByTestId('test-button-0');
62+
63+
fireEvent.click(button)
64+
65+
expect(deleteNoteCallback.mock.calls.length).toBe(1);
66+
expect(deleteNoteCallback.mock.calls[0][0]).toStrictEqual(1);
67+
});
68+
4169
test('should throw an exception the note array is undefined', () => {
4270
expect(() => {render(<NoteList />)}).toThrowError();
4371
});

0 commit comments

Comments
 (0)