Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ describe('EqlQueryBar', () => {
expect(wrapper.find('[data-test-subj="eqlFilterBar"]')).toHaveLength(1);
});

it('re-validates when index pattern id or title changes', () => {
const validate = jest.fn();
mockField = useFormFieldMock({
value: mockQueryBar,
validate,
});

const { rerender } = render(
<TestProviders>
<EqlQueryBar
dataTestSubj="myQueryBar"
field={mockField}
isLoading={false}
indexPattern={mockIndexPattern}
/>
</TestProviders>
);

expect(validate).toHaveBeenCalledTimes(1);

rerender(
<TestProviders>
<EqlQueryBar
dataTestSubj="myQueryBar"
field={mockField}
isLoading={false}
indexPattern={{ ...mockIndexPattern, title: 'other-*' }}
/>
</TestProviders>
);

expect(validate).toHaveBeenCalledTimes(2);
});

it('should set the field value on input change', () => {
render(
<TestProviders>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export const EqlQueryBar: FC<EqlQueryBarProps> = ({
const { addError } = useAppToasts();
const { uiSettings } = useKibana().services;
const filterManager = useRef<FilterManager>(new FilterManager(uiSettings));
const validateRef = useRef(field.validate);
validateRef.current = field.validate;
const { isValidating, value: fieldValue, setValue: setFieldValue, isValid, errors } = field;
const errorMessages = useMemo(() => errors.map((x) => x.message), [errors]);

Expand Down Expand Up @@ -83,6 +85,14 @@ export const EqlQueryBar: FC<EqlQueryBarProps> = ({
}
}, [errors, addError]);

// `use_field` only re-runs validators when the field value changes. The EQL validator closes
// over the current data view / index pattern, so errors from a previous index can stick around
// after switching back to a valid index until the user edits the query. Re-validate whenever
// the index pattern identity changes.
useEffect(() => {
void validateRef.current();
}, [indexPattern.id, indexPattern.title]);

useEffect(() => {
if (onValidatingChange) {
onValidatingChange(isValidating);
Expand Down
Loading