mirror of
https://github.com/Unleash/unleash.git
synced 2025-03-23 00:16:25 +01:00
fix: constraint validation affecting disabled button (#4183)
This commit is contained in:
parent
1b99b700d6
commit
748bfaad7d
@ -0,0 +1,57 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { IConstraint } from 'interfaces/strategy'; // Assuming you have your component in this path
|
||||
import { FC } from 'react';
|
||||
import { useConstraintsValidation } from './useConstraintsValidation';
|
||||
import { testServerRoute, testServerSetup } from 'utils/testServer';
|
||||
|
||||
const server = testServerSetup();
|
||||
|
||||
const TestComponent: FC<{ constraints: IConstraint[] }> = ({ constraints }) => {
|
||||
const valid = useConstraintsValidation(constraints);
|
||||
|
||||
return <div>{valid ? 'Valid' : 'Invalid'}</div>;
|
||||
};
|
||||
|
||||
it('should display Valid when constraints are valid', async () => {
|
||||
testServerRoute(
|
||||
server,
|
||||
'/api/admin/constraints/validate',
|
||||
{ data: 'OK' },
|
||||
'post'
|
||||
);
|
||||
|
||||
const validConstraints: IConstraint[] = [
|
||||
{
|
||||
value: 'test',
|
||||
values: ['test'],
|
||||
operator: 'IN',
|
||||
contextName: 'irrelevant',
|
||||
},
|
||||
{
|
||||
value: 'test',
|
||||
values: ['test'],
|
||||
operator: 'IN',
|
||||
contextName: 'irrelevant',
|
||||
},
|
||||
];
|
||||
|
||||
render(<TestComponent constraints={validConstraints} />);
|
||||
|
||||
await screen.findByText('Valid');
|
||||
});
|
||||
|
||||
it('should display Invalid when constraints are invalid', async () => {
|
||||
const emptyValueAndValues: IConstraint[] = [
|
||||
{ value: '', values: [], operator: 'IN', contextName: 'irrelevant' },
|
||||
{
|
||||
value: '',
|
||||
values: [],
|
||||
operator: 'IN',
|
||||
contextName: 'irrelevant',
|
||||
},
|
||||
];
|
||||
|
||||
render(<TestComponent constraints={emptyValueAndValues} />);
|
||||
|
||||
await screen.findByText('Invalid');
|
||||
});
|
@ -2,6 +2,14 @@ import useFeatureApi from 'hooks/api/actions/useFeatureApi/useFeatureApi';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { IConstraint } from 'interfaces/strategy';
|
||||
|
||||
const isValid = (constraint: IConstraint) => {
|
||||
const hasValues =
|
||||
Array.isArray(constraint.values) && constraint.values.length > 0;
|
||||
const hasValue = Boolean(constraint.value);
|
||||
|
||||
return hasValues || hasValue;
|
||||
};
|
||||
|
||||
export const useConstraintsValidation = (
|
||||
constraints?: IConstraint[]
|
||||
): boolean => {
|
||||
@ -16,15 +24,14 @@ export const useConstraintsValidation = (
|
||||
return;
|
||||
}
|
||||
|
||||
const validationRequests = constraints
|
||||
.filter(constraint => {
|
||||
const hasValues =
|
||||
Array.isArray(constraint.values) &&
|
||||
constraint.values.length > 0;
|
||||
const hasValue = Boolean(constraint.value);
|
||||
const invalidConstraints = constraints.find(item => !isValid(item));
|
||||
if (invalidConstraints) {
|
||||
setValid(false);
|
||||
return;
|
||||
}
|
||||
|
||||
return hasValues || hasValue;
|
||||
})
|
||||
const validationRequests = constraints
|
||||
.filter(isValid)
|
||||
.map(constraint => {
|
||||
return validateConstraint(constraint);
|
||||
});
|
||||
@ -34,7 +41,7 @@ export const useConstraintsValidation = (
|
||||
.catch(() => setValid(false));
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [constraints]);
|
||||
}, [JSON.stringify(constraints)]);
|
||||
|
||||
return valid;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user