|
1 | 1 | import createManagerApi, { initialMeta, flatObject } from '../../utils/manager-api';
|
| 2 | +import FORM_ERROR from '../../files/form-error'; |
2 | 3 |
|
3 | 4 | describe('managerApi', () => {
|
4 | 5 | it('should create managerApi getter', () => {
|
@@ -570,6 +571,55 @@ describe('managerApi', () => {
|
570 | 571 | });
|
571 | 572 | managerApi().handleSubmit({ preventDefault: jest.fn() });
|
572 | 573 | expect(onSubmit).toHaveBeenCalledWith(expectedValues);
|
| 574 | + |
| 575 | + expect(managerApi().submitting).toEqual(false); |
| 576 | + expect(managerApi().submitError).toEqual(undefined); |
| 577 | + expect(managerApi().submitFailed).toEqual(false); |
| 578 | + expect(managerApi().submitSucceeded).toEqual(true); |
| 579 | + }); |
| 580 | + |
| 581 | + it('onsubmit receives an error', () => { |
| 582 | + const error = 'some-error'; |
| 583 | + const onSubmit = jest.fn().mockImplementation(() => error); |
| 584 | + const managerApi = createManagerApi({ onSubmit }); |
| 585 | + const { registerField } = managerApi(); |
| 586 | + |
| 587 | + const render = jest.fn(); |
| 588 | + registerField({ name: 'field', render }); |
| 589 | + |
| 590 | + expect(render).not.toHaveBeenCalled(); |
| 591 | + |
| 592 | + managerApi().handleSubmit(); |
| 593 | + |
| 594 | + expect(render).toHaveBeenCalled(); |
| 595 | + expect(managerApi().submitting).toEqual(false); |
| 596 | + expect(managerApi().submitError).toEqual(undefined); |
| 597 | + expect(managerApi().submitFailed).toEqual(true); |
| 598 | + expect(managerApi().submitSucceeded).toEqual(false); |
| 599 | + expect(managerApi().submitErrors).toEqual(error); |
| 600 | + expect(managerApi().hasSubmitErrors).toEqual(true); |
| 601 | + }); |
| 602 | + |
| 603 | + it('onsubmit receives an error - form level', () => { |
| 604 | + const error = { [FORM_ERROR]: 'some-error' }; |
| 605 | + const onSubmit = jest.fn().mockImplementation(() => error); |
| 606 | + const managerApi = createManagerApi({ onSubmit }); |
| 607 | + const { registerField } = managerApi(); |
| 608 | + |
| 609 | + const render = jest.fn(); |
| 610 | + registerField({ name: 'field', render }); |
| 611 | + |
| 612 | + expect(render).not.toHaveBeenCalled(); |
| 613 | + |
| 614 | + managerApi().handleSubmit(); |
| 615 | + |
| 616 | + expect(render).toHaveBeenCalled(); |
| 617 | + expect(managerApi().submitting).toEqual(false); |
| 618 | + expect(managerApi().submitError).toEqual('some-error'); |
| 619 | + expect(managerApi().submitFailed).toEqual(true); |
| 620 | + expect(managerApi().submitSucceeded).toEqual(false); |
| 621 | + expect(managerApi().submitErrors).toEqual(error); |
| 622 | + expect(managerApi().hasSubmitErrors).toEqual(true); |
573 | 623 | });
|
574 | 624 |
|
575 | 625 | it('getField state should return correct field state', () => {
|
@@ -1726,4 +1776,104 @@ describe('managerApi', () => {
|
1726 | 1776 | expect(onSubmit).not.toHaveBeenCalled();
|
1727 | 1777 | });
|
1728 | 1778 | });
|
| 1779 | + |
| 1780 | + describe('async submit', () => { |
| 1781 | + it('calls async submit', async () => { |
| 1782 | + jest.useFakeTimers(); |
| 1783 | + |
| 1784 | + const onSubmit = jest.fn().mockImplementation(() => new Promise((res) => setTimeout(() => res('ok'), 1000))); |
| 1785 | + const render = jest.fn(); |
| 1786 | + |
| 1787 | + const managerApi = createManagerApi({ onSubmit }); |
| 1788 | + |
| 1789 | + managerApi().registerField({ name: 'field', internalId: '1', render }); |
| 1790 | + |
| 1791 | + expect(managerApi().submitting).toEqual(false); |
| 1792 | + expect(render).not.toHaveBeenCalled(); |
| 1793 | + |
| 1794 | + managerApi().submit(); |
| 1795 | + |
| 1796 | + expect(render).toHaveBeenCalled(); |
| 1797 | + render.mockClear(); |
| 1798 | + expect(managerApi().submitting).toEqual(true); |
| 1799 | + |
| 1800 | + await jest.runAllTimers(); |
| 1801 | + |
| 1802 | + expect(render).toHaveBeenCalled(); |
| 1803 | + expect(managerApi().submitting).toEqual(false); |
| 1804 | + expect(managerApi().submitError).toEqual(undefined); |
| 1805 | + expect(managerApi().submitFailed).toEqual(false); |
| 1806 | + expect(managerApi().submitSucceeded).toEqual(true); |
| 1807 | + expect(managerApi().submitErrors).toEqual(undefined); |
| 1808 | + expect(managerApi().hasSubmitErrors).toEqual(false); |
| 1809 | + }); |
| 1810 | + |
| 1811 | + it('calls async submit - failed', async () => { |
| 1812 | + jest.useFakeTimers(); |
| 1813 | + |
| 1814 | + const error = 'some evil error'; |
| 1815 | + |
| 1816 | + const onSubmit = jest.fn().mockImplementation(() => new Promise((res, rej) => setTimeout(() => rej(error), 100))); |
| 1817 | + const render = jest.fn(); |
| 1818 | + |
| 1819 | + const managerApi = createManagerApi({ onSubmit }); |
| 1820 | + |
| 1821 | + managerApi().registerField({ name: 'field', internalId: '1', render }); |
| 1822 | + |
| 1823 | + expect(managerApi().submitting).toEqual(false); |
| 1824 | + expect(render).not.toHaveBeenCalled(); |
| 1825 | + |
| 1826 | + managerApi().submit(); |
| 1827 | + |
| 1828 | + expect(render).toHaveBeenCalled(); |
| 1829 | + render.mockClear(); |
| 1830 | + |
| 1831 | + expect(managerApi().submitting).toEqual(true); |
| 1832 | + |
| 1833 | + await jest.runAllTimers(); |
| 1834 | + await jest.runAllTimers(); // for some reason, catch branch is not triggerd on first run |
| 1835 | + |
| 1836 | + expect(render).toHaveBeenCalled(); |
| 1837 | + expect(managerApi().submitError).toEqual(undefined); |
| 1838 | + expect(managerApi().submitting).toEqual(false); |
| 1839 | + expect(managerApi().submitFailed).toEqual(true); |
| 1840 | + expect(managerApi().submitSucceeded).toEqual(false); |
| 1841 | + expect(managerApi().submitErrors).toEqual(error); |
| 1842 | + expect(managerApi().hasSubmitErrors).toEqual(true); |
| 1843 | + }); |
| 1844 | + |
| 1845 | + it('calls async submit - failed with form level', async () => { |
| 1846 | + jest.useFakeTimers(); |
| 1847 | + |
| 1848 | + const error = { [FORM_ERROR]: 'some evil error' }; |
| 1849 | + |
| 1850 | + const onSubmit = jest.fn().mockImplementation(() => new Promise((res, rej) => setTimeout(() => rej(error), 100))); |
| 1851 | + const render = jest.fn(); |
| 1852 | + |
| 1853 | + const managerApi = createManagerApi({ onSubmit }); |
| 1854 | + |
| 1855 | + managerApi().registerField({ name: 'field', internalId: '1', render }); |
| 1856 | + |
| 1857 | + expect(managerApi().submitting).toEqual(false); |
| 1858 | + expect(render).not.toHaveBeenCalled(); |
| 1859 | + |
| 1860 | + managerApi().submit(); |
| 1861 | + |
| 1862 | + expect(render).toHaveBeenCalled(); |
| 1863 | + render.mockClear(); |
| 1864 | + |
| 1865 | + expect(managerApi().submitting).toEqual(true); |
| 1866 | + |
| 1867 | + await jest.runAllTimers(); |
| 1868 | + await jest.runAllTimers(); // for some reason, catch branch is not triggerd on first run |
| 1869 | + |
| 1870 | + expect(render).toHaveBeenCalled(); |
| 1871 | + expect(managerApi().submitError).toEqual('some evil error'); |
| 1872 | + expect(managerApi().submitting).toEqual(false); |
| 1873 | + expect(managerApi().submitFailed).toEqual(true); |
| 1874 | + expect(managerApi().submitSucceeded).toEqual(false); |
| 1875 | + expect(managerApi().submitErrors).toEqual(error); |
| 1876 | + expect(managerApi().hasSubmitErrors).toEqual(true); |
| 1877 | + }); |
| 1878 | + }); |
1729 | 1879 | });
|
0 commit comments