Files
WonderQ-Project/WonderQ-MiniAPP/tests/customer-records-api.test.ts

65 lines
3.2 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import * as api from "@/lib/api";
const customerRecordsApi = api as typeof api & {
createCustomerBrowseHistory: (payload: { itemType: string; itemId: string }, token: string) => Promise<unknown>;
fetchCustomerBrowseHistory: (token: string, pageNum: number, pageSize: number) => Promise<unknown>;
fetchCustomerVehicleDemand: (leadId: string, token: string) => Promise<unknown>;
fetchCustomerVehicleDemands: (token: string, pageNum: number, pageSize: number) => Promise<unknown>;
};
describe("customer records API client", () => {
afterEach(() => vi.unstubAllGlobals());
function requestPath(url: string) {
return url.startsWith("http") ? new URL(url).pathname : url;
}
it("requests the authenticated vehicle demand page and detail", async () => {
const request = vi.fn()
.mockImplementationOnce((options) => options.success({
statusCode: 200,
data: { code: 200, msg: "success", data: { items: [], total: 0, pageNum: 2, pageSize: 20 } },
}))
.mockImplementationOnce((options) => options.success({
statusCode: 200,
data: { code: 200, msg: "success", data: { id: "lead-1" } },
}));
vi.stubGlobal("uni", { request });
expect(typeof customerRecordsApi.fetchCustomerVehicleDemands).toBe("function");
expect(typeof customerRecordsApi.fetchCustomerVehicleDemand).toBe("function");
await customerRecordsApi.fetchCustomerVehicleDemands("customer-token", 2, 20);
await customerRecordsApi.fetchCustomerVehicleDemand("lead-1", "customer-token");
expect(request.mock.calls.map(([options]) => [requestPath(options.url), options.method, options.data, options.header.Authorization])).toEqual([
["/api/public/customer/vehicle-demands", "GET", { pageNum: 2, pageSize: 20 }, "Bearer customer-token"],
["/api/public/customer/vehicle-demands/lead-1", "GET", undefined, "Bearer customer-token"],
]);
});
it("requests browse history pages and records a viewed item with the customer token", async () => {
const request = vi.fn()
.mockImplementationOnce((options) => options.success({
statusCode: 200,
data: { code: 200, msg: "success", data: { items: [], total: 0, pageNum: 1, pageSize: 20 } },
}))
.mockImplementationOnce((options) => options.success({
statusCode: 201,
data: { code: 201, msg: "success", data: { itemId: "family-water" } },
}));
vi.stubGlobal("uni", { request });
expect(typeof customerRecordsApi.fetchCustomerBrowseHistory).toBe("function");
expect(typeof customerRecordsApi.createCustomerBrowseHistory).toBe("function");
await customerRecordsApi.fetchCustomerBrowseHistory("customer-token", 1, 20);
await customerRecordsApi.createCustomerBrowseHistory({ itemType: "wanfa-route", itemId: "family-water" }, "customer-token");
expect(request.mock.calls.map(([options]) => [requestPath(options.url), options.method, options.data, options.header.Authorization])).toEqual([
["/api/public/customer/browse-history", "GET", { pageNum: 1, pageSize: 20 }, "Bearer customer-token"],
["/api/public/customer/browse-history", "POST", { itemType: "wanfa-route", itemId: "family-water" }, "Bearer customer-token"],
]);
});
});