marketingPushStrategy.integration.spec.js
13.8 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import { createLocalVue, shallowMount } from "@vue/test-utils";
import Vuex from "vuex";
const mockDeleteStrategy = jest.fn();
const mockStatusStrategy = jest.fn();
const mockGetList = jest.fn();
const mockCreateStrategy = jest.fn();
const mockUpdateStrategy = jest.fn();
const mockGetMetaOptionsApi = jest.fn();
const mockGetMarketingPushStrategyDetailApi = jest.fn();
const mockNanoid = jest.fn(() => "mock-id");
jest.mock("nanoid", () => ({
nanoid: mockNanoid,
}));
jest.mock("@/common/util", () => ({
deepClone: jest.fn(value => JSON.parse(JSON.stringify(value))),
Util: {
dateFormate: jest.fn(() => "2026-06-25 10:00"),
},
}));
jest.mock("@/mixins/reportTrackFunMixin", () => ({
__esModule: true,
default: {
methods: {
setReportTrackData: jest.fn(),
},
},
}));
jest.mock("@/views/marketingManagement/marketingPushStrategy/api/index", () => ({
deleteStrategy: mockDeleteStrategy,
getList: mockGetList,
statusStrategy: mockStatusStrategy,
createStrategy: mockCreateStrategy,
updateStrategy: mockUpdateStrategy,
getMetaOptionsApi: mockGetMetaOptionsApi,
getMarketingPushStrategyDetailApi: mockGetMarketingPushStrategyDetailApi,
}));
jest.mock("@/views/marketingManagement/marketingPushStrategy/api/index.js", () => ({
deleteStrategy: mockDeleteStrategy,
getList: mockGetList,
statusStrategy: mockStatusStrategy,
createStrategy: mockCreateStrategy,
updateStrategy: mockUpdateStrategy,
getMetaOptionsApi: mockGetMetaOptionsApi,
getMarketingPushStrategyDetailApi: mockGetMarketingPushStrategyDetailApi,
}));
jest.mock(
"@/views/marketingManagement/marketingPushStrategy/components/recordDialog/index",
() => ({
name: "RecordDialog",
props: ["recordVisible", "strategyId"],
render(h) {
return h("div");
},
}),
);
jest.mock(
"@/views/marketingManagement/marketingPushStrategy/components/branchGroup/index.vue",
() => ({
name: "BranchGroup",
render(h) {
return h("div");
},
}),
);
jest.mock(
"@/views/marketingManagement/marketingPushStrategy/components/conditionPanel/index.vue",
() => ({
name: "ConditionPanel",
render(h) {
return h("div");
},
}),
);
jest.mock(
"@/views/marketingManagement/marketingPushStrategy/components/selectStoreDialog/index.vue",
() => ({
name: "SelectStoreDialog",
render(h) {
return h("div");
},
}),
);
const { MessageTypeEnum } = require("./constant/index.js");
const TablePage = require("./views/table/index.vue").default;
const ModifyPage = require("./views/modify/index.vue").default;
const flushPromises = () => new Promise(resolve => setTimeout(resolve, 0));
describe("marketingPushStrategy 集成测试", () => {
const localVue = createLocalVue();
localVue.use(Vuex);
localVue.directive("loading", () => {});
let wrapper;
let store;
let routerPushMock;
let messageSuccessMock;
let confirmMock;
function createStore() {
return new Vuex.Store({
state: {},
modules: {
d2admin: {
namespaced: true,
state: {
user: {
info: {
userId: "u-1",
userName: "tester",
},
},
loading: {
value: false,
},
},
},
},
});
}
function createTableWrapper() {
store = createStore();
routerPushMock = jest.fn();
messageSuccessMock = jest.fn();
confirmMock = jest.fn();
wrapper = shallowMount(TablePage, {
localVue,
store,
mocks: {
$router: {
push: routerPushMock,
},
$message: {
success: messageSuccessMock,
},
$confirm: confirmMock,
},
stubs: {
"d2-container": true,
"d2-search": true,
"d2-table-header": true,
"d2-table": true,
"auth-button": true,
"el-button": true,
"el-switch": true,
},
});
wrapper.vm.$refs.marketTable = {
page: {
currentPage: 3,
},
refreshTable: jest.fn(),
};
return wrapper;
}
function createModifyVm(query = { editType: "add" }) {
store = createStore();
routerPushMock = jest.fn();
messageSuccessMock = jest.fn();
const baseCtx = {
$store: store,
};
const data = ModifyPage.data.call(baseCtx);
const vm = {
...data,
$store: store,
$route: {
query,
},
$router: {
push: routerPushMock,
},
$message: {
success: messageSuccessMock,
},
$refs: {},
$set(target, key, value) {
target[key] = value;
},
$nextTick: jest.fn(() => Promise.resolve()),
setReportTrackData: jest.fn(),
};
Object.entries(ModifyPage.methods).forEach(([key, fn]) => {
vm[key] = fn.bind(vm);
});
return vm;
}
function createModifyWrapper(query = { editType: "add" }) {
store = createStore();
routerPushMock = jest.fn();
messageSuccessMock = jest.fn();
wrapper = shallowMount(ModifyPage, {
localVue,
store,
mocks: {
$route: {
query,
},
$router: {
push: routerPushMock,
},
$message: {
success: messageSuccessMock,
},
},
stubs: {
"glsx-combi-form": {
name: "glsx-combi-form",
render(h) {
return h("div", [
...(this.$slots.default || []),
...(this.$slots.storeIds || []),
...(this.$slots.stageSlot || []),
...(this.$slots.step2ConditionGroups || []),
]);
},
},
"el-form": {
name: "el-form",
render(h) {
return h("form", this.$slots.default);
},
},
"el-form-item": {
name: "el-form-item",
render(h) {
return h("div", this.$slots.default);
},
},
"el-radio-group": {
name: "el-radio-group",
render(h) {
return h("div", this.$slots.default);
},
},
"el-radio": {
name: "el-radio",
functional: true,
props: {
label: {
type: [String, Number],
default: "",
},
},
render(h, ctx) {
return h("label", {
attrs: {
"data-action-confirm-label": String(ctx.props.label),
},
}, ctx.children);
},
},
"el-button": true,
"el-select": true,
"el-option": true,
"el-input-number": true,
"BranchGroup": true,
"ConditionPanel": true,
"SelectStoreDialog": true,
},
});
return wrapper;
}
function mockMetaOptions(overrides = {}) {
mockGetMetaOptionsApi.mockResolvedValue({
conditionVariables: [{ code: "age", name: "车龄" }],
step2ConditionVariables: [{ code: "level", name: "等级" }],
stages: [
{
stageCode: "AFTER_DELIVERY",
stageName: "交付后",
stageDesc: "交付后的营销阶段",
triggerMetaDates: [{ code: "DELIVERY_DATE", name: "交付日期" }],
},
],
triggers: [{ code: "DATE_TIME", name: "日期时间" }],
messageVariables: [{ code: "customerName", name: "客户姓名" }],
actionConfirmOptions: [
{ value: 0, label: "自动执行" },
{ value: 1, label: "需驻店人员确认" },
],
...overrides,
});
}
function setModifyFormRefs(vm) {
vm.$refs.BaseEditFormRef = {
validate: jest.fn().mockResolvedValue(),
$refs: {
BaseEditChildRef: {
clearValidate: jest.fn(),
},
},
};
vm.$refs.SpecFormRef = {
validate: jest.fn().mockResolvedValue(),
$refs: {
SpecChildRef: {
validateField: jest.fn(),
},
},
};
vm.$refs.BranchGroupFormRef = {
validate: jest.fn().mockResolvedValue(),
};
}
afterEach(() => {
if (wrapper) {
wrapper.destroy();
}
wrapper = null;
jest.clearAllMocks();
});
describe("列表页联动", () => {
it("应初始化查询选项,并支持查询和重置", async () => {
createTableWrapper();
wrapper.setData({
searchForm: {
strategyName: "策略A",
status: 1,
},
});
await wrapper.vm.beforeSearchFn();
expect(wrapper.vm.showFormData[1].optionList).toEqual([
{ confId: "", confLabel: "全部" },
{ confId: 1, confLabel: "启用" },
{ confId: 0, confLabel: "停用" },
]);
expect(wrapper.vm.searchFormSearch).toEqual({
strategyName: "策略A",
status: 1,
pageNo: 3,
});
wrapper.vm.handleSearch();
expect(wrapper.vm.$refs.marketTable.refreshTable).toHaveBeenCalledWith(false, true);
wrapper.vm.handleReset();
expect(wrapper.vm.searchForm).toEqual({
strategyName: "",
status: "",
});
});
it("应处理执行记录弹窗和跳转编辑", () => {
createTableWrapper();
wrapper.vm.handleRecord({ id: 88 });
expect(wrapper.vm.dialog.recordVisible).toBe(true);
expect(wrapper.vm.dialog.strategyId).toBe("88");
wrapper.vm.handleRecordClose();
expect(wrapper.vm.dialog.recordVisible).toBe(false);
wrapper.vm.handleModify({ id: 99 });
expect(routerPushMock).toHaveBeenCalledWith({
path: "/marketing-push-strategy/modify",
query: {
editType: "edit",
strategyId: 99,
},
});
wrapper.vm.handleModify();
expect(routerPushMock).toHaveBeenLastCalledWith({
path: "/marketing-push-strategy/modify",
query: {
editType: "add",
strategyId: "",
},
});
});
it("应处理启停和删除流程", async () => {
createTableWrapper();
const resetSpy = jest.spyOn(wrapper.vm, "handleReset");
mockStatusStrategy.mockResolvedValue({});
confirmMock.mockResolvedValue();
mockDeleteStrategy.mockResolvedValue({});
wrapper.vm.handleChangeStatus({ id: 1, status: 0 });
await flushPromises();
expect(mockStatusStrategy).toHaveBeenCalledWith({
strategyId: 1,
status: 0,
});
expect(messageSuccessMock).toHaveBeenCalledWith("操作成功");
wrapper.vm.handleDelete({ id: 2, strategyName: "老客唤醒" });
await flushPromises();
await flushPromises();
expect(confirmMock).toHaveBeenCalledWith(
"确定要删除策略\"老客唤醒\"吗?",
"删除策略",
expect.objectContaining({
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}),
);
expect(mockDeleteStrategy).toHaveBeenCalledWith({ strategyId: 2 });
expect(resetSpy).toHaveBeenCalled();
});
});
describe("编辑页联动", () => {
it("新增场景应初始化元数据和表单结构", async () => {
mockMetaOptions();
const vm = createModifyVm({ editType: "add" });
await ModifyPage.mounted.call(vm);
expect(mockGetMetaOptionsApi).toHaveBeenCalled();
expect(vm.options.stages).toHaveLength(1);
expect(vm.formSchema.find(item => item.name === "marketingStage").optionList).toEqual(
vm.options.stages,
);
expect(vm.specFormSchema.find(item => item.name === "triggerType").optionList).toEqual(
vm.options.triggers,
);
});
it("应使用项目配置的动作执行确认选项", async () => {
mockMetaOptions();
createModifyWrapper({ editType: "add" });
await flushPromises();
await flushPromises();
expect(wrapper.vm.options.actionConfirmOptions).toEqual([
{ value: 0, label: "自动执行" },
{ value: 1, label: "需驻店人员确认" },
]);
});
it("编辑场景应回填详情并转换网页素材数据", async () => {
mockMetaOptions();
mockGetMarketingPushStrategyDetailApi.mockResolvedValue({
marketingStage: "AFTER_DELIVERY",
relativeDateType: 0,
triggerHour: 9,
triggerMinute: 5,
storeIds: [{ merchantId: "m-1", merchantName: "门店1" }],
branches: [
{
conditionGroups: [],
actions: [
{
messageType: MessageTypeEnum.VARIABLE_WEBPAGE,
materialJson: "[{\"url\":\"https://img.test/a.png\"}]",
},
],
},
],
});
const vm = createModifyVm({ editType: "edit", strategyId: "66" });
await ModifyPage.mounted.call(vm);
expect(mockGetMarketingPushStrategyDetailApi).toHaveBeenCalledWith({ strategyId: "66" });
expect(vm.form.triggerHour).toBe("09");
expect(vm.form.triggerMinute).toBe("05");
expect(vm.form.storeIds).toEqual([{ merchantId: "m-1", merchantName: "门店1" }]);
expect(vm.form.branches[0].actions[0].targetGroupType).toBe(1);
expect(vm.form.branches[0].actions[0].materialJson).toEqual([
{ url: "https://img.test/a.png" },
]);
expect(
vm.specFormSchema.find(item => item.name === "dateVariableType").optionList,
).toEqual([{ code: "DELIVERY_DATE", name: "交付日期" }]);
});
it("新增提交应组装参数并调用创建接口", async () => {
mockMetaOptions();
mockCreateStrategy.mockResolvedValue({});
const vm = createModifyVm({ editType: "add" });
await ModifyPage.mounted.call(vm);
setModifyFormRefs(vm);
vm.form.storeScopeType = 1;
vm.form.storeIds = [
{ merchantId: "m-1", merchantName: "门店1", extra: "ignored" },
];
vm.form.branches = [
{
conditionGroups: [],
actions: [
{
messageType: MessageTypeEnum.VARIABLE_WEBPAGE,
materialJson: [{ url: "https://img.test/a.png" }],
},
],
},
];
await vm.handleConfirm();
await flushPromises();
await flushPromises();
expect(mockCreateStrategy).toHaveBeenCalledWith(expect.objectContaining({
storeIds: [{ merchantId: "m-1", merchantName: "门店1" }],
branches: [
{
conditionGroups: [],
actions: [
expect.objectContaining({
messageType: MessageTypeEnum.VARIABLE_WEBPAGE,
materialJson: "[{\"url\":\"https://img.test/a.png\"}]",
}),
],
},
],
}));
expect(messageSuccessMock).toHaveBeenCalledWith("操作成功");
expect(routerPushMock).toHaveBeenCalledWith({
name: "marketingPushStrategy",
});
});
it("编辑提交应选择更新接口并带上 strategyId", async () => {
mockMetaOptions();
mockGetMarketingPushStrategyDetailApi.mockResolvedValue({
marketingStage: "AFTER_DELIVERY",
storeIds: [],
relativeDateType: 2,
branches: [{ actions: [], conditionGroups: [] }],
});
mockUpdateStrategy.mockResolvedValue({});
const vm = createModifyVm({ editType: "edit", strategyId: "77" });
await ModifyPage.mounted.call(vm);
setModifyFormRefs(vm);
vm.form.branches = [{ actions: [], conditionGroups: [] }];
await vm.handleConfirm();
await flushPromises();
await flushPromises();
expect(mockUpdateStrategy).toHaveBeenCalledWith(expect.objectContaining({
strategyId: "77",
}));
});
});
});