Skip to content

Commit

Permalink
fix: disable on invalid constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Jul 7, 2023
1 parent 4d85328 commit 430c331
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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');
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -16,15 +24,14 @@ export const useConstraintsValidation = (
return;
}

const invalidConstraints = constraints.find(item => !isValid(item));
if (invalidConstraints) {
setValid(false);
return;
}

const validationRequests = constraints
.filter(constraint => {
const hasValues =
Array.isArray(constraint.values) &&
constraint.values.length > 0;
const hasValue = Boolean(constraint.value);

return hasValues || hasValue;
})
.filter(isValid)
.map(constraint => {
return validateConstraint(constraint);
});
Expand All @@ -34,7 +41,7 @@ export const useConstraintsValidation = (
.catch(() => setValid(false));

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [constraints]);
}, [JSON.stringify(constraints)]);

return valid;
};

0 comments on commit 430c331

Please sign in to comment.