From d0975c52a917e39408c50e8946cf46a6a839884f Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 12 May 2025 14:57:55 +0200 Subject: [PATCH] Test(1-3733): get invalid and deleted legal values (#9963) Extracts and tests the implementation of the functions to get deleted and invalid legal values. This is pretty straightforward stuff and arguably not crucial, but it was an easy place to start and I don't think it hurts to have these in place anyway. --- .../legal-value-functions.test.ts | 20 +++++++++++++++ .../legal-value-functions.ts | 25 +++++++++++++++++++ .../useEditableConstraint.tsx | 23 +++++++---------- 3 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/legal-value-functions.test.ts create mode 100644 frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/legal-value-functions.ts diff --git a/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/legal-value-functions.test.ts b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/legal-value-functions.test.ts new file mode 100644 index 0000000000..5655839ad4 --- /dev/null +++ b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/legal-value-functions.test.ts @@ -0,0 +1,20 @@ +import { + getDeletedLegalValues, + getInvalidLegalValues, +} from './legal-value-functions'; + +test('should return deleted legal values', () => { + const deletedLegalValues = getDeletedLegalValues( + [{ value: 'A' }, { value: 'B' }], + ['A', 'C'], + ); + expect([...deletedLegalValues]).toStrictEqual(['C']); +}); + +test('should return invalid legal values', () => { + const invalidLegalValues = getInvalidLegalValues( + (value: string) => value === 'B', + [{ value: 'A' }, { value: 'B' }], + ); + expect([...invalidLegalValues]).toEqual(['A']); +}); diff --git a/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/legal-value-functions.ts b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/legal-value-functions.ts new file mode 100644 index 0000000000..bb07886864 --- /dev/null +++ b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/legal-value-functions.ts @@ -0,0 +1,25 @@ +import type { ILegalValue } from 'interfaces/context'; +import { difference } from './set-functions'; + +export const getDeletedLegalValues = ( + allLegalValues: ILegalValue[], + selectedLegalValues: string[], +): Set => { + const currentLegalValues = new Set( + allLegalValues.map(({ value }) => value), + ); + const deletedValues = difference(selectedLegalValues, currentLegalValues); + + return deletedValues; +}; + +export const getInvalidLegalValues = ( + validate: (value: string) => boolean, + allLegalValues: ILegalValue[], +): Set => { + return new Set( + allLegalValues + .filter(({ value }) => !validate(value)) + .map(({ value }) => value), + ); +}; diff --git a/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/useEditableConstraint.tsx b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/useEditableConstraint.tsx index 76c246074d..ffdfcc0e0c 100644 --- a/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/useEditableConstraint.tsx +++ b/frontend/src/component/feature/FeatureStrategy/FeatureStrategyConstraints/useEditableConstraint/useEditableConstraint.tsx @@ -20,7 +20,10 @@ import { type ConstraintValidationResult, constraintValidator, } from './constraint-validator'; -import { difference } from './set-functions'; +import { + getDeletedLegalValues, + getInvalidLegalValues, +} from './legal-value-functions'; const resolveContextDefinition = ( context: IUnleashContextDefinition[], @@ -88,16 +91,10 @@ export const useEditableConstraint = ( contextDefinition.legalValues?.length && constraint.values?.length ) { - // todo: extract and test - const currentLegalValues = new Set( - contextDefinition.legalValues.map(({ value }) => value), - ); - const deletedValues = difference( + return getDeletedLegalValues( + contextDefinition.legalValues, constraint.values, - currentLegalValues, ); - - return deletedValues; } return undefined; }, [ @@ -110,11 +107,9 @@ export const useEditableConstraint = ( contextDefinition.legalValues?.length && isSingleValueConstraint(localConstraint) ) { - // todo: extract and test - return new Set( - contextDefinition.legalValues - .filter(({ value }) => !validator(value)[0]) - .map(({ value }) => value), + return getInvalidLegalValues( + (value) => validator(value)[0], + contextDefinition.legalValues, ); } return undefined;