diff --git a/server/resolvers/ScenarioResult/assertionResultsResolver.js b/server/resolvers/ScenarioResult/assertionResultsResolver.js index 41513915b..f537179d9 100644 --- a/server/resolvers/ScenarioResult/assertionResultsResolver.js +++ b/server/resolvers/ScenarioResult/assertionResultsResolver.js @@ -1,4 +1,4 @@ -const convertAssertionPriority = require('../helpers/convertAssertionPriority'); +const convertAssertionPriority = require('../../util/convertAssertionPriority'); const assertionResultsResolver = (scenarioResult, { priority }) => { if (!priority) return scenarioResult.assertionResults; diff --git a/server/resolvers/helpers/convertAssertionPriority.js b/server/resolvers/helpers/convertAssertionPriority.js deleted file mode 100644 index 9590a6c49..000000000 --- a/server/resolvers/helpers/convertAssertionPriority.js +++ /dev/null @@ -1,7 +0,0 @@ -const convertAssertionPriority = priority => { - if (priority === 'REQUIRED') return 'MUST'; - if (priority === 'OPTIONAL') return 'SHOULD'; - return priority; -}; - -module.exports = convertAssertionPriority; diff --git a/server/scripts/import-tests/index.js b/server/scripts/import-tests/index.js index 65deb5c07..949ff8419 100644 --- a/server/scripts/import-tests/index.js +++ b/server/scripts/import-tests/index.js @@ -27,6 +27,7 @@ const { const deepPickEqual = require('../../util/deepPickEqual'); const { hashTests } = require('../../util/aria'); const convertDateToString = require('../../util/convertDateToString'); +const convertAssertionPriority = require('../../util/convertAssertionPriority'); const args = require('minimist')(process.argv.slice(2), { alias: { @@ -475,15 +476,10 @@ const getTests = ({ assertion.assertionId ) .map(({ priority: assertionPriority }) => { - let priority = ''; - if (assertionPriority === 0) - priority = 'EXCLUDE'; - if (assertionPriority === 1) - priority = 'MUST'; - if (assertionPriority === 2) - priority = 'SHOULD'; - if (assertionPriority === 3) - priority = 'MAY'; + let priority = + convertAssertionPriority( + assertionPriority + ); return { priority, diff --git a/server/util/convertAssertionPriority.js b/server/util/convertAssertionPriority.js new file mode 100644 index 000000000..9265ae177 --- /dev/null +++ b/server/util/convertAssertionPriority.js @@ -0,0 +1,23 @@ +/** + * This should take any of the valid priority inputs and return the currently known string + * representation of that value. + * TODO: Eventually, this should only need to take 'number' types as priority once 'REQUIRED' and + * 'OPTIONAL' are no longer used + * + * @param {number|string} priority + * @returns {null|string} + */ +const convertAssertionPriority = priority => { + const validInputRegex = + /^(0|1|2|3|EXCLUDE|REQUIRED|MUST|OPTIONAL|SHOULD|MAY)$/; + + if (!validInputRegex.test(priority)) return null; + + if (priority === 0) return 'EXCLUDE'; + if (priority === 'REQUIRED' || priority === 1) return 'MUST'; + if (priority === 'OPTIONAL' || priority === 2) return 'SHOULD'; + if (priority === 3) return 'MAY'; + return priority; +}; + +module.exports = convertAssertionPriority; diff --git a/server/util/convertAssertionPriority.test.js b/server/util/convertAssertionPriority.test.js new file mode 100644 index 000000000..3a5799bb0 --- /dev/null +++ b/server/util/convertAssertionPriority.test.js @@ -0,0 +1,55 @@ +const convertAssertionPriority = require('./convertAssertionPriority'); + +describe('Verify expected values are returned when calling convertAssertionPriority', () => { + it('expects valid priority strings are returned for known priority inputs', () => { + const excludePriorityA = convertAssertionPriority(0); + const excludePriorityB = convertAssertionPriority('EXCLUDE'); + + const mustPriorityA = convertAssertionPriority(1); + const mustPriorityB = convertAssertionPriority('REQUIRED'); + const mustPriorityC = convertAssertionPriority('MUST'); + + const shouldPriorityA = convertAssertionPriority(2); + const shouldPriorityB = convertAssertionPriority('OPTIONAL'); + const shouldPriorityC = convertAssertionPriority('SHOULD'); + + const mayPriorityA = convertAssertionPriority(3); + const mayPriorityB = convertAssertionPriority('MAY'); + + expect(excludePriorityA).toEqual('EXCLUDE'); + expect(excludePriorityB).toEqual('EXCLUDE'); + + expect(mustPriorityA).toEqual('MUST'); + expect(mustPriorityB).toEqual('MUST'); + expect(mustPriorityC).toEqual('MUST'); + + expect(shouldPriorityA).toEqual('SHOULD'); + expect(shouldPriorityB).toEqual('SHOULD'); + expect(shouldPriorityC).toEqual('SHOULD'); + + expect(mayPriorityA).toEqual('MAY'); + expect(mayPriorityB).toEqual('MAY'); + }); + + it('expects null values are returned for unknown priority inputs', () => { + const invalidInputA = convertAssertionPriority(-1); + const invalidInputB = convertAssertionPriority(4); + const invalidInputC = convertAssertionPriority(12); + const invalidInputD = convertAssertionPriority('EXCLUDED'); + const invalidInputE = convertAssertionPriority('RANDOM STRING'); + + expect(invalidInputA).not.toEqual('MUST'); + expect(invalidInputA).toEqual(null); + + expect(invalidInputB).toEqual(null); + + expect(invalidInputC).not.toEqual('MUST'); + expect(invalidInputC).not.toEqual('SHOULD'); + expect(invalidInputC).toEqual(null); + + expect(invalidInputD).not.toEqual('EXCLUDE'); + expect(invalidInputD).toEqual(null); + + expect(invalidInputE).toEqual(null); + }); +}); diff --git a/server/util/getMetrics.js b/server/util/getMetrics.js index d9f1168d2..d5b7e9feb 100644 --- a/server/util/getMetrics.js +++ b/server/util/getMetrics.js @@ -1,4 +1,4 @@ -const convertAssertionPriority = require('../resolvers/helpers/convertAssertionPriority'); +const convertAssertionPriority = require('./convertAssertionPriority'); const sum = arr => arr.reduce((total, item) => total + item, 0);