index.spec.js 2.57 KB
const mockRequest = jest.fn();
const mockRequestMethods = { POST: "POST", GET: "GET" };

jest.mock("@/api/request", () => mockRequest);
jest.mock("@/api/request.methods", () => mockRequestMethods);

const {
	getList,
	getMarketingPushStrategyDetailApi,
	createStrategy,
	updateStrategy,
	statusStrategy,
	deleteStrategy,
	confirmStrategy,
	recordsStrategy,
	recordDetail,
	getMetaData,
	getMetaOptionsApi,
} = require("./index.js");

describe("marketingPushStrategy api/index.js", () => {
	beforeEach(() => {
		jest.clearAllMocks();
	});

	it("getList 应追加 list 方法名并走默认请求方式", async () => {
		const params = { pageNo: 1 };
		mockRequest.mockResolvedValue({ list: [] });

		await getList(params);

		expect(mockRequest).toHaveBeenCalledWith({
			pageNo: 1,
			method: "dj.api.smartcarlife.sop.strategy.list",
		});
	});

	it("详情与创建更新相关接口应使用 post", async () => {
		mockRequest.mockResolvedValue({});

		await getMarketingPushStrategyDetailApi({ strategyId: 1 });
		await createStrategy({ strategyName: "A" });
		await updateStrategy({ strategyId: 1 });
		await statusStrategy({ strategyId: 1, status: 1 });
		await deleteStrategy({ strategyId: 1 });
		await confirmStrategy({ strategyIds: [1] });
		await recordsStrategy({ strategyId: 1 });
		await recordDetail({ recordId: 1 });
		await getMetaData({});

		expect(mockRequest.mock.calls).toEqual([
			[{ strategyId: 1, method: "dj.api.smartcarlife.sop.strategy.query" }, "post"],
			[{ strategyName: "A", method: "dj.api.smartcarlife.sop.strategy.create" }, "post"],
			[{ strategyId: 1, method: "dj.api.smartcarlife.sop.strategy.update" }, "post"],
			[{ strategyId: 1, status: 1, method: "dj.api.smartcarlife.sop.strategy.status" }, "post"],
			[{ strategyId: 1, method: "dj.api.smartcarlife.sop.strategy.delete" }, "post"],
			[{ strategyIds: [1], method: "dj.api.smartcarlife.sop.strategy.confirm" }, "post"],
			[{ strategyId: 1, method: "dj.api.smartcarlife.sop.strategy.records" }, "post"],
			[{ recordId: 1, method: "dj.api.smartcarlife.sop.strategy.record.detail" }, "post"],
			[{ method: "dj.api.smartcarlife.sop.strategy.meta" }, "post"],
		]);
	});

	it("getMetaOptionsApi 应默认空对象并使用 RequestMethods.POST", async () => {
		mockRequest.mockResolvedValue({});

		await getMetaOptionsApi();
		await getMetaOptionsApi({ bizType: 1 });

		expect(mockRequest.mock.calls).toEqual([
			[
				{ method: "dj.api.smartcarlife.sop.strategy.meta" },
				mockRequestMethods.POST,
			],
			[
				{ bizType: 1, method: "dj.api.smartcarlife.sop.strategy.meta" },
				mockRequestMethods.POST,
			],
		]);
	});
});