-
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.
test(useAddFavorite): add unit test for addFavorite mutation and erro…
…r handling
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...ponents/Books/components/BookCard/components/FavoriteButton/hooks/useAddFavorite.test.tsx
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,67 @@ | ||
import { InMemoryCache, useMutation } from "@apollo/client"; | ||
import { renderHook, act } from "@testing-library/react"; | ||
import { MockedProvider } from "@apollo/client/testing"; | ||
import { ReactNode } from "react"; | ||
import { vi } from "vitest"; | ||
|
||
import { useAddFavorite } from "./useAddFavorite"; | ||
|
||
vi.mock("@apollo/client"); | ||
describe("useAddFavorite", () => { | ||
const bookId = "abc123"; | ||
const cache = new InMemoryCache(); | ||
const addFavoriteMock = vi.fn(); | ||
|
||
const wrapper = ({ children }: { children: ReactNode }) => ( | ||
<MockedProvider cache={cache}>{children}</MockedProvider> | ||
); | ||
|
||
it("adds favorite when called", async () => { | ||
(useMutation as jest.Mock).mockImplementation(() => { | ||
return [ | ||
addFavoriteMock, | ||
{ loading: false, error: undefined, data: undefined }, | ||
]; | ||
}); | ||
const { result } = renderHook(() => useAddFavorite(), { wrapper }); | ||
|
||
act(() => { | ||
result.current.addFavorite({ variables: { bookId } }); | ||
}); | ||
|
||
// Check that the mutation was called with the correct variables | ||
expect(addFavoriteMock).toHaveBeenCalledWith({ | ||
variables: { bookId }, | ||
}); | ||
// Check that the mutation did not result in an error | ||
expect(result.current.addError).toBeUndefined(); | ||
}); | ||
|
||
it("handles loading state", () => { | ||
const mutationResult = { loading: true, error: undefined, data: undefined }; | ||
(useMutation as jest.Mock).mockReturnValue([ | ||
addFavoriteMock, | ||
mutationResult, | ||
]); | ||
|
||
const { result } = renderHook(() => useAddFavorite(), { wrapper }); | ||
|
||
expect(result.current.addLoading).toBe(true); | ||
}); | ||
|
||
it("handles error state", () => { | ||
const mutationResult = { | ||
loading: false, | ||
error: new Error("Mutation error"), | ||
data: undefined, | ||
}; | ||
(useMutation as jest.Mock).mockReturnValue([ | ||
addFavoriteMock, | ||
mutationResult, | ||
]); | ||
|
||
const { result } = renderHook(() => useAddFavorite(), { wrapper }); | ||
|
||
expect(result.current.addError).toEqual(new Error("Mutation error")); | ||
}); | ||
}); |
2e3c4ec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Deployed on https://65aeea0dddd90e5c5f7a97a5--aesthetic-jalebi-84d3c0.netlify.app