|
| 1 | +/* eslint-disable no-template-curly-in-string */ |
| 2 | +import React from 'react'; |
| 3 | +import { mount } from 'enzyme'; |
| 4 | +import Form from '../src'; |
| 5 | +import InfoField from './common/InfoField'; |
| 6 | +import timeout from './common/timeout'; |
| 7 | + |
| 8 | +describe('Form.Preserve', () => { |
| 9 | + const Demo = ({ |
| 10 | + removeField, |
| 11 | + formPreserve, |
| 12 | + fieldPreserve, |
| 13 | + onFinish, |
| 14 | + }: { |
| 15 | + removeField: boolean; |
| 16 | + formPreserve?: boolean; |
| 17 | + fieldPreserve?: boolean; |
| 18 | + onFinish: (values: object) => void; |
| 19 | + }) => ( |
| 20 | + <Form onFinish={onFinish} initialValues={{ keep: 233, remove: 666 }} preserve={formPreserve}> |
| 21 | + <InfoField name="keep" /> |
| 22 | + {!removeField && <InfoField name="remove" preserve={fieldPreserve} />} |
| 23 | + </Form> |
| 24 | + ); |
| 25 | + |
| 26 | + it('field', async () => { |
| 27 | + const onFinish = jest.fn(); |
| 28 | + const wrapper = mount(<Demo removeField={false} onFinish={onFinish} fieldPreserve={false} />); |
| 29 | + |
| 30 | + async function matchTest(removeField: boolean, match: object) { |
| 31 | + onFinish.mockReset(); |
| 32 | + wrapper.setProps({ removeField }); |
| 33 | + wrapper.find('form').simulate('submit'); |
| 34 | + await timeout(); |
| 35 | + expect(onFinish).toHaveBeenCalledWith(match); |
| 36 | + } |
| 37 | + |
| 38 | + await matchTest(false, { keep: 233, remove: 666 }); |
| 39 | + await matchTest(true, { keep: 233 }); |
| 40 | + await matchTest(false, { keep: 233 }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('form', async () => { |
| 44 | + const onFinish = jest.fn(); |
| 45 | + const wrapper = mount(<Demo removeField={false} onFinish={onFinish} formPreserve={false} />); |
| 46 | + |
| 47 | + async function matchTest(removeField: boolean, match: object) { |
| 48 | + onFinish.mockReset(); |
| 49 | + wrapper.setProps({ removeField }); |
| 50 | + wrapper.find('form').simulate('submit'); |
| 51 | + await timeout(); |
| 52 | + expect(onFinish).toHaveBeenCalledWith(match); |
| 53 | + } |
| 54 | + |
| 55 | + await matchTest(false, { keep: 233, remove: 666 }); |
| 56 | + await matchTest(true, { keep: 233 }); |
| 57 | + await matchTest(false, { keep: 233 }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('form perishable but field !perishable', async () => { |
| 61 | + const onFinish = jest.fn(); |
| 62 | + const wrapper = mount( |
| 63 | + <Demo removeField={false} onFinish={onFinish} formPreserve={false} fieldPreserve />, |
| 64 | + ); |
| 65 | + |
| 66 | + async function matchTest(removeField: boolean, match: object) { |
| 67 | + onFinish.mockReset(); |
| 68 | + wrapper.setProps({ removeField }); |
| 69 | + wrapper.find('form').simulate('submit'); |
| 70 | + await timeout(); |
| 71 | + expect(onFinish).toHaveBeenCalledWith(match); |
| 72 | + } |
| 73 | + |
| 74 | + await matchTest(true, { keep: 233 }); |
| 75 | + await matchTest(false, { keep: 233, remove: 666 }); |
| 76 | + }); |
| 77 | +}); |
| 78 | +/* eslint-enable no-template-curly-in-string */ |
0 commit comments