Skip to content

Commit 52ed6bb

Browse files
committed
test: verify learning status through mocked fetch
1 parent b6d4e90 commit 52ed6bb

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

โ€Žhandlers/learning-status.test.jsโ€Ž

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@ vi.mock("../utils/openai.js", () => ({
1010
generateApproachAnalysis: vi.fn(),
1111
}));
1212

13-
vi.mock("../utils/learningComment.js", async (importOriginal) => ({
14-
...(await importOriginal()),
15-
upsertLearningStatusComment: vi.fn(),
16-
}));
17-
1813
import { postLearningStatus } from "./learning-status.js";
1914
import {
2015
fetchProblemCategories,
2116
fetchCohortUserSolutions,
2217
fetchPRSubmissions,
2318
} from "../utils/learningData.js";
2419
import { generateApproachAnalysis } from "../utils/openai.js";
25-
import { upsertLearningStatusComment } from "../utils/learningComment.js";
2620

2721
describe("postLearningStatus โ€” ๋ˆ„์  ํ’€์ด ์ง‘๊ณ„", () => {
2822
let originalFetch;
@@ -44,10 +38,17 @@ describe("postLearningStatus โ€” ๋ˆ„์  ํ’€์ด ์ง‘๊ณ„", () => {
4438
matches: true,
4539
explanation: "Uses a hash table",
4640
});
47-
globalThis.fetch = vi.fn().mockResolvedValue({
48-
ok: true,
49-
text: () => Promise.resolve("class Solution: pass"),
50-
});
41+
globalThis.fetch = vi
42+
.fn()
43+
.mockResolvedValueOnce({
44+
ok: true,
45+
text: () => Promise.resolve("class Solution: pass"),
46+
})
47+
.mockResolvedValueOnce({
48+
ok: true,
49+
json: () => Promise.resolve([]),
50+
})
51+
.mockResolvedValueOnce({ ok: true });
5152
});
5253

5354
afterEach(() => {
@@ -82,13 +83,12 @@ describe("postLearningStatus โ€” ๋ˆ„์  ํ’€์ด ์ง‘๊ณ„", () => {
8283
{}
8384
);
8485

85-
expect(upsertLearningStatusComment).toHaveBeenCalledWith(
86-
"DaleStudy",
87-
"leetcode-study",
88-
42,
89-
expect.stringContaining(`ํ’€์ดํ•œ ๋ฌธ์ œ: ${expected} / 1๊ฐœ`),
90-
"token",
91-
null
86+
const post = globalThis.fetch.mock.calls.find(
87+
([, options]) => options?.method === "POST"
9288
);
89+
expect(post).toBeDefined();
90+
91+
const body = JSON.parse(post[1].body).body;
92+
expect(body).toContain(`ํ’€์ดํ•œ ๋ฌธ์ œ: ${expected} / 1๊ฐœ`);
9393
});
9494
});

0 commit comments

Comments
ย (0)