From ff019d08de072d3b616bec8c7247373522838839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Thu, 21 Mar 2024 15:37:17 +0100 Subject: [PATCH] tests: Add `getImpactsFromSchema` tests --- umap/static/umap/js/modules/utils.js | 13 ++++--- umap/static/umap/test/Util.js | 54 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/umap/static/umap/js/modules/utils.js b/umap/static/umap/js/modules/utils.js index 82819ed0..94292d8a 100644 --- a/umap/static/umap/js/modules/utils.js +++ b/umap/static/umap/js/modules/utils.js @@ -28,13 +28,14 @@ export function checkId(string) { /** * 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] - * @returns Set[string] + * @returns Array[string] */ -export function getImpactsFromSchema(fields) { - return fields +export function getImpactsFromSchema(fields, schema) { + schema = schema || U.SCHEMA + let impacted = fields .map((field) => { // remove the option prefix for fields // And only keep the first part in case of a subfield @@ -44,10 +45,12 @@ export function getImpactsFromSchema(fields) { .reduce((acc, field) => { // retrieve the "impacts" field from the schema // and merge them together using sets - const impacts = U.SCHEMA[field]?.impacts || [] + const impacts = schema[field]?.impacts || [] impacts.forEach((impact) => acc.add(impact)) return acc }, new Set()) + + return Array.from(impacted) } /** diff --git a/umap/static/umap/test/Util.js b/umap/static/umap/test/Util.js index 08852926..32c8527a 100644 --- a/umap/static/umap/test/Util.js +++ b/umap/static/umap/test/Util.js @@ -546,4 +546,58 @@ describe('L.Util', function () { 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']) + }) + }) })