This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
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.
Merge pull request #179 from ictsc/feature/add_notice_test
add: notice の hooks テストを追加
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
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,66 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import useSWR from "swr"; | ||
import { Mock, vi } from "vitest"; | ||
|
||
import useNotice from "@/hooks/notice"; | ||
import { Notice, testNotice } from "@/types/Notice"; | ||
import { Result } from "@/types/_api"; | ||
|
||
vi.mock("swr"); | ||
|
||
beforeEach(() => { | ||
// toHaveBeenCalledTimes がテストごとにリセットされるようにする | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("useNotice", () => { | ||
it("通知一覧が取得できる", () => { | ||
// setup | ||
const mockResult: Result<Notice[]> = { | ||
code: 200, | ||
data: [testNotice], | ||
}; | ||
|
||
(useSWR as Mock).mockReturnValue({ | ||
data: mockResult, | ||
mutate: vi.fn(), | ||
isLoading: false, | ||
}); | ||
|
||
// when | ||
const { result } = renderHook(() => useNotice()); | ||
|
||
// then | ||
expect(useSWR).toBeCalledWith("notices", expect.any(Function)); | ||
expect(result.current.notices).toEqual(mockResult.data); | ||
expect(result.current.mutate).toBeDefined(); | ||
|
||
// verify | ||
expect(useSWR).toBeCalledTimes(1); | ||
}); | ||
|
||
it("取得した通知一覧が空の時 notices が空になる", () => { | ||
// setup | ||
const mockResult: Result<Notice[]> = { | ||
code: 200, | ||
data: null, | ||
}; | ||
|
||
(useSWR as Mock).mockReturnValue({ | ||
data: mockResult, | ||
mutate: vi.fn(), | ||
isLoading: false, | ||
}); | ||
|
||
// when | ||
const { result } = renderHook(() => useNotice()); | ||
|
||
// then | ||
expect(useSWR).toBeCalledWith("notices", expect.any(Function)); | ||
expect(result.current.notices).toBeNull(); | ||
expect(result.current.mutate).toBeDefined(); | ||
|
||
// verify | ||
expect(useSWR).toBeCalledTimes(1); | ||
}); | ||
}); |
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 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