diff --git a/__test__/hooks/answer.test.ts b/__test__/hooks/answer.test.ts new file mode 100644 index 0000000..2b8a321 --- /dev/null +++ b/__test__/hooks/answer.test.ts @@ -0,0 +1,103 @@ +import { renderHook } from "@testing-library/react"; +import useSWR from "swr"; +import { Mock, vi } from "vitest"; + +import useAnswers from "@/hooks/answer"; +import { testAnswer } from "@/types/Answer"; +import { AnswerResult, Result } from "@/types/_api"; + +vi.mock("swr"); + +beforeEach(() => { + // toHaveBeenCalledTimes がテストごとにリセットされるようにする + vi.clearAllMocks(); +}); + +describe("useAnswer", () => { + it("回答一覧が取得できる", () => { + // setup + const mockResult: Result = { + code: 200, + data: { + answers: [testAnswer], + }, + }; + + (useSWR as Mock).mockReturnValue({ + data: mockResult, + mutate: vi.fn(), + isLoading: false, + }); + + // when + const { result } = renderHook(() => useAnswers("1")); + + // then + expect(useSWR).toBeCalledWith("problems/1/answers", expect.any(Function)); + expect(result.current.answers).toEqual(mockResult.data?.answers); + expect(result.current.getAnswer("1")).toEqual(testAnswer); + expect(result.current.mutate).toBeDefined(); + + // verify + expect(useSWR).toBeCalledTimes(1); + }); + + it("取得した回答一覧が空の時 answers, getAnswer が空になる", () => { + // setup + const mockResult: Result = { + code: 200, + data: null, + }; + + (useSWR as Mock).mockReturnValue({ + data: mockResult, + mutate: vi.fn(), + isLoading: false, + }); + + // when + const { result } = renderHook(() => useAnswers("1")); + + // then + expect(useSWR).toBeCalledWith("problems/1/answers", expect.any(Function)); + expect(result.current.answers).toEqual([]); + expect(result.current.getAnswer("1")).toBeNull(); + expect(result.current.mutate).toBeDefined(); + + // verify + expect(useSWR).toBeCalledTimes(1); + }); + + it("ProblemId で回答が見つからなかった時 getAnswer が空になる", () => { + // setup + const mockResult: Result = { + code: 200, + data: { + answers: [testAnswer], + }, + }; + + (useSWR as Mock).mockReturnValue({ + data: mockResult, + mutate: vi.fn(), + isLoading: false, + }); + + const id = "unknownId"; + + // when + const { result } = renderHook(() => useAnswers(id)); + + // then + expect(useSWR).toBeCalledWith( + `problems/${id}/answers`, + expect.any(Function) + ); + expect(result.current.answers).toEqual(mockResult.data?.answers); + expect(result.current.getAnswer(id)).toBeNull(); + expect(result.current.mutate).toBeDefined(); + + // verify + expect(useSWR).toBeCalledTimes(1); + }); +}); diff --git a/hooks/answer.ts b/hooks/answer.ts index cd2e780..146b60d 100644 --- a/hooks/answer.ts +++ b/hooks/answer.ts @@ -2,10 +2,8 @@ import useSWR from "swr"; import useApi from "@/hooks/api"; import { Answer } from "@/types/Answer"; +import { AnswerResult } from "@/types/_api"; -type AnswerResult = { - answers: Answer[]; -}; const useAnswers = (problemId: string | null) => { const { client } = useApi(); diff --git a/types/Answer.tsx b/types/Answer.tsx index f6439ca..6754ab0 100644 --- a/types/Answer.tsx +++ b/types/Answer.tsx @@ -1,4 +1,4 @@ -import { UserGroup } from "@/types/UserGroup"; +import { testUserGroup, UserGroup } from "@/types/UserGroup"; export type Answer = { id: string; @@ -9,3 +9,13 @@ export type Answer = { created_at: string; updated_at: string; }; + +export const testAnswer: Answer = { + id: "1", + body: "test", + point: 100, + problem_id: "1", + user_group: testUserGroup, + created_at: "2021-01-01", + updated_at: "2021-01-01", +}; diff --git a/types/User.ts b/types/User.ts index 9380cd3..c25425d 100644 --- a/types/User.ts +++ b/types/User.ts @@ -1,4 +1,4 @@ -import { UserGroup } from "@/types/UserGroup"; +import { testUserGroup, UserGroup } from "@/types/UserGroup"; export type User = { id: string; @@ -69,16 +69,7 @@ export const testAdminUser: User = { is_read_only: false, created_at: new Date(), updated_at: new Date(), - user_group: { - id: "1", - name: "Test User Group", - organization: "Test Organization", - created_at: new Date(), - updated_at: new Date(), - is_full_access: true, - members: [], - bastion: null, - }, + user_group: testUserGroup, user_group_id: "1", profile: { id: "1", diff --git a/types/UserGroup.ts b/types/UserGroup.ts index c952026..e0bb0e7 100644 --- a/types/UserGroup.ts +++ b/types/UserGroup.ts @@ -15,3 +15,14 @@ export type UserGroup = { bastion_port: number; } | null; }; + +export const testUserGroup: UserGroup = { + id: "1", + name: "Test User Group", + organization: "Test Organization", + created_at: new Date(), + updated_at: new Date(), + is_full_access: true, + members: [], + bastion: null, +}; diff --git a/types/_api.ts b/types/_api.ts index 62099f5..b4c48e7 100644 --- a/types/_api.ts +++ b/types/_api.ts @@ -19,5 +19,5 @@ export type ProblemResult = { // /api/problems/:id/answers export type AnswerResult = { - answer: Answer; + answers: Answer[]; };