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 #182 from ictsc/feature/add_usergroup_hook_test
add: userGroups の hooks テストを追加
- Loading branch information
Showing
2 changed files
with
65 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,64 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import useSWR from "swr"; | ||
import { Mock, vi } from "vitest"; | ||
|
||
import useUserGroups from "@/hooks/userGroups"; | ||
import { testUserGroup, UserGroup } from "@/types/UserGroup"; | ||
import { Result } from "@/types/_api"; | ||
|
||
vi.mock("swr"); | ||
|
||
beforeEach(() => { | ||
// toHaveBeenCalledTimes がテストごとにリセットされるようにする | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("useUserGroups", () => { | ||
it("ユーザーグループ一覧を取得できる", () => { | ||
// setup | ||
const mockResult: Result<UserGroup[]> = { | ||
code: 200, | ||
data: [testUserGroup], | ||
}; | ||
|
||
(useSWR as Mock).mockReturnValue({ | ||
data: mockResult, | ||
isLoading: false, | ||
}); | ||
|
||
// when | ||
const { result } = renderHook(() => useUserGroups()); | ||
|
||
// then | ||
expect(useSWR).toBeCalledWith("usergroups", expect.any(Function)); | ||
expect(result.current.userGroups).toEqual(mockResult.data); | ||
expect(result.current.isLoading).toEqual(false); | ||
|
||
// verify | ||
expect(useSWR).toBeCalledTimes(1); | ||
}); | ||
|
||
it("取得したユーザーグループ一覧が空の時 userGroups が Null になる", () => { | ||
// setup | ||
const mockResult: Result<UserGroup[]> = { | ||
code: 200, | ||
data: null, | ||
}; | ||
|
||
(useSWR as Mock).mockReturnValue({ | ||
data: mockResult, | ||
isLoading: false, | ||
}); | ||
|
||
// when | ||
const { result } = renderHook(() => useUserGroups()); | ||
|
||
// then | ||
expect(useSWR).toBeCalledWith("usergroups", expect.any(Function)); | ||
expect(result.current.userGroups).toBeNull(); | ||
expect(result.current.isLoading).toEqual(false); | ||
|
||
// 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