Skip to content

Commit

Permalink
Merge pull request #58 from LaunchPadLab/sync-submit
Browse files Browse the repository at this point in the history
Wrap onSubmit in a promise
  • Loading branch information
dpikt authored Aug 12, 2019
2 parents 32809e5 + 54cf819 commit 328f95d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ A wrapper around the `reduxForm` HOC exported from
[redux-form](https://www.npmjs.com/package/redux-form) that gives it some extra functionality:

1. Makes extra options available for configuring the form
2. Wraps every rejected `onSubmit` in a `SubmissionError`. If the thrown error has an `errors` property, its value will be passed to `SubmissionError`. The original error will be accessible via the `SubmissionError`s `meta.error` property. This enables developers to access useful information regarding the origin of the failure, e.g., HTTP status.
2. Wraps every `onSubmit` result in a promise. Additionally, wraps rejected `onSubmit` results in a `SubmissionError`. If the thrown error has an `errors` property, its value will be passed to `SubmissionError`. The original error will be accessible via the `SubmissionError`s `meta.error` property. This enables developers to access useful information regarding the origin of the failure, e.g., HTTP status.
3. Provides a default `onSubmit` function that resolves successfully and logs a warning.
4. Ignores any `onChange` events that occur on a pristine and untouched form, patching a bug in `redux-form v8`.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@launchpadlab/lp-form",
"version": "2.8.3",
"version": "2.9.0",
"description": "Extensions for the reduxForm HOC",
"main": "lib/index.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions src/lpForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
enableSubmitOnChange,
mergeOptionsWithProps,
renameFormProp,
wrapSubmissionErrors,
wrapSubmissionPromise,
omitCustomProps,
ignorePristineOnChange,
} from './middleware'
Expand All @@ -24,7 +24,7 @@ import {
* {@link https://www.npmjs.com/package/redux-form|redux-form} that gives it some extra functionality:
*
* 1. Makes extra options available for configuring the form
* 2. Wraps every rejected `onSubmit` in a `SubmissionError`. If the thrown error has an `errors` property, its value will be passed to `SubmissionError`. The original error will be accessible via the `SubmissionError`s `meta.error` property. This enables developers to access useful information regarding the origin of the failure, e.g., HTTP status.
* 2. Wraps every `onSubmit` result in a promise. Additionally, wraps rejected `onSubmit` results in a `SubmissionError`. If the thrown error has an `errors` property, its value will be passed to `SubmissionError`. The original error will be accessible via the `SubmissionError`s `meta.error` property. This enables developers to access useful information regarding the origin of the failure, e.g., HTTP status.
* 3. Provides a default `onSubmit` function that resolves successfully and logs a warning.
* 4. Ignores any `onChange` events that occur on a pristine and untouched form, patching a bug in `redux-form v8`.
*
Expand Down Expand Up @@ -78,7 +78,7 @@ function lpForm (options={}) {
debounceOnSubmit,
filterSubmitValues,
addBeforeSubmitHook,
wrapSubmissionErrors,
wrapSubmissionPromise,
enableSubmitOnChange,
ignorePristineOnChange,
addDefaultValidate,
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export filterSubmitValues from './filterSubmitValues'
export renameFormProp from './renameFormProp'
export mergeOptionsWithProps from './mergeOptionsWithProps'
export omitCustomProps from './omitCustomProps'
export wrapSubmissionErrors from './wrapSubmissionErrors'
export wrapSubmissionPromise from './wrapSubmissionPromise'
export ignorePristineOnChange from './ignorePristineOnChange'
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { SubmissionError } from 'redux-form'
import isPromise from 'is-promise'
import { getOr } from 'lodash/fp'

// Wrap submission results in a redux-form SubmissionError
const wrapSubmissionErrors = withPropsOnChange(
// Wrap submission results in a redux-form SubmissionError.
// Also ensures that the return value of onSubmit is a promise.
const wrapSubmissionPromise = withPropsOnChange(
['onSubmit'],
({ onSubmit }) => {
return {
onSubmit: (...args) => {
const result = onSubmit(...args)
if (!isPromise(result)) return result
if (!isPromise(result)) return Promise.resolve(result)
return result.catch(err => {
const messages = getOr({}, 'errors', err)
const submissionError = new SubmissionError(messages)
Expand All @@ -24,4 +25,4 @@ const wrapSubmissionErrors = withPropsOnChange(
}
)

export default wrapSubmissionErrors
export default wrapSubmissionPromise
15 changes: 15 additions & 0 deletions test/lpForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { configure, mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import debounce from 'lodash/debounce'
import { SubmissionError } from 'redux-form'
import isPromise from 'is-promise'
import { lpForm } from '../src'

jest.mock('lodash/debounce', () => jest.fn(fn => fn))
Expand Down Expand Up @@ -52,6 +53,20 @@ test('lpForm: filters submitted values', () => {
expect(onSubmit).toHaveBeenCalledWith({ address: { street: 'Shady Lane' }})
})

test('lpForm: wraps synchronous onSubmits in a promise', () => {
const syncResult = 'a synchronous result'
const onSubmit = () => syncResult
const Wrapped = () => <div> Hi </div>
const Form = lpForm({ onSubmit })(Wrapped)
const wrapper = mountWithProvider(<Form />)
const formConfig = wrapper.find(Wrapped).props()
const result = formConfig.onSubmit(INITIAL_VALUES)
expect(isPromise(result)).toBe(true)
return result.then(resolvedResult => {
expect(resolvedResult).toEqual(syncResult)
})
})

test('lpForm: wraps rejected promises in a SubmissionError', () => {
expect.assertions(2)
const ERRORS = [ 'my', 'errors' ]
Expand Down

0 comments on commit 328f95d

Please sign in to comment.