179 lines
4.4 KiB
JavaScript
179 lines
4.4 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const ExpressMethodService = require('../../../../../app/service/systemMethods/setting/express');
|
|
|
|
function createContext() {
|
|
const calls = {
|
|
list: [],
|
|
create: [],
|
|
update: [],
|
|
checkCodeUnique: [],
|
|
listBase: 0,
|
|
prepareWhereContext: 0,
|
|
};
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {
|
|
jwt: {},
|
|
},
|
|
Sequelize: {},
|
|
},
|
|
service: {
|
|
systems: {
|
|
setting: {
|
|
express: {
|
|
async list(payload) {
|
|
calls.list.push(payload);
|
|
payload.touched = true;
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
list: [
|
|
{ id: 1, code: 'SF-001' },
|
|
],
|
|
},
|
|
};
|
|
},
|
|
async listBase() {
|
|
calls.listBase += 1;
|
|
return {
|
|
code: 200,
|
|
data: [
|
|
{ code: 'SF', name: '顺丰' },
|
|
],
|
|
};
|
|
},
|
|
async create(payload) {
|
|
calls.create.push(payload);
|
|
payload.touched = true;
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
id: 99,
|
|
},
|
|
};
|
|
},
|
|
async checkCodeUnique(payload) {
|
|
calls.checkCodeUnique.push(payload);
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
isUnique: true,
|
|
},
|
|
};
|
|
},
|
|
async update(payload) {
|
|
calls.update.push(payload);
|
|
payload.touched = true;
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
id: 11,
|
|
},
|
|
};
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/setting/express.test.js', () => {
|
|
it('should copy payload before calling list', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new ExpressMethodService(ctx);
|
|
service.prepareWhereContext = function prepareWhereContext(data) {
|
|
calls.prepareWhereContext += 1;
|
|
data.where = {
|
|
quickWhere: 'SF',
|
|
};
|
|
return data;
|
|
};
|
|
|
|
const payload = {
|
|
page: 1,
|
|
page_size: 20,
|
|
where: {
|
|
quickWhere: 'SF',
|
|
},
|
|
extra: {
|
|
keep: true,
|
|
},
|
|
};
|
|
|
|
const result = await service.list(payload);
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(calls.prepareWhereContext, 1);
|
|
assert.equal(calls.list.length, 1);
|
|
assert.notStrictEqual(calls.list[0], payload);
|
|
assert.equal(payload.touched, undefined);
|
|
assert.deepEqual(calls.list[0].where, { quickWhere: 'SF' });
|
|
assert.deepEqual(calls.list[0].pagination, {
|
|
page: 1,
|
|
pageSize: 20,
|
|
});
|
|
});
|
|
|
|
it('should forward normalized create payload and keep the original payload intact', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new ExpressMethodService(ctx);
|
|
|
|
const payload = {
|
|
code: 'sf-001',
|
|
name: '顺丰设置',
|
|
express_code: 'SF',
|
|
extra: {
|
|
keep: true,
|
|
},
|
|
};
|
|
|
|
const result = await service.create(payload);
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(calls.create.length, 1);
|
|
assert.notStrictEqual(calls.create[0], payload);
|
|
assert.deepEqual(calls.create[0], {
|
|
code: 'sf-001',
|
|
name: '顺丰设置',
|
|
express_code: 'SF',
|
|
extra: {
|
|
keep: true,
|
|
},
|
|
touched: true,
|
|
});
|
|
assert.equal(payload.touched, undefined);
|
|
});
|
|
|
|
it('should delegate listBase and checkCodeUnique to the setting express service', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new ExpressMethodService(ctx);
|
|
|
|
const baseResult = await service.listBase();
|
|
const checkResult = await service.checkCodeUnique({
|
|
id: 11,
|
|
code: 'SF-001',
|
|
});
|
|
|
|
assert.equal(calls.listBase, 1);
|
|
assert.equal(calls.checkCodeUnique.length, 1);
|
|
assert.deepEqual(calls.checkCodeUnique[0], {
|
|
id: 11,
|
|
code: 'SF-001',
|
|
});
|
|
assert.equal(baseResult.flag, 'success');
|
|
assert.equal(checkResult.flag, 'success');
|
|
assert.equal(checkResult.data.isUnique, true);
|
|
});
|
|
});
|