Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,24 @@ jobs:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run: npm test -- --coverage && bash <(curl -s https://codecov.io/bash)
compile:
docker:
- image: circleci/node:latest
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- run: npm install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run: npm run compile
workflows:
version: 2
build_and_test:
jobs:
- lint
- test
- test
- compile
2 changes: 1 addition & 1 deletion src/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface FormProps extends BaseFormProps {
onFinishFailed?: Callbacks['onFinishFailed'];
}

const Form: React.FunctionComponent<FormProps> = (
const Form: React.ForwardRefRenderFunction<FormInstance, FormProps> = (
{
name,
initialValues,
Expand Down
2 changes: 1 addition & 1 deletion src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ export class FormStore {
errors: string[];
}>[] = [];

this.getFieldEntities().forEach((field: FieldEntity) => {
this.getFieldEntities(true).forEach((field: FieldEntity) => {
// Add field if not provide `nameList`
if (!provideNameList) {
namePathList.push(field.getNamePath());
Expand Down
87 changes: 66 additions & 21 deletions tests/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,35 +272,80 @@ describe('Form.Validate', () => {
expect(form.getFieldError('title')).toEqual(['Title should be 3+ characters']);
});

it('validate only accept exist fields', async () => {
let form;
const onFinish = jest.fn();
describe('validate only accept exist fields', () => {
it('skip init value', async () => {
let form;
const onFinish = jest.fn();

const wrapper = mount(
<div>
const wrapper = mount(
<div>
<Form
onFinish={onFinish}
ref={instance => {
form = instance;
}}
initialValues={{ user: 'light', pass: 'bamboo' }}
>
<InfoField name="user">
<Input />
</InfoField>
<button type="submit">submit</button>
</Form>
</div>,
);

// Validate callback
expect(await form.validateFields(['user'])).toEqual({ user: 'light' });
expect(await form.validateFields()).toEqual({ user: 'light' });

// Submit callback
wrapper.find('button').simulate('submit');
await timeout();
expect(onFinish).toHaveBeenCalledWith({ user: 'light' });
});

it('remove from fields', async () => {
const onFinish = jest.fn();
const wrapper = mount(
<Form
onFinish={onFinish}
ref={instance => {
form = instance;
initialValues={{
switch: true,
ignore: 'test',
}}
initialValues={{ user: 'light', pass: 'bamboo' }}
>
<InfoField name="user">
<Input />
<InfoField name="switch" valuePropName="checked">
<Input type="checkbox" className="switch" />
</InfoField>
<Field shouldUpdate>
{(_, __, { getFieldValue }) =>
getFieldValue('switch') && (
<InfoField name="ignore">
<Input className="ignore" />
</InfoField>
)
}
</Field>
<button type="submit">submit</button>
</Form>
</div>,
);

// Validate callback
expect(await form.validateFields(['user'])).toEqual({ user: 'light' });
expect(await form.validateFields()).toEqual({ user: 'light' });
</Form>,
);

// Submit callback
wrapper.find('button').simulate('submit');
await timeout();
expect(onFinish).toHaveBeenCalledWith({ user: 'light' });
// Submit callback
wrapper.find('button').simulate('submit');
await timeout();
expect(onFinish).toHaveBeenCalledWith({ switch: true, ignore: 'test' });
onFinish.mockReset();

// Hide one
wrapper.find('input.switch').simulate('change', {
target: {
checked: false,
},
});
wrapper.find('button').simulate('submit');
await timeout();
expect(onFinish).toHaveBeenCalledWith({ switch: false });
});
});

it('should error in console if user script failed', async () => {
Expand Down