-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5146 from marmelab/fix-form-initialization-from-r…
…ecord Fix Form Initialization from Record
- Loading branch information
Showing
2 changed files
with
86 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as React from 'react'; | ||
import { cleanup } from '@testing-library/react'; | ||
|
||
import { renderWithRedux } from '../util'; | ||
import FormWithRedirect from './FormWithRedirect'; | ||
import useInput from './useInput'; | ||
|
||
describe('FormWithRedirect', () => { | ||
afterEach(cleanup); | ||
const Input = props => { | ||
const { input } = useInput(props); | ||
|
||
return <input type="text" {...input} />; | ||
}; | ||
|
||
it('Does not make the form dirty when reinitialized from a record', () => { | ||
const renderProp = jest.fn(() => ( | ||
<Input source="name" defaultValue="Bar" /> | ||
)); | ||
const { getByDisplayValue, rerender } = renderWithRedux( | ||
<FormWithRedirect | ||
save={jest.fn()} | ||
redirect={false} | ||
saving={false} | ||
version={0} | ||
render={renderProp} | ||
/> | ||
); | ||
|
||
expect(renderProp.mock.calls[0][0].pristine).toEqual(true); | ||
expect(getByDisplayValue('Bar')).not.toBeNull(); | ||
|
||
rerender( | ||
<FormWithRedirect | ||
save={jest.fn()} | ||
redirect={false} | ||
saving={false} | ||
version={0} | ||
render={renderProp} | ||
record={{ id: 1, name: 'Foo' }} | ||
/> | ||
); | ||
|
||
expect(renderProp.mock.calls[1][0].pristine).toEqual(true); | ||
expect(getByDisplayValue('Foo')).not.toBeNull(); | ||
expect(renderProp).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('Does not make the form dirty when reinitialized from a different record', () => { | ||
const renderProp = jest.fn(() => ( | ||
<Input source="name" defaultValue="Bar" /> | ||
)); | ||
const { getByDisplayValue, rerender } = renderWithRedux( | ||
<FormWithRedirect | ||
save={jest.fn()} | ||
redirect={false} | ||
saving={false} | ||
version={0} | ||
record={{ id: 1, name: 'Foo' }} | ||
render={renderProp} | ||
/> | ||
); | ||
|
||
expect(renderProp.mock.calls[0][0].pristine).toEqual(true); | ||
expect(getByDisplayValue('Foo')).not.toBeNull(); | ||
|
||
rerender( | ||
<FormWithRedirect | ||
save={jest.fn()} | ||
redirect={false} | ||
saving={false} | ||
version={0} | ||
record={{ id: 1, name: 'Foo', anotherServerAddedProp: 'Bar' }} | ||
render={renderProp} | ||
/> | ||
); | ||
|
||
expect(renderProp.mock.calls[1][0].pristine).toEqual(true); | ||
expect(getByDisplayValue('Foo')).not.toBeNull(); | ||
expect(renderProp).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters