ring-websites-toolbelt
Version:
Ring Publishing Platform tool to work with Ring Websites
64 lines (50 loc) • 2.38 kB
JavaScript
const fs = require('fs');
function getPortalStructure(testPath) {
if (!fs.existsSync(`${testPath}/portalStructure.json`)) {
throw new Error(`Missing required file: portalStructure.json`);
}
const portalStructure = JSON.parse(fs.readFileSync(`${testPath}/portalStructure.json`, 'utf8'));
return portalStructure.map((row) => ({
slug: row.slug,
portal_structure_uuid: row.id,
category_uuid: row.categoryId,
uuid_path: row.idsPath,
domain: row.domain,
extra_data: row.extraData
}));
}
function getNamespaceSettings(testPath) {
if (!fs.existsSync(`${testPath}/namespaceSettings.json`)) {
throw new Error(`Missing required file: namespaceSettings.json`);
}
const namespaceSettings = JSON.parse(fs.readFileSync(`${testPath}/namespaceSettings.json`, 'utf8'));
namespaceSettings.taxonomiesSettings = namespaceSettings.taxonomiesSettings.map((row) => ({
taxonomy_code: row.taxonomyCode,
taxonomy: row.taxonomy,
autopublish: row.autopublish,
path_prefix: row.pathPrefix,
previous_path_prefixes: row.previousPathPrefixes
}));
return namespaceSettings;
}
function testRegexpExpetedData(expectedData, result, fieldPrefix = '') {
if (expectedData && typeof expectedData === 'object') {
Object.keys(expectedData).forEach((expectedDataField) => {
const expectedValue = expectedData[expectedDataField];
const resultValue = result[expectedDataField];
if (typeof expectedValue === 'string' && (/regexp:(.*)/g).test(expectedValue)) {
const regexpString = expectedValue.substring(7);
const regexp = new RegExp(regexpString, 'g');
expect(regexp.test(resultValue)).toBe(true, `Expected param "${fieldPrefix}${expectedDataField}": "${resultValue}" to pass RegExp "${regexpString}"`);
expectedData[expectedDataField] = resultValue;
} else if (expectedValue && resultValue && typeof expectedValue === 'object' && typeof resultValue === 'object') {
testRegexpExpetedData(expectedValue, resultValue, `${fieldPrefix}${expectedDataField}.`)
}
});
}
}
module.exports = {
getPortalStructure,
getNamespaceSettings,
testRegexpExpetedData
}