Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #182 from ictsc/feature/add_usergroup_hook_test
Browse files Browse the repository at this point in the history
add: userGroups の hooks テストを追加
  • Loading branch information
K-shir0 committed Jul 6, 2023
2 parents 8a8cc57 + 6acdb5b commit 2e29f6c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions __test__/hooks/userGroups.test.ts
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);
});
});
1 change: 1 addition & 0 deletions hooks/userGroups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UserGroup } from "@/types/UserGroup";
const useUserGroups = () => {
const { client } = useApi();

/* c8 ignore next */
const fetcher = (url: string) => client.get<UserGroup[]>(url);

const { data, isLoading } = useSWR("usergroups", fetcher);
Expand Down

0 comments on commit 2e29f6c

Please sign in to comment.