Skip to content

Commit

Permalink
Fix error when validating the form with non blocking validations
Browse files Browse the repository at this point in the history
issue: #102338
  • Loading branch information
machadoum committed Jun 29, 2021
1 parent f89dc9c commit 5a7e071
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* 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, FormHook, VALIDATION_TYPES } from '..';

describe('useField() hook', () => {
describe('form.validate()', () => {
test('It should not invalidate a field with arrayItem validation when validating a form', async () => {
let formHook: FormHook;

const TestComp = () => {
const { form } = useForm();
formHook = form;

return (
<Form form={form}>
<UseField
path="test-path"
config={{
validations: [
{
validator: emptyField('error-message'),
type: VALIDATION_TYPES.ARRAY_ITEM,
},
],
}}
/>
</Form>
);
};

registerTestBed(TestComp)();

await act(async () => {
await formHook!.validate();
});

const { isValid } = formHook!;
expect(isValid).toBe(true);
});
});

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 TestComp = () => {
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(TestComp)();

act(() => {
fieldHook!.validate({
value: EMPTY_VALUE,
validationType: VALIDATION_TYPES.ARRAY_ITEM,
});
});

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 TestComp = () => {
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(TestComp)();

act(() => {
fieldHook!.validate({
value: EMPTY_VALUE,
validationType: VALIDATION_TYPES.ARRAY_ITEM,
});
});

expect(fieldHook!.isValid).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export const useField = <T, FormType = FormData, I = T>(
const {
formData = __getFormData$().value,
value: valueToValidate = value,
validationType,
validationType = VALIDATION_TYPES.FIELD,
} = validationData;

setIsValidated(true);
Expand Down

0 comments on commit 5a7e071

Please sign in to comment.