tests: Add getImpactsFromSchema tests

This commit is contained in:
Alexis Métaireau 2024-03-21 15:37:17 +01:00
parent 55cc7a098f
commit ff019d08de
2 changed files with 62 additions and 5 deletions

View file

@ -28,13 +28,14 @@ export function checkId(string) {
/** /**
* Compute the impacts for a given list of fields. * Compute the impacts for a given list of fields.
* *
* Return a set containing the impacts. * Return an array of unique impacts.
* *
* @param {fields} list[fields] * @param {fields} list[fields]
* @returns Set[string] * @returns Array[string]
*/ */
export function getImpactsFromSchema(fields) { export function getImpactsFromSchema(fields, schema) {
return fields schema = schema || U.SCHEMA
let impacted = fields
.map((field) => { .map((field) => {
// remove the option prefix for fields // remove the option prefix for fields
// And only keep the first part in case of a subfield // And only keep the first part in case of a subfield
@ -44,10 +45,12 @@ export function getImpactsFromSchema(fields) {
.reduce((acc, field) => { .reduce((acc, field) => {
// retrieve the "impacts" field from the schema // retrieve the "impacts" field from the schema
// and merge them together using sets // and merge them together using sets
const impacts = U.SCHEMA[field]?.impacts || [] const impacts = schema[field]?.impacts || []
impacts.forEach((impact) => acc.add(impact)) impacts.forEach((impact) => acc.add(impact))
return acc return acc
}, new Set()) }, new Set())
return Array.from(impacted)
} }
/** /**

View file

@ -546,4 +546,58 @@ describe('L.Util', function () {
assert.equal(features[2], feat2) assert.equal(features[2], feat2)
}) })
}) })
describe('#getImpactsFromSchema()', function () {
let getImpactsFromSchema = U.Utils.getImpactsFromSchema
it('should return an array', function () {
expect(getImpactsFromSchema(['foo'], {})).to.be.an('array')
expect(getImpactsFromSchema(['foo'], { foo: {} })).to.be.an('array')
expect(getImpactsFromSchema(['foo'], { foo: { impacts: [] } })).to.be.an('array')
expect(getImpactsFromSchema(['foo'], { foo: { impacts: ['A'] } })).to.be.an(
'array'
)
})
it('should return a list of unique impacted values', function () {
let schema = {
foo: { impacts: ['A'] },
bar: { impacts: ['A', 'B'] },
baz: { impacts: ['B', 'C'] },
}
assert.deepEqual(getImpactsFromSchema(['foo'], schema), ['A'])
assert.deepEqual(getImpactsFromSchema(['foo', 'bar'], schema), ['A', 'B'])
assert.deepEqual(getImpactsFromSchema(['foo', 'bar', 'baz'], schema), [
'A',
'B',
'C',
])
})
it('should return an empty list if nothing is found', function () {
let schema = {
foo: { impacts: ['A'] },
bar: { impacts: ['A', 'B'] },
baz: { impacts: ['B', 'C'] },
}
assert.deepEqual(getImpactsFromSchema(['bad'], schema), [])
})
it('should return an empty list if the schema key does not exist', function () {
let schema = {
foo: { impacts: ['A'] },
}
assert.deepEqual(getImpactsFromSchema(['bad'], schema), [])
})
it('should work if the "impacts" key is not defined', function () {
let schema = {
foo: {},
bar: { impacts: ['A'] },
baz: { impacts: ['B'] },
}
assert.deepEqual(getImpactsFromSchema(['foo', 'bar', 'baz'], schema), ['A', 'B'])
})
})
}) })