Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Sep 19, 2018
1 parent a40cc9e commit db7b3c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
41 changes: 21 additions & 20 deletions packages/ra-core/src/form/formMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import isEqual from 'lodash/isEqual';
import { resetForm } from '../actions/formActions';
import { REDUX_FORM_NAME } from '../form/constants';

let previousLocation;

/**
* This middleware ensure that whenever a location change happen, we get the
* chance to properly reset the redux-form record form, preventing data to be
Expand All @@ -17,26 +15,29 @@ let previousLocation;
* change. Otherwise, the new page which may contains a record redux-form might
* initialize before our reset and loose its data.
*/
const formMiddleware = () => next => action => {
if (
action.type !== LOCATION_CHANGE ||
(action.payload.state && action.payload.state.skipFormReset)
) {
return next(action);
}
const formMiddleware = () => {
let previousLocation;
return next => action => {
if (
action.type !== LOCATION_CHANGE ||
(action.payload.state && action.payload.state.skipFormReset)
) {
return next(action);
}

// history allows one to redirect to the same location which can happen
// when using a special menu for a create page for instance. In this case,
// we don't want to reset the form.
// See https://github.com/marmelab/react-admin/issues/2291
if (isEqual(action.payload, previousLocation)) {
return next(action);
}
// history allows one to redirect to the same location which can happen
// when using a special menu for a create page for instance. In this case,
// we don't want to reset the form.
// See https://github.com/marmelab/react-admin/issues/2291
if (isEqual(action.payload, previousLocation)) {
return next(action);
}

previousLocation = action.payload;
next(resetForm());
next(destroy(REDUX_FORM_NAME));
return next(action);
previousLocation = action.payload;
next(resetForm());
next(destroy(REDUX_FORM_NAME));
return next(action);
};
};

export default formMiddleware;
9 changes: 5 additions & 4 deletions packages/ra-core/src/form/formMiddleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('form middleware', () => {
expect(next).toHaveBeenCalledWith(resetForm());
expect(next).toHaveBeenCalledWith(destroy(REDUX_FORM_NAME));
expect(next).toHaveBeenCalledWith(action);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledTimes(3);
});

it('does not resets the record and form if LOCATION_CHANGE targets the same location', () => {
Expand All @@ -54,8 +54,9 @@ describe('form middleware', () => {
const middleware = formMiddleware()(next);
middleware(action);
middleware(action);
expect(next).toHaveBeenNthCalledWith(1, resetForm());
expect(next).toHaveBeenNthCalledWith(1, destroy(REDUX_FORM_NAME));
expect(next).toHaveBeenNthCalledWith(2, action);
expect(next).toHaveBeenCalledWith(resetForm());
expect(next).toHaveBeenCalledWith(destroy(REDUX_FORM_NAME));
expect(next).toHaveBeenCalledWith([action, action]);
expect(next).toHaveBeenCalledTimes(4);
});
});

0 comments on commit db7b3c3

Please sign in to comment.