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 #178 from ictsc/feature/add_answer_test
Browse files Browse the repository at this point in the history
add: Answer の hooks テストを追加
  • Loading branch information
K-shir0 authored Jul 6, 2023
2 parents a1023ff + 4add844 commit b88c9ca
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 16 deletions.
103 changes: 103 additions & 0 deletions __test__/hooks/answer.test.ts
Original file line number Diff line number Diff line change
@@ -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<AnswerResult> = {
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<AnswerResult> = {
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<AnswerResult> = {
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);
});
});
4 changes: 1 addition & 3 deletions hooks/answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
12 changes: 11 additions & 1 deletion types/Answer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserGroup } from "@/types/UserGroup";
import { testUserGroup, UserGroup } from "@/types/UserGroup";

export type Answer = {
id: string;
Expand All @@ -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",
};
13 changes: 2 additions & 11 deletions types/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserGroup } from "@/types/UserGroup";
import { testUserGroup, UserGroup } from "@/types/UserGroup";

export type User = {
id: string;
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions types/UserGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
2 changes: 1 addition & 1 deletion types/_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export type ProblemResult = {

// /api/problems/:id/answers
export type AnswerResult = {
answer: Answer;
answers: Answer[];
};

0 comments on commit b88c9ca

Please sign in to comment.