|
1 | 1 | import React from 'react' |
2 | | -import { render, fireEvent, cleanup, act } from 'react-testing-library' |
| 2 | +import { render, fireEvent, cleanup } from 'react-testing-library' |
3 | 3 | import 'jest-dom/extend-expect' |
4 | 4 | import deepEqual from 'fast-deep-equal' |
5 | 5 | import { ErrorBoundary, Toggle, wrapWith } from './testUtils' |
@@ -857,4 +857,54 @@ describe('ReactFinalForm', () => { |
857 | 857 | expect(onSubmitMock).toHaveBeenCalledTimes(1) |
858 | 858 | expect(onSubmitMock.mock.calls[0][0]).toEqual({ name: 'erikras' }) |
859 | 859 | }) |
| 860 | + |
| 861 | + it('should set submitting back to false after submit', async () => { |
| 862 | + /** |
| 863 | + * This test causes a warning: |
| 864 | + * |
| 865 | + * Warning: An update to ReactFinalForm inside a test was not wrapped in act(...). |
| 866 | + * |
| 867 | + * ...but it's working as expected: |
| 868 | + * 1) We click "Submit" |
| 869 | + * 2) Submission is async and takes 1ms |
| 870 | + * 3) The form's state has to go to `submitting = true` and then back to `submitting = false` |
| 871 | + * |
| 872 | + * That state change when the submission completes is not an "act" from the |
| 873 | + * user, but an internal state change. If you have an idea of how to fix this, |
| 874 | + * please submit a PR. Thanks. -@erikras |
| 875 | + */ |
| 876 | + const onSubmit = jest.fn(async () => { |
| 877 | + sleep(1) |
| 878 | + }) |
| 879 | + const recordSubmitting = jest.fn() |
| 880 | + const { getByText } = render( |
| 881 | + <Form onSubmit={onSubmit} subscription={{ submitting: true }}> |
| 882 | + {({ handleSubmit, submitting }) => { |
| 883 | + recordSubmitting(submitting) |
| 884 | + return ( |
| 885 | + <form onSubmit={handleSubmit}> |
| 886 | + <Field name="name" component="input" /> |
| 887 | + <button type="submit">Submit</button> |
| 888 | + </form> |
| 889 | + ) |
| 890 | + }} |
| 891 | + </Form> |
| 892 | + ) |
| 893 | + expect(onSubmit).not.toHaveBeenCalled() |
| 894 | + expect(recordSubmitting).toHaveBeenCalled() |
| 895 | + expect(recordSubmitting).toHaveBeenCalledTimes(1) |
| 896 | + expect(recordSubmitting.mock.calls[0][0]).toBe(false) |
| 897 | + |
| 898 | + fireEvent.click(getByText('Submit')) |
| 899 | + |
| 900 | + expect(onSubmit).toHaveBeenCalled() |
| 901 | + expect(onSubmit).toHaveBeenCalledTimes(1) |
| 902 | + expect(recordSubmitting).toHaveBeenCalledTimes(2) |
| 903 | + expect(recordSubmitting.mock.calls[1][0]).toBe(true) |
| 904 | + |
| 905 | + await sleep(5) |
| 906 | + |
| 907 | + expect(recordSubmitting).toHaveBeenCalledTimes(3) |
| 908 | + expect(recordSubmitting.mock.calls[2][0]).toBe(false) |
| 909 | + }) |
860 | 910 | }) |
0 commit comments