[Form lib] Fix validate method returning wrong state when called from onChange handler (#146371)

Fixes https://github.com/elastic/kibana/issues/145846

### Summary
This PR fixes a bug in the form lib where the `validate` method would
return the wrong state when called from an `onChange` handler. See
https://github.com/elastic/kibana/issues/145846#issue-1457815157 for
more details.

Co-authored-by: Muhammad Ibragimov <muhammad.ibragimov@elastic.co>
Co-authored-by: Yaroslav Kuznietsov <kuznetsov.yaroslav.yk@gmail.com>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Muhammad Ibragimov 2022-12-09 09:44:51 +05:00 committed by GitHub
parent cd4030e0bb
commit 6814ed7e91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 181 additions and 24 deletions

View file

@ -72,7 +72,9 @@ describe('Runtime field editor', () => {
let data;
await act(async () => {
({ data } = await lastState.submit());
const submit = lastState.submit();
jest.advanceTimersByTime(0); // advance timers to allow the form to validate
({ data } = await submit);
});
expect(data).toEqual(defaultValue);
@ -93,6 +95,7 @@ describe('Runtime field editor', () => {
await act(async () => {
form.setInputValue('nameField.input', existingConcreteFields[0].name);
jest.advanceTimersByTime(0); // advance timers to allow the form to validate
});
component.update();
@ -117,7 +120,9 @@ describe('Runtime field editor', () => {
});
await act(async () => {
await lastOnChangeCall()[0].submit();
const submit = lastOnChangeCall()[0].submit();
jest.advanceTimersByTime(0); // advance timers to allow the form to validate
await submit;
});
component.update();
@ -145,7 +150,9 @@ describe('Runtime field editor', () => {
const { form } = testBed;
await act(async () => {
await lastOnChangeCall()[0].submit();
const submit = lastOnChangeCall()[0].submit();
jest.advanceTimersByTime(0); // advance timers to allow the form to validate
await submit;
});
expect(lastOnChangeCall()[0].isValid).toBe(true);

View file

@ -33,6 +33,14 @@ const noop = () => {};
const defaultProps = { onSave: noop, onCancel: noop, docLinks };
describe('Runtime field editor flyout', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
test('should have a flyout title', () => {
const { exists, find } = setup(defaultProps);
@ -67,6 +75,7 @@ describe('Runtime field editor flyout', () => {
await act(async () => {
find('saveFieldButton').simulate('click');
jest.advanceTimersByTime(0); // advance timers to allow the form to validate
});
expect(onSave).toHaveBeenCalled();
@ -93,6 +102,7 @@ describe('Runtime field editor flyout', () => {
await act(async () => {
find('saveFieldButton').simulate('click');
jest.advanceTimersByTime(0); // advance timers to allow the form to validate
});
component.update();
@ -115,6 +125,7 @@ describe('Runtime field editor flyout', () => {
await act(async () => {
find('saveFieldButton').simulate('click');
jest.advanceTimersByTime(0);
});
expect(onSave).toHaveBeenCalled();
@ -133,9 +144,11 @@ describe('Runtime field editor flyout', () => {
value: 'other_type',
},
]);
jest.advanceTimersByTime(0);
});
await act(async () => {
find('saveFieldButton').simulate('click');
jest.advanceTimersByTime(0);
});
fieldReturned = onSave.mock.calls[onSave.mock.calls.length - 1][0];
expect(fieldReturned).toEqual({