105 lines
2.5 KiB
JavaScript
105 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const SettingWarehouseService = require('../../../../../app/service/systems/setting/warehouse');
|
|
|
|
function createHarness(options = {}) {
|
|
const calls = {
|
|
findByPkWarehouseIds: [],
|
|
updatePayload: null,
|
|
cacheDeletes: [],
|
|
};
|
|
|
|
const initialSubjectConfig = options.initialSubjectConfig || {
|
|
max_wave_quantity: 500,
|
|
};
|
|
|
|
const storeRecord = {
|
|
id: Number(options.storeId || 100000),
|
|
subject_config: {
|
|
...initialSubjectConfig,
|
|
},
|
|
async update(payload) {
|
|
calls.updatePayload = {
|
|
...payload,
|
|
};
|
|
this.subject_config = {
|
|
...(payload.subject_config || {}),
|
|
};
|
|
return this;
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
user: {
|
|
store_id: Number(options.storeId || 100000),
|
|
},
|
|
app: {
|
|
async delCacheAsync(key) {
|
|
calls.cacheDeletes.push(key);
|
|
},
|
|
},
|
|
model: {
|
|
Store: {
|
|
async findByPk(warehouseId) {
|
|
calls.findByPkWarehouseIds.push(Number(warehouseId));
|
|
return storeRecord;
|
|
},
|
|
normalizeSubjectConfig(config) {
|
|
return {
|
|
...config,
|
|
};
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
calls,
|
|
storeRecord,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/setting/warehouse.test.js', () => {
|
|
it('should always clear cache for current ctx warehouse when saving settings', async () => {
|
|
const { ctx, calls } = createHarness({
|
|
storeId: 100000,
|
|
});
|
|
const service = new SettingWarehouseService(ctx);
|
|
|
|
const result = await service.saveSettings({
|
|
subject_config: {
|
|
max_wave_quantity: 600,
|
|
},
|
|
cache_warehouse_id: 999999,
|
|
cacheWarehouseId: 888888,
|
|
});
|
|
|
|
assert.deepEqual(calls.findByPkWarehouseIds, [ 100000 ]);
|
|
assert.equal(result.warehouse_id, 100000);
|
|
assert.equal(result.cache_cleared_warehouse_id, 100000);
|
|
assert.equal(calls.updatePayload.subject_config.max_wave_quantity, 600);
|
|
assert.deepEqual(calls.cacheDeletes, [
|
|
'delivery_order:store:100000:subject_config',
|
|
'warehouse:100000store:100000:info',
|
|
]);
|
|
});
|
|
|
|
it('should keep validation behavior for invalid subject_config', async () => {
|
|
const { ctx } = createHarness();
|
|
const service = new SettingWarehouseService(ctx);
|
|
|
|
await assert.rejects(
|
|
service.saveSettings({
|
|
subject_config: null,
|
|
}),
|
|
error => {
|
|
assert.equal(error.code, 400);
|
|
assert.match(error.message, /subject_config 参数不能为空且必须是对象/);
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
});
|