Skip to content

Commit 0fcca0a

Browse files
committed
Add tests for validator warnings
1 parent 9b474fb commit 0fcca0a

File tree

5 files changed

+115
-6
lines changed

5 files changed

+115
-6
lines changed

packages/react-form-renderer/src/files/compose-validators.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const composeValidators = (validators = []) => (value, allValues, meta) => {
55
return error;
66
}
77

8+
if (typeof validator !== 'function') {
9+
return undefined;
10+
}
11+
812
return validator(value, allValues, meta);
913
};
1014

packages/react-form-renderer/src/files/validator-mapper.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { ValidatorFunction } from "./validators";
2+
13
export interface ValidatorMapper {
2-
[key: string]: (options?: object) => (value: any, allValues: object) => string | undefined;
4+
[key: string]: (options?: object) => ValidatorFunction;
35
}
46

57
declare const validatorMapper: ValidatorMapper;

packages/react-form-renderer/src/files/validators.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface ValidatorConfiguration extends AnyObject {
99
type: string;
1010
message?: string;
1111
msg?: string;
12+
warning?: boolean;
1213
}
1314

1415
export type Validator = ValidatorConfiguration | ValidatorFunction;

packages/react-form-renderer/src/form-renderer/validator-helpers.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { ValidatorType } from "../files/use-field-api";
22
import { ReactNode } from "react";
33
import { ValidatorMapper } from "../files/validator-mapper";
44
import { DataType } from "../files/data-types";
5-
6-
export type ValidatorFunction = (value: any, allValues: object) => ReactNode | undefined;
5+
import { ValidatorFunction } from "../files/validators";
76

87
export type convertToWarning = (validator: ValidatorType) => ValidatorFunction;
98

packages/react-form-renderer/src/tests/form-renderer/validator.test.js

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3+
import { act } from 'react-dom/test-utils';
34

45
import FormRenderer from '../../files/form-renderer';
56
import componentTypes from '../../files/component-types';
67
import FormTemplate from '../../../../../__mocks__/mock-form-template';
78
import useFieldApi from '../../files/use-field-api';
8-
import { act } from 'react-dom/test-utils';
99

1010
describe('FormRenderer validator', () => {
1111
const TextField = (props) => {
@@ -18,11 +18,12 @@ describe('FormRenderer validator', () => {
1818
);
1919
};
2020

21+
const VALUE = 'some-value';
22+
const NAME = 'field1';
23+
2124
it('pass value, allvalues, meta to custom validator func', async () => {
2225
expect.assertions(3);
2326

24-
const VALUE = 'some-value';
25-
const NAME = 'field1';
2627
const META = expect.any(Object);
2728

2829
const validator = (value, allValues, meta) => {
@@ -55,4 +56,106 @@ describe('FormRenderer validator', () => {
5556
wrapper.find('input').simulate('change', { target: { value: VALUE } });
5657
});
5758
});
59+
60+
describe('warning validators', () => {
61+
const TextFieldWarning = (props) => {
62+
const { input, meta, ...rest } = useFieldApi(props);
63+
return (
64+
<div>
65+
<input {...input} {...rest} />
66+
{meta.warning && <div id="warning">{meta.warning}</div>}
67+
</div>
68+
);
69+
};
70+
71+
let wrapper;
72+
73+
it('should not convert object validator to warning when warnings are not used', async () => {
74+
await act(async () => {
75+
wrapper = mount(
76+
<FormRenderer
77+
FormTemplate={FormTemplate}
78+
componentMapper={{
79+
[componentTypes.TEXT_FIELD]: TextFieldWarning
80+
}}
81+
schema={{
82+
fields: [{ component: 'text-field', name: NAME, validate: [{ type: 'required', warning: true }] }]
83+
}}
84+
onSubmit={jest.fn()}
85+
/>
86+
);
87+
});
88+
wrapper.update();
89+
90+
expect(wrapper.find('#warning')).toHaveLength(0);
91+
});
92+
93+
it('should convert object validator to warning', async () => {
94+
await act(async () => {
95+
wrapper = mount(
96+
<FormRenderer
97+
FormTemplate={FormTemplate}
98+
componentMapper={{
99+
[componentTypes.TEXT_FIELD]: TextFieldWarning
100+
}}
101+
schema={{
102+
fields: [{ useWarnings: true, component: 'text-field', name: NAME, validate: [{ type: 'required', warning: true }] }]
103+
}}
104+
onSubmit={jest.fn()}
105+
/>
106+
);
107+
});
108+
wrapper.update();
109+
110+
expect(wrapper.find('#warning').text()).toEqual('Required');
111+
});
112+
113+
it('should convert function validator to warning', async () => {
114+
const ERROR = 'SOME-ERROR';
115+
116+
const customValidator = () => ({ type: 'warning', error: ERROR });
117+
118+
await act(async () => {
119+
wrapper = mount(
120+
<FormRenderer
121+
FormTemplate={FormTemplate}
122+
componentMapper={{
123+
[componentTypes.TEXT_FIELD]: TextFieldWarning
124+
}}
125+
schema={{
126+
fields: [{ useWarnings: true, component: 'text-field', name: NAME, validate: [customValidator] }]
127+
}}
128+
onSubmit={jest.fn()}
129+
/>
130+
);
131+
});
132+
wrapper.update();
133+
134+
expect(wrapper.find('#warning').text()).toEqual(ERROR);
135+
});
136+
137+
it('should convert async function validator to warning', async () => {
138+
const ERROR = 'SOME-ERROR';
139+
140+
const customValidator = () => new Promise((res, rej) => setTimeout(() => rej({ type: 'warning', error: ERROR })));
141+
142+
await act(async () => {
143+
wrapper = mount(
144+
<FormRenderer
145+
FormTemplate={FormTemplate}
146+
componentMapper={{
147+
[componentTypes.TEXT_FIELD]: TextFieldWarning
148+
}}
149+
schema={{
150+
fields: [{ useWarnings: true, component: 'text-field', name: NAME, validate: [customValidator] }]
151+
}}
152+
onSubmit={jest.fn()}
153+
/>
154+
);
155+
});
156+
wrapper.update();
157+
158+
expect(wrapper.find('#warning').text()).toEqual(ERROR);
159+
});
160+
});
58161
});

0 commit comments

Comments
 (0)