Skip to content

Commit 7ee45b2

Browse files
committed
Add more tests for initialValue
1 parent 4f073d4 commit 7ee45b2

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { act } from 'react-dom/test-utils';
23
import { mount } from 'enzyme';
34
import toJson from 'enzyme-to-json';
45
import arrayMutators from 'final-form-arrays';
@@ -1752,5 +1753,99 @@ describe('renderForm function', () => {
17521753
wrapper.find('form').simulate('submit');
17531754
expect(onSubmit).toHaveBeenCalledWith({ testField: 'higher-priority' });
17541755
});
1756+
1757+
it('empty initialValues ', () => {
1758+
const onSubmit = jest.fn();
1759+
const wrapper = mount(
1760+
<FormRenderer
1761+
FormTemplate={(props) => <FormTemplate {...props} />}
1762+
componentMapper={{
1763+
'custom-component': TextField,
1764+
}}
1765+
schema={{
1766+
fields: [
1767+
{
1768+
component: 'custom-component',
1769+
name: 'testField',
1770+
initialValue: 'lower-priority',
1771+
},
1772+
],
1773+
}}
1774+
onSubmit={(values) => onSubmit(values)}
1775+
initialValues={{ testField: undefined }}
1776+
/>
1777+
);
1778+
1779+
expect(wrapper.find('input').instance().value).toEqual('lower-priority');
1780+
1781+
wrapper.find('form').simulate('submit');
1782+
expect(onSubmit).toHaveBeenCalledWith({ testField: 'lower-priority' });
1783+
});
1784+
1785+
it('null initialValues ', () => {
1786+
const onSubmit = jest.fn();
1787+
const wrapper = mount(
1788+
<FormRenderer
1789+
FormTemplate={(props) => <FormTemplate {...props} />}
1790+
componentMapper={{
1791+
'custom-component': TextField,
1792+
}}
1793+
schema={{
1794+
fields: [
1795+
{
1796+
component: 'custom-component',
1797+
name: 'testField',
1798+
initialValue: 'lower-priority',
1799+
},
1800+
],
1801+
}}
1802+
onSubmit={(values) => onSubmit(values)}
1803+
initialValues={{ testField: null }}
1804+
/>
1805+
);
1806+
1807+
expect(wrapper.find('input').instance().value).toEqual('');
1808+
1809+
wrapper.find('form').simulate('submit');
1810+
expect(onSubmit).toHaveBeenCalledWith({ testField: null });
1811+
});
1812+
1813+
it('use initialValue when initialOnMount', async () => {
1814+
const onSubmit = jest.fn();
1815+
let wrapper;
1816+
1817+
await act(async () => {
1818+
wrapper = mount(
1819+
<FormRenderer
1820+
FormTemplate={(props) => <FormTemplate {...props} />}
1821+
componentMapper={{
1822+
'custom-component': TextField,
1823+
}}
1824+
schema={{
1825+
fields: [
1826+
{
1827+
component: 'custom-component',
1828+
name: 'testField',
1829+
initialValue: 'lower-priority',
1830+
initializeOnMount: true,
1831+
},
1832+
],
1833+
}}
1834+
onSubmit={(values) => onSubmit(values)}
1835+
initialValues={{ testField: 'higher-priority' }}
1836+
/>
1837+
);
1838+
});
1839+
wrapper.update();
1840+
1841+
expect(wrapper.find('input').instance().value).toEqual('lower-priority');
1842+
1843+
await act(async () => {
1844+
wrapper.find('form').simulate('submit');
1845+
});
1846+
wrapper.update();
1847+
1848+
expect(onSubmit).toHaveBeenCalledWith({ testField: 'lower-priority' });
1849+
});
17551850
});
17561851
});

0 commit comments

Comments
 (0)