Skip to content

Commit

Permalink
Merge pull request #74 from Kozea/fix-modified-state-on-submit
Browse files Browse the repository at this point in the history
Reset this.state.modified on form submit
  • Loading branch information
Aro committed May 24, 2021
2 parents 5e91bbf + 2ca3556 commit 08ea8ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Formol.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export default class Formol extends React.PureComponent {

if (!Object.keys(errors).length) {
// No errors on submit
this.setState({ modified: false })
if (item === emptyItem) {
// Resetting form if no item was given
this.handleCancel()
Expand Down
25 changes: 25 additions & 0 deletions test/formol/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { mount } from 'enzyme'
import React from 'react'
import { act } from 'react-dom/test-utils'

import Formol, { Field } from '../../src'
import InputField from '../../src/fields/InputField'
Expand Down Expand Up @@ -607,4 +608,28 @@ describe('Formol', () => {
false
)
}, 30000)

it('internal value of modified must be false on successful submit', async () => {
const extra = jest.fn()
const wrapper = mount(
<Formol
onSubmit={jest.fn()}
item={{ username: '' }}
extra={({ modified }) => extra(modified)}
>
<Field type="text" name="username">
Username
</Field>
</Formol>
)

wrapper.find('input').simulate('change', { target: { value: 'Toto' } })
expect(wrapper.getDOMNode().checkValidity()).toBeTruthy()
await act(async () => {
await wrapper.find('.Formol_Formol__submit').simulate('submit')
})
const { length } = extra.mock.calls
// last value of `modified` must be false
expect(extra.mock.calls[length - 1][0]).toBeFalsy()
}, 30000)
})

0 comments on commit 08ea8ea

Please sign in to comment.