UNPKG

ring-websites-toolbelt

Version:

Ring Publishing Platform tool to work with Ring Websites

65 lines (54 loc) 3.25 kB
const fs = require('fs'); const path = require('path'); const { has } = require('lodash'); const { RoutersManager } = require('ring-websites-routers'); const testConfig = require('./config'); const { getPortalStructure, getNamespaceSettings, testRegexpExpetedData } = require('./helpers'); const testDirectories = fs.readdirSync('./tests', { withFileTypes: true }) .map(dirent => dirent.name ? dirent.name : dirent) .filter((dirent) => (fs.statSync(`./tests/${dirent}`)).isDirectory()); testDirectories.forEach((testDirectory) => { console.info(`Running test directory: ./tests/${testDirectory}`); const testPath = path.resolve(`./tests/${testDirectory}`); const portalStructure = getPortalStructure(testPath); const namespaceSettings = getNamespaceSettings(testPath); const routersManager = new RoutersManager({ mode: 'LOCAL' }); let testCases; Object.keys(testConfig).forEach((routerClass) => { const functionTypes = testConfig[routerClass].types; const requiredParams = testConfig[routerClass].requiredParams; Object.keys(functionTypes).forEach((functionType) => { const testsPath = `./tests/${testDirectory}/testCases/${routerClass}/${functionType}.json`; const functionName = functionTypes[functionType]; if (fs.existsSync(testsPath)) { try { testCases = JSON.parse(fs.readFileSync(path.resolve(testsPath), 'utf8')); } catch (error) { console.error(`Unable to parse testCases: ${testsPath}`); throw error; } if (testCases.length) { describe(`[${testDirectory}] ${routerClass}/${functionType}:`, () => { testCases.forEach((testCase, idx) => { it(`passes testCase nr. ${idx}`, async () => { expect(testCase.inputParams).not.toBeUndefined('Missing "inputParams" key in test case'); expect(testCase.expectedData).not.toBeUndefined('Missing "expectedData" key in test case'); requiredParams[functionType].forEach((key) => { expect(has(testCase, key)).toBe(true, `MISSING key in test case definition: ${key}`); }); let result = await routersManager[functionName](null, namespaceSettings, portalStructure, testCase.inputParams); if (testCase.expectedData) { expect(result).toBeDefined('No result for provided params'); testRegexpExpetedData(testCase.expectedData, result); expect(testCase.expectedData).toEqual(result); } else { expect(result).not.toBeDefined('Provided params should result in empty response'); } }); }); }); } } }); }); });