|
1 | 1 | import React from 'react';
|
| 2 | +import { act } from 'react-dom/test-utils'; |
2 | 3 | import { mount } from 'enzyme';
|
3 | 4 | import toJson from 'enzyme-to-json';
|
4 | 5 | import arrayMutators from 'final-form-arrays';
|
@@ -1752,5 +1753,99 @@ describe('renderForm function', () => {
|
1752 | 1753 | wrapper.find('form').simulate('submit');
|
1753 | 1754 | expect(onSubmit).toHaveBeenCalledWith({ testField: 'higher-priority' });
|
1754 | 1755 | });
|
| 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 | + }); |
1755 | 1850 | });
|
1756 | 1851 | });
|
0 commit comments