Skip to content

Commit

Permalink
more tests, fixed empty value checks for string conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
mitodrummer committed May 15, 2023
1 parent 6b76a1d commit 96352a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 8 additions & 1 deletion x-pack/plugins/cloud_defend/public/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ export function validateStringValuesForCondition(condition: SelectorCondition, v
const { pattern, patternError } = SelectorConditionsMap[condition];

values?.forEach((value) => {
if (pattern && !new RegExp(pattern).test(value)) {
if (value.length === 0) {
errors.push(
i18n.translate('xpack.cloudDefend.errorGenericEmptyValue', {
defaultMessage: '"{condition}" values cannot be empty',
values: { condition, pattern },
})
);
} else if (pattern && !new RegExp(pattern).test(value)) {
if (patternError) {
errors.push(patternError);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,27 +348,32 @@ describe('<ControlGeneralViewSelector />', () => {
}

updatedSelector = onChange.mock.calls[1][0];
expect(updatedSelector.hasErrors).toBeFalsy();
rerender(<WrappedComponent selector={updatedSelector} />);

expect(findByText(errorStr)).toMatchObject({});

userEvent.type(el, '/*{enter}');
updatedSelector = onChange.mock.calls[2][0];
expect(updatedSelector.hasErrors).toBeFalsy();
rerender(<WrappedComponent selector={updatedSelector} />);

expect(findByText(errorStr)).toMatchObject({});

userEvent.type(el, '/usr/bin/ls{enter}');
updatedSelector = onChange.mock.calls[3][0];
expect(updatedSelector.hasErrors).toBeFalsy();
rerender(<WrappedComponent selector={updatedSelector} />);

expect(findByText(errorStr)).toMatchObject({});

userEvent.type(el, 'badpath{enter}');
updatedSelector = onChange.mock.calls[4][0];

expect(updatedSelector.hasErrors).toBeTruthy();
rerender(<WrappedComponent selector={updatedSelector} />);
expect(getByText(errorStr)).toBeTruthy();

userEvent.type(el, ' {enter}');
updatedSelector = onChange.mock.calls[4][0];
expect(updatedSelector.hasErrors).toBeTruthy();
rerender(<WrappedComponent selector={updatedSelector} />);
expect(getByText(errorStr)).toBeTruthy();
});

Expand Down

0 comments on commit 96352a1

Please sign in to comment.