Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resetForm should reinitialize this.initialValues even if nextValues is not provided #444

Merged
merged 2 commits into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,17 +571,17 @@ export class Formik<ExtraProps = {}, Values = object> extends React.Component<
};

resetForm = (nextValues?: Values) => {
if (nextValues) {
this.initialValues = nextValues;
}
const values = nextValues ? nextValues : this.props.initialValues;

this.initialValues = values;

this.setState({
isSubmitting: false,
errors: {},
touched: {},
error: undefined,
status: undefined,
values: nextValues ? nextValues : this.props.initialValues,
values,
});
};

Expand Down
54 changes: 53 additions & 1 deletion test/formik.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';

import { Formik, FormikProps } from '../src/formik';
import { shallow } from 'enzyme';
import { shallow, mount } from 'enzyme';
import { sleep, noop } from './testHelpers';

interface Values {
Expand All @@ -16,6 +16,7 @@ const Form: React.SFC<FormikProps<Values>> = ({
handleBlur,
status,
errors,
dirty,
isSubmitting,
}) => {
return (
Expand All @@ -42,6 +43,25 @@ const BasicForm = (
<Formik initialValues={{ name: 'jared' }} onSubmit={noop} component={Form} />
);

class WithState extends React.Component<{}, { data: { name: string } }> {
constructor(props: {}) {
super(props);
this.state = {
data: { name: 'ivan' },
};
}

render() {
return (
<Formik
initialValues={this.state.data}
onSubmit={noop}
component={Form}
/>
);
}
}

describe('<Formik>', () => {
it('should initialize Formik state and pass down props', () => {
const tree = shallow(BasicForm);
Expand Down Expand Up @@ -963,5 +983,37 @@ describe('<Formik>', () => {

expect((tree.instance() as any).resetForm).toHaveBeenCalledWith('data');
});

it('should reset dirty flag even if initialValues has changed', async () => {
const tree = mount(<WithState />);
expect(tree.find(Form).props().dirty).toEqual(false);

tree
.find(Form)
.find('input')
.simulate('change', {
persist: noop,
target: {
id: 'name',
value: 'Ian',
},
});

expect(tree.find(Form).props().dirty).toEqual(true);

tree.setState({ data: { name: 'Jared' } });

await tree
.find(Form)
.props()
.handleReset();

expect(
tree
.update()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO should work without update call here, but it doesn't. Cant get why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.find(Form)
.props().dirty
).toEqual(false);
});
});
});