194 lines
5.7 KiB
JavaScript
194 lines
5.7 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const PrintService = require('../../../../app/service/systemMethods/print');
|
|
|
|
/**
|
|
* 构造 savePrinters 测试上下文。
|
|
* @param {Object} [options] 测试选项。
|
|
* @param {Object|null} [options.stationItem] 打印台模型实例。
|
|
* @return {{ctx: Object, calls: Object, stationItem: Object|null}} 测试上下文与调用记录。
|
|
*/
|
|
function createContext(options = {}) {
|
|
const calls = {
|
|
findOneArgs: [],
|
|
normalizeSubjectConfigArgs: [],
|
|
saveCount: 0,
|
|
};
|
|
|
|
const stationItem = options.stationItem || {
|
|
id: 1,
|
|
deleted: 0,
|
|
default_printer_name: '旧默认打印机',
|
|
subject_config: {
|
|
keep_field: 'keep-value',
|
|
express_bind_printer: [
|
|
{
|
|
printer_name: '旧打印机',
|
|
express_code: 'OLD',
|
|
},
|
|
],
|
|
},
|
|
async save() {
|
|
calls.saveCount += 1;
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {
|
|
jwt: {},
|
|
},
|
|
Sequelize: {},
|
|
},
|
|
user: {
|
|
store_id: 100000,
|
|
},
|
|
model: {
|
|
PrintStations: {
|
|
async findOne(payload) {
|
|
calls.findOneArgs.push(payload);
|
|
return options.stationItem === null ? null : stationItem;
|
|
},
|
|
normalizeSubjectConfig(config) {
|
|
calls.normalizeSubjectConfigArgs.push(config);
|
|
if (!config || typeof config !== 'object' || Array.isArray(config)) {
|
|
throw new Error('subject_config 必须是对象');
|
|
}
|
|
if (!Array.isArray(config.express_bind_printer)) {
|
|
throw new Error('subject_config.express_bind_printer 必须是数组');
|
|
}
|
|
const normalizedList = config.express_bind_printer.map((item, index) => {
|
|
const printerName = String(item.printer_name === undefined || item.printer_name === null ? '' : item.printer_name).trim();
|
|
const expressCode = String(item.express_code === undefined || item.express_code === null ? '' : item.express_code).trim();
|
|
if (!printerName) {
|
|
throw new Error(`subject_config.express_bind_printer[${index}].printer_name 不能为空`);
|
|
}
|
|
if (!expressCode) {
|
|
throw new Error(`subject_config.express_bind_printer[${index}].express_code 不能为空`);
|
|
}
|
|
return {
|
|
printer_name: printerName,
|
|
express_code: expressCode,
|
|
};
|
|
});
|
|
|
|
return {
|
|
...config,
|
|
express_bind_printer: normalizedList,
|
|
};
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return { ctx, calls, stationItem };
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/print.savePrinters.test.js', () => {
|
|
it('正常保存时应写入 print_stations.subject_config.express_bind_printer', async () => {
|
|
const { ctx, calls, stationItem } = createContext();
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.savePrinters({
|
|
defaultPrinter: '默认打印机A',
|
|
print_station_id: 11,
|
|
express: [
|
|
{
|
|
express_code: 'STO',
|
|
printer_name: '申通打印机',
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.code, 0);
|
|
assert.equal(calls.saveCount, 1);
|
|
assert.equal(stationItem.default_printer_name, '默认打印机A');
|
|
assert.equal(stationItem.subject_config.keep_field, 'keep-value');
|
|
assert.deepEqual(stationItem.subject_config.express_bind_printer, [
|
|
{
|
|
printer_name: '申通打印机',
|
|
express_code: 'STO',
|
|
},
|
|
]);
|
|
assert.equal(calls.findOneArgs.length, 1);
|
|
assert.deepEqual(calls.findOneArgs[0], {
|
|
where: {
|
|
id: 11,
|
|
deleted: 0,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('express 使用 express_id 与 name 时应兼容映射为 express_code 与 printer_name', async () => {
|
|
const { ctx, stationItem } = createContext();
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.savePrinters({
|
|
defaultPrinter: '默认打印机B',
|
|
print_station_id: 12,
|
|
express: [
|
|
{
|
|
express_id: 'YTO',
|
|
name: '圆通打印机',
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.code, 0);
|
|
assert.deepEqual(stationItem.subject_config.express_bind_printer, [
|
|
{
|
|
printer_name: '圆通打印机',
|
|
express_code: 'YTO',
|
|
},
|
|
]);
|
|
});
|
|
|
|
it('print_station_id 不存在时应返回失败', async () => {
|
|
const { ctx, calls } = createContext({ stationItem: null });
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.savePrinters({
|
|
defaultPrinter: '默认打印机C',
|
|
print_station_id: 999999,
|
|
express: [
|
|
{
|
|
express_code: 'ZTO',
|
|
printer_name: '中通打印机',
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(result.flag, 'failure');
|
|
assert.equal(result.code, 1002);
|
|
assert.match(result.message, /打印台不存在/);
|
|
assert.equal(calls.saveCount, 0);
|
|
});
|
|
|
|
it('缺少 defaultPrinter 或 express 时应返回参数错误', async () => {
|
|
const { ctx: ctxA } = createContext();
|
|
const serviceA = new PrintService(ctxA);
|
|
const resultA = await serviceA.savePrinters({
|
|
print_station_id: 13,
|
|
express: [],
|
|
});
|
|
|
|
assert.equal(resultA.flag, 'failure');
|
|
assert.equal(resultA.code, 1001);
|
|
assert.match(resultA.message, /参数错误/);
|
|
|
|
const { ctx: ctxB } = createContext();
|
|
const serviceB = new PrintService(ctxB);
|
|
const resultB = await serviceB.savePrinters({
|
|
defaultPrinter: '默认打印机D',
|
|
print_station_id: 14,
|
|
});
|
|
|
|
assert.equal(resultB.flag, 'failure');
|
|
assert.equal(resultB.code, 1001);
|
|
assert.match(resultB.message, /参数错误/);
|
|
});
|
|
});
|