72 lines
2.5 KiB
JavaScript
72 lines
2.5 KiB
JavaScript
const assert = require('node:assert/strict');
|
|
const Module = require('node:module');
|
|
const path = require('node:path');
|
|
const test = require('node:test');
|
|
|
|
const registerConfigPath = path.resolve(__dirname, '../src/register-config.js');
|
|
|
|
test('saving a valid shop connection refreshes the persisted source icon', async () => {
|
|
let blocks;
|
|
let iconOptions;
|
|
let savedStates;
|
|
const originalLoad = Module._load;
|
|
Module._load = function(request, parent, isMain) {
|
|
if (request === '@orbito/api-lib') {
|
|
return {
|
|
Config: {
|
|
PropertyType: { INFO: 'info', STRING: 'string' },
|
|
DisplayFormat: { URL: 'url', SECRET: 'secret' },
|
|
registerConfigBlocks: async value => { blocks = value; },
|
|
},
|
|
Log: { info() {}, warn() {} },
|
|
};
|
|
}
|
|
if (request === './PrestaApi') {
|
|
return class PrestashopApi {
|
|
async getStatuses() {
|
|
return [{ id: '2', name: 'Payment accepted', color: '#00ff00' }];
|
|
}
|
|
};
|
|
}
|
|
if (request === './utils') return { isLegacyApi: () => false };
|
|
if (request === './status-mapper') return { extractPsStateName: value => value };
|
|
if (request === './module-config') {
|
|
return {
|
|
initializeOutboundMapping: async () => {},
|
|
setPsStates: async value => { savedStates = value; },
|
|
loadPsStates: async () => [],
|
|
};
|
|
}
|
|
if (request === './shop-icon') {
|
|
return { ensureShopIcon: async options => { iconOptions = options; } };
|
|
}
|
|
return originalLoad.call(this, request, parent, isMain);
|
|
};
|
|
|
|
delete require.cache[registerConfigPath];
|
|
try {
|
|
const { registerPrestashopConfigBlocks } = require(registerConfigPath);
|
|
await registerPrestashopConfigBlocks();
|
|
} finally {
|
|
Module._load = originalLoad;
|
|
}
|
|
|
|
const errors = [];
|
|
await blocks.connection.validate({
|
|
apiUrl: 'https://shop.test',
|
|
apiKey: 'secret',
|
|
versionCode: '8',
|
|
shopName: 'Sklep główny',
|
|
}, {
|
|
addError: (field, message) => errors.push({ field, message }),
|
|
});
|
|
|
|
assert.deepEqual(errors, []);
|
|
assert.deepEqual(savedStates, [{ id: 2, name: 'Payment accepted', color: '#00ff00' }]);
|
|
assert.deepEqual(iconOptions, {
|
|
apiUrl: 'https://shop.test',
|
|
shopName: 'Sklep główny',
|
|
force: true,
|
|
});
|
|
});
|