-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error when validating the form with non blocking validations (#10…
…3629) (#105478) * Fix error when validating the form with non blocking validations issue: #102338 * Add a flag to only validate blocking validation when validating the form * Refactor bypass validation Co-authored-by: Sébastien Loix <sabee77@gmail.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Sébastien Loix <sabee77@gmail.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
b8ff424
commit 8018af9
Showing
5 changed files
with
250 additions
and
15 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
src/plugins/es_ui_shared/static/forms/hook_form_lib/hooks/use_field.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { act } from 'react-dom/test-utils'; | ||
import { registerTestBed } from '../shared_imports'; | ||
|
||
import { Form, UseField } from '../components'; | ||
import React from 'react'; | ||
import { useForm } from '.'; | ||
import { emptyField } from '../../helpers/field_validators'; | ||
import { FieldHook, FieldValidateResponse, VALIDATION_TYPES } from '..'; | ||
|
||
describe('useField() hook', () => { | ||
describe('field.validate()', () => { | ||
const EMPTY_VALUE = ' '; | ||
|
||
test('It should not invalidate a field with arrayItem validation when isBlocking is false', async () => { | ||
let fieldHook: FieldHook; | ||
|
||
const TestField = ({ field }: { field: FieldHook }) => { | ||
fieldHook = field; | ||
return null; | ||
}; | ||
|
||
const TestForm = () => { | ||
const { form } = useForm(); | ||
|
||
return ( | ||
<Form form={form}> | ||
<UseField | ||
path="test-path" | ||
component={TestField} | ||
config={{ | ||
validations: [ | ||
{ | ||
validator: emptyField('error-message'), | ||
type: VALIDATION_TYPES.ARRAY_ITEM, | ||
isBlocking: false, | ||
}, | ||
], | ||
}} | ||
/> | ||
</Form> | ||
); | ||
}; | ||
|
||
registerTestBed(TestForm)(); | ||
|
||
let validateResponse: FieldValidateResponse; | ||
|
||
await act(async () => { | ||
validateResponse = await fieldHook!.validate({ | ||
value: EMPTY_VALUE, | ||
validationType: VALIDATION_TYPES.ARRAY_ITEM, | ||
}); | ||
}); | ||
|
||
// validation fails for ARRAY_ITEM with a non-blocking validation error | ||
expect(validateResponse!).toEqual({ | ||
isValid: false, | ||
errors: [ | ||
{ | ||
code: 'ERR_FIELD_MISSING', | ||
path: 'test-path', | ||
message: 'error-message', | ||
__isBlocking__: false, | ||
validationType: 'arrayItem', | ||
}, | ||
], | ||
}); | ||
|
||
// expect the field to be valid because the validation error is non-blocking | ||
expect(fieldHook!.isValid).toBe(true); | ||
}); | ||
|
||
test('It should invalidate an arrayItem field when isBlocking is true', async () => { | ||
let fieldHook: FieldHook; | ||
|
||
const TestField = ({ field }: { field: FieldHook }) => { | ||
fieldHook = field; | ||
return null; | ||
}; | ||
|
||
const TestForm = () => { | ||
const { form } = useForm(); | ||
|
||
return ( | ||
<Form form={form}> | ||
<UseField | ||
path="test-path" | ||
component={TestField} | ||
config={{ | ||
validations: [ | ||
{ | ||
validator: emptyField('error-message'), | ||
type: VALIDATION_TYPES.ARRAY_ITEM, | ||
isBlocking: true, | ||
}, | ||
], | ||
}} | ||
/> | ||
</Form> | ||
); | ||
}; | ||
|
||
registerTestBed(TestForm)(); | ||
|
||
let validateResponse: FieldValidateResponse; | ||
|
||
await act(async () => { | ||
validateResponse = await fieldHook!.validate({ | ||
value: EMPTY_VALUE, | ||
validationType: VALIDATION_TYPES.ARRAY_ITEM, | ||
}); | ||
}); | ||
|
||
// validation fails for ARRAY_ITEM with a blocking validation error | ||
expect(validateResponse!).toEqual({ | ||
isValid: false, | ||
errors: [ | ||
{ | ||
code: 'ERR_FIELD_MISSING', | ||
path: 'test-path', | ||
message: 'error-message', | ||
__isBlocking__: true, | ||
validationType: 'arrayItem', | ||
}, | ||
], | ||
}); | ||
|
||
// expect the field to be invalid because the validation error is blocking | ||
expect(fieldHook!.isValid).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters