MM-28968 Fix redux-persist serializer (#4918)

This commit is contained in:
Elias Nahum 2020-10-23 19:12:06 -03:00 committed by GitHub
parent 016a725732
commit 79d2d7b31d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 3 deletions

View file

@ -18,7 +18,7 @@ import appReducer from 'app/reducers';
import {createReducer, getStoredState} from './helpers';
import {createMiddlewares} from './middlewares';
import Store from './store';
import {transformSet} from './utils';
import {transformSet, serialize} from './utils';
/**
* Configures and constructs the redux store. Accepts the following parameters:
@ -167,7 +167,7 @@ export default function configureStore(storage: any, preloadedState: any = {}, o
const defaultConfig: PersistConfig<GlobalState> = {
key: 'root',
storage,
serialize: (state: GlobalState) => ({...state}),
serialize,
deserialize: false,
blacklist: ['device', 'navigation', 'requests', '_persist'],
transforms: [

View file

@ -30,6 +30,18 @@ function transformToSet(incoming) {
return state;
}
export function serialize(state) {
if (!state) {
return state;
}
if (Array.isArray(state)) {
return [...state];
}
return {...state};
}
export function transformSet(incoming, setTransforms, toStorage = true) {
const state = {...incoming};

View file

@ -4,7 +4,7 @@
import DeviceInfo from 'react-native-device-info';
import initialState from '@store/initial_state';
import {getStateForReset} from '@store/utils';
import {getStateForReset, serialize} from '@store/utils';
/*
const {currentUserId} = currentState.entities.users;
@ -106,3 +106,32 @@ describe('getStateForReset', () => {
expect(app.previousVersion).toStrictEqual(currentState.app.version);
});
});
describe('Store serialzer', () => {
it('should set the value to be undefined', () => {
const value = serialize();
expect(value).toBeUndefined();
});
it('should set the value to be null', () => {
const value = serialize(null);
expect(value).toBeNull();
});
it('should set the value to be a new array with the same values', () => {
const initial = [1, 2, 3];
const value = serialize(initial);
expect(initial === value).toEqual(false);
expect(value).toEqual(initial);
});
it('should set the value to be a new object with the same values', () => {
const initial = {
key: '123',
value: 'some value',
};
const value = serialize(initial);
expect(initial === value).toEqual(false);
expect(value).toEqual(initial);
});
});