158 lines
3.7 KiB
JavaScript
158 lines
3.7 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const StoreWarehouseStoreService = require('../../../../../app/service/systemMethods/storeWarehouse/store');
|
|
|
|
function createContext(options = {}) {
|
|
const {
|
|
includeList = true,
|
|
includeSign = true,
|
|
} = options;
|
|
|
|
const actionCalls = {
|
|
list: [],
|
|
sign: [],
|
|
};
|
|
|
|
const storeWarehouse = {};
|
|
|
|
if (includeList) {
|
|
storeWarehouse.list = async function list(data) {
|
|
actionCalls.list.push(data);
|
|
data.touched = true;
|
|
return {
|
|
received: data,
|
|
};
|
|
};
|
|
}
|
|
|
|
if (includeSign) {
|
|
storeWarehouse.sign = async function sign(data) {
|
|
actionCalls.sign.push(data);
|
|
data.touched = true;
|
|
return {
|
|
received: data,
|
|
};
|
|
};
|
|
}
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {},
|
|
},
|
|
service: {
|
|
systems: {
|
|
storeWarehouse,
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
actionCalls,
|
|
ctx,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/storeWarehouse/store.test.js', () => {
|
|
it('should copy payload before calling list', async () => {
|
|
const { actionCalls, ctx } = createContext();
|
|
const service = new StoreWarehouseStoreService(ctx);
|
|
const payload = {
|
|
page: 1,
|
|
page_size: 20,
|
|
nested: {
|
|
keep: true,
|
|
},
|
|
};
|
|
|
|
const result = await service.list(payload);
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(actionCalls.list.length, 1);
|
|
assert.notStrictEqual(actionCalls.list[0], payload);
|
|
assert.deepEqual(actionCalls.list[0], {
|
|
page: 1,
|
|
page_size: 20,
|
|
nested: {
|
|
keep: true,
|
|
},
|
|
touched: true,
|
|
});
|
|
assert.equal(payload.touched, undefined);
|
|
assert.deepEqual(payload, {
|
|
page: 1,
|
|
page_size: 20,
|
|
nested: {
|
|
keep: true,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('should copy payload before calling sign', async () => {
|
|
const { actionCalls, ctx } = createContext();
|
|
const service = new StoreWarehouseStoreService(ctx);
|
|
const payload = {
|
|
warehouse_id: 1001,
|
|
nested: {
|
|
keep: true,
|
|
},
|
|
};
|
|
|
|
const result = await service.sign(payload);
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(actionCalls.sign.length, 1);
|
|
assert.notStrictEqual(actionCalls.sign[0], payload);
|
|
assert.deepEqual(actionCalls.sign[0], {
|
|
warehouse_id: 1001,
|
|
nested: {
|
|
keep: true,
|
|
},
|
|
touched: true,
|
|
});
|
|
assert.equal(payload.touched, undefined);
|
|
assert.deepEqual(payload, {
|
|
warehouse_id: 1001,
|
|
nested: {
|
|
keep: true,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('should keep the failure fallback for missing list method', async () => {
|
|
const { actionCalls, ctx } = createContext({
|
|
includeList: false,
|
|
});
|
|
const service = new StoreWarehouseStoreService(ctx);
|
|
|
|
const result = await service.list({
|
|
page: 1,
|
|
});
|
|
|
|
assert.equal(result.flag, 'failure');
|
|
assert.equal(result.message, '获取签约列表失败');
|
|
assert.equal(Number(result.code), 500);
|
|
assert.equal(actionCalls.list.length, 0);
|
|
assert.equal(actionCalls.sign.length, 0);
|
|
});
|
|
|
|
it('should keep the failure fallback for missing sign method', async () => {
|
|
const { actionCalls, ctx } = createContext({
|
|
includeSign: false,
|
|
});
|
|
const service = new StoreWarehouseStoreService(ctx);
|
|
|
|
const result = await service.sign({
|
|
warehouse_id: 1001,
|
|
});
|
|
|
|
assert.equal(result.flag, 'failure');
|
|
assert.equal(result.message, '签约失败');
|
|
assert.equal(Number(result.code), 500);
|
|
assert.equal(actionCalls.list.length, 0);
|
|
assert.equal(actionCalls.sign.length, 0);
|
|
});
|
|
});
|