groundingForm.spec.js 5.28 KB
import { validateStoreRegion } from "@api/grounding";
import { createLocalVue, shallowMount } from "@vue/test-utils";
import ElementUI from "element-ui";
import EditGrounding from "../editGrounding.vue";
import GroundingForm from "./groundingForm.vue";

const localVue = createLocalVue();
localVue.use(ElementUI);

jest.mock("@api/grounding", () => ({
	validateStoreRegion: jest.fn(),
}));
jest.mock("@api/serviceManage", () => ({ searchServiceMerchant: jest.fn() }));
jest.mock("glsk-oss-jssdk", () => jest.fn());
jest.mock("./relateStoreDialog.vue", () => ({}));
jest.mock("./relateStoreTable.vue", () => ({}));
jest.mock("../../components/selectGoodsDialog.vue", () => ({}));
jest.mock("../../components/ellipsisPopover.vue", () => ({}));
jest.mock("@/main", () => ({
	baseConfig: {
		equitySceneType: [],
		extendedPosiType: [],
		membraneTypeList: [],
	},
}));

describe("purchase grounding form", () => {
	it("blocks product selection until stores are selected", () => {
		const warning = jest.fn();
		const context = {
			formData: { merchantRefList: [] },
			$message: { warning },
			selectGoodsVisible: false,
		};

		GroundingForm.methods.handleSelectGoods.call(context);

		expect(warning).toHaveBeenCalledWith("请选择上架商户");
		expect(context.selectGoodsVisible).toBe(false);
	});

	it("renders the selected product purchase cost in the product details", async () => {
		const editForm = EditGrounding.data.call({}).editForm;
		const wrapper = shallowMount(GroundingForm, {
			localVue,
			mocks: {
				$$: (value, key) => value?.[key],
				$store: {
					state: {
						d2admin: { loading: { value: false } },
					},
				},
			},
			propsData: {
				formList: editForm,
				originData: {
					goodsType: 3,
					productType: [],
					purchaseCostType: 0,
					purchasePrice: "",
					upgradeGoodsId: 10,
					upgradeGoods: { purchaseCost: 1 },
				},
			},
			stubs: {
				ElForm: {
					methods: {
						clearValidate() {},
					},
					template: "<form><slot /></form>",
				},
				EllipsisPopover: {
					props: ["content"],
					template: "<span class=\"rendered-product-detail\">{{ content }}</span>",
				},
			},
		});
		await wrapper.vm.$nextTick();

		const purchasePriceItem = wrapper.findAll(".incid_box .item").wrappers.find(item => item.find(".label").text().includes("采购价"));

		expect(purchasePriceItem.find(".rendered-product-detail").text()).toBe("1.00元");
	});

	it("rolls stores back when an invalid region confirmation is cancelled", async () => {
		validateStoreRegion.mockResolvedValue({
			allMatch: false,
			invalidUpgradeGoodsIds: [10],
		});
		const previousStores = [{ merchantId: 1 }];
		const context = {
			formData: {
				upgradeGoodsId: 10,
				merchantRefList: [{ merchantId: 2 }],
				upgradeGoods: { upgradeGoodsName: "测试产品" },
			},
			$confirm: jest.fn().mockRejectedValue(new Error("cancel")),
			$set(target, key, value) {
				target[key] = value;
			},
			clearPurchaseGoods: jest.fn(),
		};

		await GroundingForm.methods.validateStoreRegionAfterChange.call(context, {
			previousMerchantRefList: previousStores,
			revertStoresOnCancel: true,
		});

		expect(context.formData.merchantRefList).toEqual(previousStores);
		expect(context.clearPurchaseGoods).not.toHaveBeenCalled();
	});

	it("keeps the new stores and clears product data after invalid-region confirmation", async () => {
		validateStoreRegion.mockResolvedValue({
			allMatch: false,
			invalidUpgradeGoodsIds: [10],
		});
		const context = {
			formData: {
				upgradeGoodsId: 10,
				merchantRefList: [{ merchantId: 2 }],
				upgradeGoods: { upgradeGoodsName: "测试产品" },
			},
			$confirm: jest.fn().mockResolvedValue(),
			$set: jest.fn(),
			clearPurchaseGoods: jest.fn(),
		};

		await GroundingForm.methods.validateStoreRegionAfterChange.call(context);

		expect(context.clearPurchaseGoods).toHaveBeenCalledTimes(1);
		expect(context.formData.merchantRefList).toEqual([{ merchantId: 2 }]);
	});

	it("blocks submit and identifies stores outside the product supply region", async () => {
		validateStoreRegion
			.mockResolvedValueOnce({ allMatch: true, invalidUpgradeGoodsIds: [] })
			.mockResolvedValueOnce({ allMatch: false, invalidUpgradeGoodsIds: [10] });
		const warning = jest.fn();
		const context = {
			formData: {
				upgradeGoodsId: 10,
				upgradeGoods: { upgradeGoodsName: "产品A" },
				merchantRefList: [
					{ merchantId: 1, merchantName: "杭州店" },
					{ merchantId: 2, merchantName: "宁波店" },
				],
			},
			$message: { warning },
		};

		const result = await GroundingForm.methods.validateCurrentStoreRegions.call(context);

		expect(result).toBe(false);
		expect(warning).toHaveBeenCalledWith("产品A不在宁波店供给范围内");
	});

	it("leaves product supply-source validation to the backend", async () => {
		const emit = jest.fn();
		const validate = jest.fn(callback => callback(true));
		const context = {
			formData: {
				purchaseCostType: 0,
				upgradeGoods: {
					supplySource: "3",
				},
			},
			formRef: "",
			$message: { warning: jest.fn() },
			$refs: {
				d2FormRuleForm: { validate },
			},
			$emit: emit,
			validateCurrentStoreRegions: jest.fn().mockResolvedValue(true),
		};

		await GroundingForm.methods.handleUp.call(context, 1);

		expect(validate).toHaveBeenCalledTimes(1);
		expect(emit).toHaveBeenCalledWith("handleAddConfirm", expect.objectContaining({ num: 1 }));
	});
});