-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
182 additions
and
677 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { HttpResponse, http } from "msw"; | ||
import { server } from "../../test/mocks/server"; | ||
import { SERVER_URL, Todo } from "./todosApi"; | ||
import { mockTodo, mockTodoList } from "../../test/mocks/constants"; | ||
import { | ||
addTodoMutation, | ||
deleteTodoMutation, | ||
updateTodoMutation, | ||
} from "./mutations"; | ||
|
||
describe("mutations", () => { | ||
it("should return the added todo item", async () => { | ||
server.use( | ||
http.post(`${SERVER_URL}`, () => { | ||
return HttpResponse.json(mockTodo, { status: 200 }); | ||
}) | ||
); | ||
const todos = mockTodoList; | ||
const todos1 = await addTodoMutation(mockTodo, todos); | ||
expect(todos1).toEqual([mockTodo, ...todos]); | ||
}); | ||
|
||
it("should return the updated todo item", async () => { | ||
server.use( | ||
http.put(`${SERVER_URL}/:id`, async ({ request }) => { | ||
const { id, userId, title, completed } = (await request.json()) as Todo; | ||
return HttpResponse.json( | ||
{ | ||
userId, | ||
title, | ||
completed, | ||
id, | ||
}, | ||
{ status: 200 } | ||
); | ||
}) | ||
); | ||
const todos = [mockTodo, ...mockTodoList]; | ||
const todos1 = await updateTodoMutation( | ||
{ ...mockTodo, title: "123" }, | ||
todos | ||
); | ||
expect(todos1).toEqual([{ ...mockTodo, title: "123" }, ...mockTodoList]); | ||
}); | ||
|
||
it("should return the deleted todo id", async () => { | ||
server.use( | ||
http.delete(`${SERVER_URL}/:id`, () => { | ||
return HttpResponse.json(mockTodo, { status: 200 }); | ||
}) | ||
); | ||
const todos = [mockTodo, ...mockTodoList]; | ||
const todos1 = await deleteTodoMutation(mockTodo.id, todos); | ||
expect(todos1).toEqual(mockTodoList); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,33 @@ | ||
import { Todo, addTodo, deleteTodo, updateTodo } from "./todosApi"; | ||
|
||
export async function addTodoMutation(newTodo: Todo, todos: Todo[]) { | ||
const added = await addTodo(newTodo); | ||
export const addTodoUtility = (added: Todo, todos: Todo[]) => { | ||
return [added, ...todos]; | ||
} | ||
}; | ||
|
||
export function addTodoMutationOptions(newTodo: Todo, todos: Todo[]) { | ||
return { | ||
optimisticData: [newTodo, ...todos], | ||
revalidate: false, | ||
}; | ||
export async function addTodoMutation(addedTodo: Todo, todos: Todo[]) { | ||
const added = await addTodo(addedTodo); | ||
return addTodoUtility(added, todos); | ||
} | ||
|
||
export async function updateTodoMutation(updatedTodo: Todo, todos: Todo[]) { | ||
const updated = await updateTodo(updatedTodo); | ||
export const updateTodoUtility = (updated: Todo, todos: Todo[]) => { | ||
return todos.map((todo) => { | ||
return updatedTodo.id === todo.id ? updated : todo; | ||
return updated.id === todo.id ? updated : todo; | ||
}); | ||
} | ||
}; | ||
|
||
export function updateTodoMutationOptions(updatedTodo: Todo, todos: Todo[]) { | ||
return { | ||
optimisticData: todos.map((todo) => { | ||
return updatedTodo.id === todo.id ? updatedTodo : todo; | ||
}), | ||
revalidate: false, | ||
}; | ||
export async function updateTodoMutation(updatedTodo: Todo, todos: Todo[]) { | ||
const response = await updateTodo(updatedTodo); | ||
return updateTodoUtility(response, todos); | ||
} | ||
|
||
export async function deleteTodoMutation(id: number, todos: Todo[]) { | ||
await deleteTodo(id); | ||
export const deleteTodoUtility = (id: number, todos: Todo[]) => { | ||
return todos.filter((todo) => { | ||
return todo.id !== id; | ||
}); | ||
} | ||
}; | ||
|
||
export function deleteTodoMutationOptions(id: number, todos: Todo[]) { | ||
return { | ||
optimisticData: todos.filter((todo) => { | ||
return todo.id !== id; | ||
}), | ||
revalidate: false, | ||
}; | ||
export async function deleteTodoMutation(id: number, todos: Todo[]) { | ||
await deleteTodo(id); | ||
return deleteTodoUtility(id, todos); | ||
} | ||
|
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
export const mockTodo = { | ||
userId: 1, | ||
title: "Wave hello! 👋", | ||
title: "A", | ||
completed: false, | ||
id: 1, | ||
}; | ||
|
||
export const mockTodoList = [ | ||
{ | ||
userId: 2, | ||
title: "B", | ||
completed: false, | ||
id: 2, | ||
}, | ||
{ | ||
userId: 3, | ||
title: "C", | ||
completed: false, | ||
id: 3, | ||
}, | ||
]; |
Oops, something went wrong.