Skip to content

Commit

Permalink
Improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoum committed Jul 5, 2021
1 parent aeaeb05 commit 9cf3d17
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,9 @@ import { Form, UseField } from '../components';
import React from 'react';
import { useForm } from '.';
import { emptyField } from '../../helpers/field_validators';
import { FieldHook, FormHook, VALIDATION_TYPES } from '..';
import { FieldHook, FieldValidateResponse, 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 = ' ';

Expand Down Expand Up @@ -87,13 +51,30 @@ describe('useField() hook', () => {

registerTestBed(TestForm)();

act(() => {
fieldHook!.validate({
let validateResponse: FieldValidateResponse;

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

// Expect 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);
});

Expand Down Expand Up @@ -129,13 +110,30 @@ describe('useField() hook', () => {

registerTestBed(TestForm)();

act(() => {
fieldHook!.validate({
let validateResponse: FieldValidateResponse;

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

// Expect 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);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import React, { useEffect } from 'react';
import { act } from 'react-dom/test-utils';

import { registerTestBed, getRandomString, TestBed } from '../shared_imports';

import { emptyField } from '../../helpers/field_validators';
import { Form, UseField } from '../components';
import {
FormSubmitHandler,
OnUpdateHandler,
FormHook,
ValidationFunc,
FieldConfig,
} from '../types';
VALIDATION_TYPES,
} from '..';
import { useForm } from './use_form';

interface MyForm {
Expand Down Expand Up @@ -501,4 +502,39 @@ describe('useForm() hook', () => {
expect(isValid).toBeUndefined(); // Make sure it is "undefined" and not "false".
});
});

describe('form.validate()', () => {
test('should not invalidate a field with arrayItem validation when validating a form', async () => {
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)();

let isValid: boolean = false;

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

expect(isValid).toBe(true);
});
});
});

0 comments on commit 9cf3d17

Please sign in to comment.