Skip to content

Commit

Permalink
Test(mogua-station#123): getUserProfile 테스트 수정
Browse files Browse the repository at this point in the history
- 옵션 처리 -> Next.js 옵션 처리로 변경
- 중첩된 describe 블록을 제거하고 각 테스트를 독립적으로 재구성
  • Loading branch information
Stilllee committed Feb 5, 2025
1 parent b0e0018 commit 56a5fab
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions src/tests/user/getUserProfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,55 @@ describe("getUserProfile", () => {
jest.spyOn(console, "error").mockImplementation(() => {});
});

describe("프로필 조회 성공", () => {
it("유저 프로필을 성공적으로 가져온다", async () => {
(fetcher as jest.Mock).mockResolvedValue({
ok: true,
json: () => Promise.resolve({ data: mockUserProfile }),
});

const userId = "1";
const data = await getUserProfile(userId);

expect(fetcher).toHaveBeenCalledWith(`/user/profile/${userId}`, "", {});
expect(data).toEqual(mockUserProfile);
it("Next.js fetch 옵션을 전달할 수 있다", async () => {
(fetcher as jest.Mock).mockResolvedValue({
ok: true,
json: () => Promise.resolve({ data: mockUserProfile }),
});

it("추가 옵션을 전달할 수 있다", async () => {
(fetcher as jest.Mock).mockResolvedValue({
ok: true,
json: () => Promise.resolve({ data: mockUserProfile }),
});
const options = {
cache: "no-store" as const,
next: { revalidate: 60 },
};

const options = { headers: { "Cache-Control": "no-cache" } };
await getUserProfile("1", options);
await getUserProfile("1", options);

expect(fetcher).toHaveBeenCalledWith(`/user/profile/1`, "", {
...options,
});
});
expect(fetcher).toHaveBeenCalledWith(
`/user/profile/1`,
"",
expect.objectContaining(options),
);
});

describe("프로필 조회 실패", () => {
it("API 요청이 실패하면 에러를 던진다", async () => {
const errorResponse = {
status: "error",
data: null,
message: "해당 유저는 존재하지 않습니다.",
additionalData: null,
};
it("유저 프로필을 성공적으로 가져온다", async () => {
(fetcher as jest.Mock).mockResolvedValue({
ok: true,
json: () => Promise.resolve({ data: mockUserProfile }),
});

const userId = "1";
const data = await getUserProfile(userId);

expect(fetcher).toHaveBeenCalledWith(`/user/profile/${userId}`, "", {});
expect(data).toEqual(mockUserProfile);
});

(fetcher as jest.Mock).mockResolvedValue({
ok: false,
status: 400,
json: () => Promise.resolve(errorResponse),
});
it("API 요청이 실패하면 에러를 던진다", async () => {
const errorResponse = {
status: "error",
data: null,
message: "해당 유저는 존재하지 않습니다.",
additionalData: null,
};

await expect(getUserProfile("999")).rejects.toThrow(
"해당 유저는 존재하지 않습니다.",
);
(fetcher as jest.Mock).mockResolvedValue({
ok: false,
status: 400,
json: () => Promise.resolve(errorResponse),
});

await expect(getUserProfile("999")).rejects.toThrow(
"해당 유저는 존재하지 않습니다.",
);
});
});

0 comments on commit 56a5fab

Please sign in to comment.