Skip to content

Commit c171007

Browse files
committed
added the ability to delete a note
1 parent 09c713a commit c171007

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,4 +1697,64 @@ describe('Note Capture', () => {
16971697
16981698
[Code for this section](https://github.com/pairing4good/tdd-amplify-react/commit/f6ee8a279908c49d6d03ccb7f209b4833832c1e6)
16991699
1700+
</details>
1701+
1702+
<details>
1703+
<summary>Add Note Deletion</summary>
1704+
1705+
## Add Note Deletion
1706+
In order to add note deletion, let's drive this from the Cypress test. This will help in cleaning up notes that were created during the UI test.
1707+
1708+
- Add a deletion test to the Cypress test
1709+
```js
1710+
it('should delete note', () => {
1711+
cy.get('[data-testid=test-button-0]').click();
1712+
1713+
cy.get('[data-testid=test-name-0]').should('not.exist')
1714+
cy.get('[data-testid=test-description-0]').should('not.exist')
1715+
})
1716+
```
1717+
- Run the Cypress test and verify that it Fails
1718+
1719+
- To make it go green, add a new deletion function to `NoteRepository.js`
1720+
```js
1721+
...
1722+
import { createNote as createNoteMutation, deleteNote as deleteNoteMutation} from './graphql/mutations';
1723+
1724+
...
1725+
1726+
export async function deleteById( id ) {
1727+
return await API.graphql({ query: deleteNoteMutation, variables: { input: { id } }});
1728+
}
1729+
```
1730+
1731+
- Create a new deletion callback function in `App.js`
1732+
```js
1733+
async function deleteNoteCallback( id ) {
1734+
const newNotesArray = notes.filter(note => note.id !== id);
1735+
setNotes(newNotesArray);
1736+
await deleteById(id);
1737+
}
1738+
```
1739+
1740+
- Pass the `deleteNoteCallback` callback function parameter to the `NoteList` component.
1741+
```js
1742+
<NoteList notes={notes}
1743+
deleteNoteCallback={deleteNoteCallback}/>
1744+
```
1745+
1746+
- Add a deletion button to the `NoteList` component
1747+
```js
1748+
<button
1749+
data-testid={'test-button-' + index}
1750+
onClick={() => props.deleteNoteCallback(note.id)}>
1751+
Delete note
1752+
</button>
1753+
```
1754+
- Run all the tests
1755+
- Green
1756+
- Commit
1757+
1758+
[Code for this section]()
1759+
17001760
</details>

cypress/integration/note.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ describe('Note Capture', () => {
4343

4444
})
4545

46+
it('should delete note', () => {
47+
cy.get('[data-testid=test-button-0]').click();
48+
49+
cy.get('[data-testid=test-name-0]').should('not.exist')
50+
cy.get('[data-testid=test-description-0]').should('not.exist')
51+
})
52+
4653
it('should have an option to sign out', () => {
4754
cy.get('[data-testid=sign-out] > .hydrated').click()
4855
cy.get('amplify-auth-container.hydrated > .hydrated').should('exist')

src/App.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import NoteForm from './NoteForm';
33
import React, { useState, useEffect } from 'react';
44
import NoteList from './NoteList';
55
import Header from './Header';
6-
import { findAll, save } from './NoteRepository';
6+
import { findAll, save, deleteById } from './NoteRepository';
77
import { withAuthenticator } from '@aws-amplify/ui-react'
88
import Footer from './Footer';
99

@@ -29,14 +29,22 @@ function App() {
2929
setNotes(updatedNoteList);
3030
}
3131

32+
33+
async function deleteNoteCallback( id ) {
34+
const newNotesArray = notes.filter(note => note.id !== id);
35+
setNotes(newNotesArray);
36+
await deleteById(id);
37+
}
38+
3239
return (
3340
<div className="App">
3441
<Header />
3542
<NoteForm notes={notes}
3643
formData={formData}
3744
setFormDataCallback={setFormData}
3845
createNoteCallback={createNote}/>
39-
<NoteList notes={notes}/>
46+
<NoteList notes={notes}
47+
deleteNoteCallback={deleteNoteCallback}/>
4048
<Footer />
4149
</div>
4250
);

src/NoteList.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ function NoteList(props) {
66
props.notes.map((note, index) => (
77
<div key={'note-' + index}>
88
<p data-testid={"test-name-" + index}>{note.name}</p>
9-
<p data-testid={"test-description-" + index}>{note.description}</p>
9+
<p data-testid={"test-description-" + index}>{note.description}</p>
10+
<button
11+
data-testid={'test-button-' + index}
12+
onClick={() => props.deleteNoteCallback(note.id)}>
13+
Delete note
14+
</button>
1015
</div>
1116
))
1217
}

src/NoteRepository.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { API } from 'aws-amplify';
22
import { listNotes } from './graphql/queries';
3-
import { createNote as createNoteMutation} from './graphql/mutations';
3+
import { createNote as createNoteMutation, deleteNote as deleteNoteMutation} from './graphql/mutations';
44

55
export async function findAll(){
66
const apiData = await API.graphql({ query: listNotes });
@@ -10,4 +10,8 @@ export async function findAll(){
1010
export async function save(note){
1111
const apiData = await API.graphql({ query: createNoteMutation, variables: { input: note } });
1212
return apiData.data.createNote;
13+
}
14+
15+
export async function deleteById( id ) {
16+
return await API.graphql({ query: deleteNoteMutation, variables: { input: { id } }});
1317
}

0 commit comments

Comments
 (0)