stockMoveDialog.spec.js 1.51 KB
import { listUpgradeGoodsSimple, moveMerchantGoodsStock } from "@api/stockManage";
import { STOCK_MOVE_ZERO_MESSAGE } from "./stockMove";
import StockMoveDialog from "./stockMoveDialog.vue";

jest.mock("@api/stockManage", () => ({
	listUpgradeGoodsSimple: jest.fn(),
	moveMerchantGoodsStock: jest.fn(),
}));

describe("stock move dialog", () => {
	beforeEach(() => {
		jest.clearAllMocks();
	});

	it("queries light-modification products and excludes the current UID", async () => {
		listUpgradeGoodsSimple.mockResolvedValue({
			list: [
				{ id: 1, commodityCode: "OLD" },
				{ id: 2, commodityCode: "NEW" },
			],
		});
		const context = {
			requestSequence: 0,
			loading: false,
			targetOptions: [],
			row: { upgradeGoodsId: 1, upgradeCommodityCode: "OLD" },
		};

		await StockMoveDialog.methods.fetchTargetOptions.call(context, "UID");

		expect(listUpgradeGoodsSimple).toHaveBeenCalledWith({
			currentPage: 1,
			pageSize: 20,
			commodityCode: "UID",
			productType: 1,
		});
		expect(context.targetOptions).toEqual([{ id: 2, commodityCode: "NEW" }]);
	});

	it("blocks zero-stock rows before validating or submitting", () => {
		const warning = jest.fn();
		const validate = jest.fn();
		StockMoveDialog.methods.handleConfirm.call({
			submitting: false,
			row: { stock: 0 },
			$message: { warning },
			$refs: { stockMoveForm: { validate } },
		});

		expect(warning).toHaveBeenCalledWith(STOCK_MOVE_ZERO_MESSAGE);
		expect(validate).not.toHaveBeenCalled();
		expect(moveMerchantGoodsStock).not.toHaveBeenCalled();
	});
});