Files
core-prestashop/test/status-mapper.test.js

51 lines
2.0 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { resolvePsStateId, readStatusMapping } = require('../src/status-mapper');
const STATUS_A = '507f191e810c19729de860ea';
const STATUS_B = '507f191e810c19729de860eb';
test('resolves a saved mapping in the outbound Orbito to PrestaShop direction', () => {
assert.equal(resolvePsStateId(STATUS_B, [
{ psStateId: 2, orbitoStatusId: STATUS_A },
{ psStateId: 7, orbitoStatusId: STATUS_B },
]), 7);
assert.equal(resolvePsStateId('507f191e810c19729de860ec', [
{ psStateId: 7, orbitoStatusId: STATUS_B },
]), null);
});
test('rejects an ambiguous outbound status mapping', () => {
assert.throws(() => resolvePsStateId(STATUS_A, [
{ psStateId: 2, orbitoStatusId: STATUS_A },
{ psStateId: 3, orbitoStatusId: STATUS_A },
]), /kilku statusów PrestaShop/);
});
test('an outbound override affects only its Orbito status and preserves all other legacy assignments', () => {
const config = { 'mapping.state_2': STATUS_A, 'mapping.state_7': STATUS_B, [`mapping.orbito_${STATUS_A}`]: '7' };
const mapping = readStatusMapping(config);
assert.equal(resolvePsStateId(STATUS_A, mapping), 7);
assert.equal(resolvePsStateId(STATUS_B, mapping), 7);
assert.equal(config['mapping.state_2'], STATUS_A);
});
test('clearing an outbound field never resurrects its legacy mapping', () => {
for (const empty of ['', null]) {
const mapping = readStatusMapping({
'mapping.state_2': STATUS_A,
'mapping.state_7': STATUS_B,
[`mapping.orbito_${STATUS_A}`]: empty,
});
assert.equal(resolvePsStateId(STATUS_A, mapping), null);
assert.equal(resolvePsStateId(STATUS_B, mapping), 7);
}
});
test('invalid saved outbound state fails safely instead of sending a truncated state ID', () => {
for (const value of ['2foo', '-2', '0', '2.5']) {
assert.throws(() => readStatusMapping({ [`mapping.orbito_${STATUS_A}`]: value }), /Niepoprawny status/);
}
});