|
1 | | -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { updateMemberName, updatePetInfo } from "@/api/client/settingsApiActions"; |
3 | 3 |
|
4 | | -vi.mock("next/headers", () => ({ |
5 | | - cookies: vi.fn(async () => ({ |
6 | | - get: vi.fn(() => undefined), |
7 | | - })), |
8 | | -})); |
| 4 | +const mockPatch = vi.fn(); |
| 5 | +const mockPut = vi.fn(); |
9 | 6 |
|
10 | | -const fetchMock = vi.fn(); |
11 | | -const originalFetch = global.fetch; |
| 7 | +vi.mock("@/api/lib/client", () => ({ |
| 8 | + client: { |
| 9 | + PATCH: (...args: unknown[]) => mockPatch(...args), |
| 10 | + PUT: (...args: unknown[]) => mockPut(...args), |
| 11 | + }, |
| 12 | +})); |
12 | 13 |
|
13 | 14 | describe("settingsApiActions", () => { |
14 | 15 | beforeEach(() => { |
15 | | - fetchMock.mockReset(); |
16 | | - global.fetch = fetchMock; |
17 | | - }); |
18 | | - |
19 | | - afterEach(() => { |
20 | | - global.fetch = originalFetch; |
| 16 | + vi.clearAllMocks(); |
21 | 17 | }); |
22 | 18 |
|
23 | 19 | it("updateMemberName: 성공 시 응답 데이터를 반환한다", async () => { |
24 | | - const payload = { memberName: "멍멍이", memberEmail: "test@example.com" }; |
25 | | - fetchMock.mockResolvedValueOnce(new Response(JSON.stringify(payload), { status: 200 })); |
| 20 | + const payload = { |
| 21 | + memberId: 1, |
| 22 | + memberName: "멍멍이", |
| 23 | + memberImageUrl: "https://example.com/image.jpg", |
| 24 | + }; |
| 25 | + mockPatch.mockResolvedValueOnce({ |
| 26 | + data: payload, |
| 27 | + response: new Response(JSON.stringify(payload), { status: 200 }), |
| 28 | + }); |
26 | 29 |
|
27 | 30 | await expect(updateMemberName("멍멍이")).resolves.toEqual(payload); |
28 | 31 | }); |
29 | 32 |
|
30 | 33 | it("updateMemberName: 실패 시 상태코드 기반 메시지를 throw한다", async () => { |
31 | | - fetchMock.mockResolvedValueOnce( |
32 | | - new Response(JSON.stringify({ message: "닉네임 형식이 올바르지 않습니다." }), { |
33 | | - status: 400, |
34 | | - }), |
35 | | - ); |
| 34 | + mockPatch.mockRejectedValueOnce(new Error("요청 값이 올바르지 않습니다.")); |
36 | 35 |
|
37 | 36 | await expect(updateMemberName("")).rejects.toThrow("요청 값이 올바르지 않습니다."); |
38 | 37 | }); |
39 | 38 |
|
40 | 39 | it("updatePetInfo: 실패 응답 본문이 없으면 상태코드 메시지를 throw한다", async () => { |
41 | | - fetchMock.mockResolvedValueOnce(new Response("", { status: 500 })); |
| 40 | + mockPut.mockRejectedValueOnce( |
| 41 | + new Error("서버 오류가 발생했습니다. 잠시 후 다시 시도해 주세요."), |
| 42 | + ); |
42 | 43 |
|
43 | 44 | await expect(updatePetInfo({} as never)).rejects.toThrow( |
44 | 45 | "서버 오류가 발생했습니다. 잠시 후 다시 시도해 주세요.", |
45 | 46 | ); |
46 | 47 | }); |
47 | 48 |
|
48 | 49 | it("updatePetInfo: 성공이지만 본문이 없으면 undefined를 반환한다", async () => { |
49 | | - fetchMock.mockResolvedValueOnce(new Response("", { status: 200 })); |
| 50 | + mockPut.mockResolvedValueOnce({ |
| 51 | + data: undefined, |
| 52 | + response: new Response("", { status: 200 }), |
| 53 | + }); |
50 | 54 |
|
51 | 55 | await expect(updatePetInfo({} as never)).resolves.toBeUndefined(); |
52 | 56 | }); |
|
0 commit comments