90 lines
3.3 KiB
TypeScript
90 lines
3.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const apiPost = vi.fn();
|
|
const apiPatch = vi.fn();
|
|
const apiDelete = vi.fn();
|
|
const apiGet = vi.fn();
|
|
|
|
vi.mock("./axios", () => ({
|
|
apiGet,
|
|
apiPost,
|
|
apiPatch,
|
|
apiDelete,
|
|
}));
|
|
|
|
describe("faqs category api", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("passes study_id as a query parameter for category write permission checks", async () => {
|
|
const { createFaqCategory, updateFaqCategory, deleteFaqCategory } = await import("./faqs");
|
|
const payload = { study_id: "study-1", name: "用药咨询" };
|
|
|
|
createFaqCategory(payload);
|
|
updateFaqCategory("category-1", payload);
|
|
deleteFaqCategory("category-1", "study-1");
|
|
|
|
expect(apiPost).toHaveBeenCalledWith("/api/v1/faqs/categories/", payload, { params: { study_id: "study-1" } });
|
|
expect(apiPatch).toHaveBeenCalledWith("/api/v1/faqs/categories/category-1", payload, {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
expect(apiDelete).toHaveBeenCalledWith("/api/v1/faqs/categories/category-1", {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("faqs item api", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("passes study_id as a query parameter for item write permission checks", async () => {
|
|
const { createFaqItem, updateFaqItem, deleteFaqItem } = await import("./faqs");
|
|
const payload = { study_id: "study-1", category_id: "category-1", question: "是否需要空腹用药?" };
|
|
|
|
createFaqItem(payload);
|
|
updateFaqItem("item-1", payload);
|
|
deleteFaqItem("item-1", "study-1");
|
|
|
|
expect(apiPost).toHaveBeenCalledWith("/api/v1/faqs/items/", payload, { params: { study_id: "study-1" } });
|
|
expect(apiPatch).toHaveBeenCalledWith("/api/v1/faqs/items/item-1", payload, {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
expect(apiDelete).toHaveBeenCalledWith("/api/v1/faqs/items/item-1", {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
});
|
|
|
|
it("passes study_id as a query parameter for item detail permission checks", async () => {
|
|
const { fetchFaqItem, fetchFaqReplies, createFaqReply, deleteFaqReply, setFaqBestReply, setFaqStatus } = await import("./faqs");
|
|
|
|
fetchFaqItem("item-1", "study-1");
|
|
fetchFaqReplies("item-1", "study-1");
|
|
createFaqReply("item-1", "study-1", { content: "建议复查。" });
|
|
deleteFaqReply("item-1", "reply-1", "study-1");
|
|
setFaqBestReply("item-1", "study-1", { best_reply_id: "reply-1" });
|
|
setFaqStatus("item-1", "study-1", { status: "RESOLVED" });
|
|
|
|
expect(apiGet).toHaveBeenCalledWith("/api/v1/faqs/items/item-1", {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
expect(apiGet).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/replies", {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
expect(apiPost).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/replies", { content: "建议复查。" }, {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
expect(apiDelete).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/replies/reply-1", {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
expect(apiPatch).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/best-reply", { best_reply_id: "reply-1" }, {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
expect(apiPatch).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/status", { status: "RESOLVED" }, {
|
|
params: { study_id: "study-1" },
|
|
});
|
|
});
|
|
});
|