Skip to content

Commit c8b56ae

Browse files
authored
feat: add PATCH support (#2)
1 parent cafef3b commit c8b56ae

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: ci
22
on: push
33
jobs:
4-
lint:
4+
test:
55
runs-on: ubuntu-24.04
66
steps:
77
- name: Checkout 🛎

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('Todos', { rest: { todos: 'todos.json' } }, () => {
2525
// GET /todos
2626
// POST /todos
2727
// DELETE /todos/:id
28+
// PATCH /todos/:id
2829
```
2930
3031
See [todos.cy.js](./cypress/e2e/todos.cy.js) for examples

app/app.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ function appStart() {
4747
let todos = state.todos
4848
todos.splice(todos.indexOf(todo), 1)
4949
},
50+
UPDATE_TODO(state, updatedTodo) {
51+
const index = state.todos.findIndex(
52+
(todo) => todo.id === updatedTodo.id,
53+
)
54+
if (index !== -1) {
55+
state.todos.splice(index, 1, updatedTodo)
56+
}
57+
},
5058
CLEAR_NEW_TODO(state) {
5159
state.newTodo = ''
5260
},
@@ -121,6 +129,11 @@ function appStart() {
121129
}
122130
commit('SET_TODOS', remainingTodos)
123131
},
132+
updateTodo({ commit }, todo) {
133+
axios.patch(`/todos/${todo.id}`, todo).then(() => {
134+
commit('UPDATE_TODO', todo)
135+
})
136+
},
124137
clearNewTodo({ commit }) {
125138
commit('CLEAR_NEW_TODO')
126139
},

app/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ <h1 data-cy="app-title">todos</h1>
6868
<input
6969
class="toggle"
7070
type="checkbox"
71-
v-model="todo.completed"
71+
:checked="todo.completed"
72+
@change="$store.dispatch('updateTodo', { ...todo, completed: !todo.completed })"
7273
/>
7374
<label>{{ todo.title }}</label>
7475
<button

cypress/e2e/todos.cy.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ describe('Todos', { rest: { todos: 'todos.json' } }, () => {
4242
.to.be.an('array')
4343
.and.have.length(4)
4444
})
45+
46+
cy.log('reload the page')
47+
cy.reload()
48+
cy.get('li.todo').should('have.length', 4)
4549
})
4650

4751
it('allows accessing individual items', () => {
@@ -57,4 +61,28 @@ describe('Todos', { rest: { todos: 'todos.json' } }, () => {
5761
expect(todos).to.be.an('array').and.have.length(2)
5862
})
5963
})
64+
65+
it('supports PATCH', () => {
66+
cy.get('li.todo').first().find('.toggle').click()
67+
cy.get('li.todo').first().should('have.class', 'completed')
68+
cy.wait('@patchTodos')
69+
.its('response.statusCode')
70+
.should('eq', 204)
71+
cy.get('@patchTodos')
72+
.its('request.body')
73+
.should('deep.include', {
74+
completed: true,
75+
})
76+
.then(() => {
77+
const todos = Cypress.env('todos')
78+
expect(todos, 'todos array')
79+
.to.be.an('array')
80+
.and.have.length(3)
81+
expect(todos[0].completed, 'first todo completed').to.be.true
82+
})
83+
84+
cy.log('reload the page')
85+
cy.reload()
86+
cy.get('li.todo').first().should('have.class', 'completed')
87+
})
6088
})

src/index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ beforeEach(function prepareRestApi() {
1212
// conver the first letter to uppercase
1313
const resourceNameCapitalized =
1414
resourceName.charAt(0).toUpperCase() + resourceName.slice(1)
15-
cy.intercept('GET', resourceName, data).as(
16-
`get${resourceNameCapitalized}`,
17-
)
15+
16+
// GET resources
17+
cy.intercept('GET', resourceName, (req) =>
18+
req.reply(200, data),
19+
).as(`get${resourceNameCapitalized}`)
20+
21+
// TODO: add GET /:id
22+
23+
// POST resources
1824
cy.intercept('POST', resourceName, (req) => {
1925
const item = req.body
2026
// modify the id?
@@ -32,6 +38,21 @@ beforeEach(function prepareRestApi() {
3238
}
3339
req.reply(204)
3440
}).as(`delete${resourceNameCapitalized}`)
41+
42+
// PATCH resources
43+
cy.intercept('PATCH', resourceName + '/*', (req) => {
44+
const id = req.url.split('/').pop()
45+
const index = data.findIndex((item) => item.id === id)
46+
if (index === -1) {
47+
return req.reply(404)
48+
}
49+
if (index >= data.length) {
50+
return req.reply(404)
51+
}
52+
53+
Object.assign(data[index], req.body)
54+
req.reply(204)
55+
}).as(`patch${resourceNameCapitalized}`)
3556
})
3657
})
3758
})

0 commit comments

Comments
 (0)