groundingForm.spec.js
5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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 }));
});
});