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

update-reducers fix #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions spec/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { syncStateUpdate, rehydrateApplicationState, dateReviver, localStorageSy
import * as CryptoJS from 'crypto-js';
import 'localstorage-polyfill';
const INIT_ACTION = '@ngrx/store/init';
const UPDATE_ACTION = '@ngrx/store/update-reducers';

// Very simple classes to test serialization options. They cover string, number, date, and nested classes
// The top level class has static functions to help test reviver, replacer, serialize and deserialize
Expand Down Expand Up @@ -486,4 +487,25 @@ describe('ngrxLocalStorage', () => {
feature2: { slice21: true, slice22: [1, 2], slice23: {} },
});
});

it('should not merge with rehydrated state after reducers are updated', () => {
const initialToken = 'initial token';
const newToken = 'new token';

localStorage.setItem('token', initialToken);

const reducer = (state = {}, action) => state;
const metaReducer = localStorageSync({keys: ['token'], rehydrate: true});
const action = {type: INIT_ACTION};

const tempState = metaReducer(reducer)({}, action);

expect(localStorage.getItem('token')).toEqual(initialToken);
expect(tempState.token).toEqual(initialToken);

const updateAction = {type: UPDATE_ACTION};
const finalState = metaReducer(reducer)({ token: newToken }, updateAction);
expect(localStorage.getItem('token')).toEqual(newToken);
expect(finalState.token).toEqual(newToken);
});
});
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as merge from 'lodash.merge';

const INIT_ACTION = '@ngrx/store/init';
const UPDATE_ACTION = '@ngrx/store/update-reducers';
const detectDate = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/;

// correctly parse dates from local storage
Expand Down Expand Up @@ -250,7 +249,7 @@ export const localStorageSync = (config: LocalStorageConfig) => (
nextState = { ...state };
}

if ((action.type === INIT_ACTION || action.type === UPDATE_ACTION) && rehydratedState) {
if (action.type === INIT_ACTION && rehydratedState) {
nextState = merge({}, nextState, rehydratedState);
}

Expand Down