Migrate Vitest store tests from vi.mock to MSW request handlers
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,20 +5,14 @@
|
||||
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { setActivePinia, createPinia } from "pinia";
|
||||
import { http, HttpResponse } from "msw";
|
||||
import { server } from "@/mocks/node.js";
|
||||
import { findRequest, captureRequest } from "@/mocks/request-log.js";
|
||||
|
||||
vi.mock("@/module/apiError.js", () => ({
|
||||
default: vi.fn(),
|
||||
}));
|
||||
|
||||
const { mockGet, mockPost, mockPut } = vi.hoisted(() => ({
|
||||
mockGet: vi.fn(),
|
||||
mockPost: vi.fn(),
|
||||
mockPut: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/api/client.js", () => ({
|
||||
default: { get: mockGet, post: mockPost, put: mockPut },
|
||||
}));
|
||||
|
||||
import { useConformanceStore } from "@/stores/conformance";
|
||||
|
||||
describe("conformanceStore", () => {
|
||||
@@ -27,7 +21,7 @@ describe("conformanceStore", () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
store = useConformanceStore();
|
||||
vi.clearAllMocks();
|
||||
document.cookie = "luciaToken=fake-test-token";
|
||||
});
|
||||
|
||||
it("has correct default state", () => {
|
||||
@@ -49,11 +43,18 @@ describe("conformanceStore", () => {
|
||||
waiting_time: {},
|
||||
cycle_time: {},
|
||||
};
|
||||
mockGet.mockResolvedValue({ data: mockData });
|
||||
server.use(
|
||||
http.get("/api/log-checks/params", ({ request }) => {
|
||||
const url = new URL(request.url);
|
||||
captureRequest("GET", url.pathname + url.search);
|
||||
return HttpResponse.json(mockData);
|
||||
}),
|
||||
);
|
||||
|
||||
await store.getConformanceParams();
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith("/api/log-checks/params?log_id=1");
|
||||
const reqs = findRequest("GET", "/api/log-checks/params?log_id=1");
|
||||
expect(reqs).toHaveLength(1);
|
||||
expect(store.allConformanceTask).toEqual([{ label: "A" }]);
|
||||
expect(store.allCfmSeqStart).toEqual(["A"]);
|
||||
});
|
||||
@@ -68,13 +69,19 @@ describe("conformanceStore", () => {
|
||||
waiting_time: {},
|
||||
cycle_time: {},
|
||||
};
|
||||
mockGet.mockResolvedValue({ data: mockData });
|
||||
server.use(
|
||||
http.get("/api/filter-checks/params", ({ request }) => {
|
||||
const url = new URL(request.url);
|
||||
captureRequest("GET", url.pathname + url.search);
|
||||
return HttpResponse.json(mockData);
|
||||
}),
|
||||
);
|
||||
|
||||
await store.getConformanceParams();
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith(
|
||||
"/api/filter-checks/params?filter_id=5",
|
||||
);
|
||||
const reqs = findRequest("GET",
|
||||
"/api/filter-checks/params?filter_id=5");
|
||||
expect(reqs).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -82,26 +89,41 @@ describe("conformanceStore", () => {
|
||||
it("posts to log temp-check and stores id", async () => {
|
||||
store.conformanceLogId = 1;
|
||||
store.conformanceFilterId = null;
|
||||
mockPost.mockResolvedValue({ data: { id: 42 } });
|
||||
server.use(
|
||||
http.post("/api/temp-log-checks", async ({ request }) => {
|
||||
const url = new URL(request.url);
|
||||
const body = await request.json();
|
||||
captureRequest("POST", url.pathname + url.search, body);
|
||||
return HttpResponse.json({ id: 42 });
|
||||
}),
|
||||
);
|
||||
|
||||
await store.addConformanceCheckId({ rule: "test" });
|
||||
|
||||
expect(mockPost).toHaveBeenCalledWith("/api/temp-log-checks?log_id=1", {
|
||||
rule: "test",
|
||||
});
|
||||
const reqs = findRequest("POST",
|
||||
"/api/temp-log-checks?log_id=1");
|
||||
expect(reqs).toHaveLength(1);
|
||||
expect(reqs[0].body).toEqual({ rule: "test" });
|
||||
expect(store.conformanceLogTempCheckId).toBe(42);
|
||||
});
|
||||
|
||||
it("posts to filter temp-check when filter set", async () => {
|
||||
store.conformanceFilterId = 3;
|
||||
mockPost.mockResolvedValue({ data: { id: 99 } });
|
||||
server.use(
|
||||
http.post("/api/temp-filter-checks", async ({ request }) => {
|
||||
const url = new URL(request.url);
|
||||
const body = await request.json();
|
||||
captureRequest("POST", url.pathname + url.search, body);
|
||||
return HttpResponse.json({ id: 99 });
|
||||
}),
|
||||
);
|
||||
|
||||
await store.addConformanceCheckId({ rule: "test" });
|
||||
|
||||
expect(mockPost).toHaveBeenCalledWith(
|
||||
"/api/temp-filter-checks?filter_id=3",
|
||||
{ rule: "test" },
|
||||
);
|
||||
const reqs = findRequest("POST",
|
||||
"/api/temp-filter-checks?filter_id=3");
|
||||
expect(reqs).toHaveLength(1);
|
||||
expect(reqs[0].body).toEqual({ rule: "test" });
|
||||
expect(store.conformanceFilterTempCheckId).toBe(99);
|
||||
});
|
||||
});
|
||||
@@ -110,18 +132,28 @@ describe("conformanceStore", () => {
|
||||
it("fetches temp log check report", async () => {
|
||||
store.conformanceLogTempCheckId = 10;
|
||||
const mockData = { file: {}, charts: {} };
|
||||
mockGet.mockResolvedValue({ data: mockData });
|
||||
server.use(
|
||||
http.get("/api/temp-log-checks/:id", ({ request }) => {
|
||||
captureRequest("GET", new URL(request.url).pathname);
|
||||
return HttpResponse.json(mockData);
|
||||
}),
|
||||
);
|
||||
|
||||
await store.getConformanceReport();
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith("/api/temp-log-checks/10");
|
||||
const reqs = findRequest("GET", "/api/temp-log-checks/10");
|
||||
expect(reqs).toHaveLength(1);
|
||||
expect(store.allConformanceTempReportData).toEqual(mockData);
|
||||
});
|
||||
|
||||
it("stores routeFile when getRouteFile=true", async () => {
|
||||
store.conformanceLogTempCheckId = 10;
|
||||
const mockData = { file: { name: "test.csv" } };
|
||||
mockGet.mockResolvedValue({ data: mockData });
|
||||
server.use(
|
||||
http.get("/api/temp-log-checks/:id", () =>
|
||||
HttpResponse.json(mockData),
|
||||
),
|
||||
);
|
||||
|
||||
await store.getConformanceReport(true);
|
||||
|
||||
@@ -136,11 +168,20 @@ describe("conformanceStore", () => {
|
||||
store.conformanceFilterId = null;
|
||||
store.conformanceLogTempCheckId = 10;
|
||||
store.conformanceRuleData = { type: "test" };
|
||||
mockPost.mockResolvedValue({ data: { id: 100 } });
|
||||
server.use(
|
||||
http.post("/api/log-checks", async ({ request }) => {
|
||||
const url = new URL(request.url);
|
||||
const body = await request.json();
|
||||
captureRequest("POST", url.pathname + url.search, body);
|
||||
return HttpResponse.json({ id: 100 });
|
||||
}),
|
||||
);
|
||||
|
||||
await store.addConformanceCreateCheckId("myRule");
|
||||
|
||||
expect(mockPost).toHaveBeenCalledWith("/api/log-checks?log_id=1", {
|
||||
const reqs = findRequest("POST", "/api/log-checks?log_id=1");
|
||||
expect(reqs).toHaveLength(1);
|
||||
expect(reqs[0].body).toEqual({
|
||||
name: "myRule",
|
||||
rule: { type: "test" },
|
||||
});
|
||||
@@ -153,13 +194,19 @@ describe("conformanceStore", () => {
|
||||
it("updates existing log check", async () => {
|
||||
store.conformanceLogCreateCheckId = 50;
|
||||
store.conformanceRuleData = { type: "updated" };
|
||||
mockPut.mockResolvedValue({ status: 200 });
|
||||
server.use(
|
||||
http.put("/api/log-checks/:id", async ({ request, params }) => {
|
||||
const body = await request.json();
|
||||
captureRequest("PUT", `/api/log-checks/${params.id}`, body);
|
||||
return new HttpResponse(null, { status: 200 });
|
||||
}),
|
||||
);
|
||||
|
||||
await store.updateConformance();
|
||||
|
||||
expect(mockPut).toHaveBeenCalledWith("/api/log-checks/50", {
|
||||
type: "updated",
|
||||
});
|
||||
const reqs = findRequest("PUT", "/api/log-checks/50");
|
||||
expect(reqs).toHaveLength(1);
|
||||
expect(reqs[0].body).toEqual({ type: "updated" });
|
||||
expect(store.isUpdateConformance).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user