[MM-24639] Set rehydrated values to true on store cleanup. (#4258)

* Set rehydration values to true on clean up

* Remove extra line

* Update app/store/middlewares/helpers.js

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Fix setting of views.root.hydrationComplete

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
This commit is contained in:
Miguel Alatzar 2020-05-05 15:16:49 -07:00 committed by GitHub
parent f5a126fd38
commit 86737a70c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 4 deletions

View file

@ -134,9 +134,13 @@ export function cleanUpState(payload, keepCurrent = false) {
nextEntities.posts.pendingPostIds = nextPendingPostIds;
}
nextState.views.root = {
// eslint-disable-next-line no-underscore-dangle
hydrationComplete: !nextState._persist,
nextState.views = {
...nextState.views,
root: {
...nextState.views?.root,
// eslint-disable-next-line no-underscore-dangle
hydrationComplete: nextState.views?.root?.hydrationComplete || !nextState._persist,
},
};
// eslint-disable-next-line no-underscore-dangle
@ -230,4 +234,4 @@ function removePendingPost(pendingPostIds, id) {
if (pendingIndex !== -1) {
pendingPostIds.splice(pendingIndex, 1);
}
}
}

View file

@ -7,8 +7,10 @@ import DeviceInfo from 'react-native-device-info';
import assert from 'assert';
import {REHYDRATE} from 'redux-persist';
import merge from 'deepmerge';
import {ViewTypes} from '@constants';
import initialState from '@store/initial_state';
import {
cleanUpPostsInChannel,
cleanUpState,
@ -348,6 +350,62 @@ describe('cleanUpState', () => {
expect(result.entities.posts.pendingPostIds).toEqual([]);
expect(result.entities.posts.postsInChannel.channel1).toEqual([{order: ['post1', 'post2'], recent: true}]);
});
test('should always set _persist.rehydrated to true', () => {
const persistValues = [
null,
{},
{rehydrated: false},
{rehydrated: true},
];
for (let i = 0; i < persistValues.length; i++) {
const _persist = persistValues[i]; // eslint-disable-line no-underscore-dangle
const state = merge(initialState, {
// eslint-disable-next-line no-underscore-dangle
_persist,
});
const result = cleanUpState(state);
expect(result._persist.rehydrated).toBe(true); // eslint-disable-line no-underscore-dangle
}
});
test('should set views.root.hydrationComplete to true when previous views.root.hydrationComplete is true', () => {
const state = merge(initialState, {
views: {
root: {
hydrationComplete: true,
},
},
});
const result = cleanUpState(state);
expect(result.views.root.hydrationComplete).toBe(true);
});
test('should set views.root.hydrationComplete to !_persist when previous views.root.hydrationComplete is falsy', () => {
const persistValues = [true, false];
const viewsValues = [
{},
{root: {}},
{root: {hydrationComplete: false}},
];
for (let i = 0; i < persistValues.length; i++) {
const _persist = persistValues[i]; // eslint-disable-line no-underscore-dangle
for (let j = 0; j < viewsValues.length; j++) {
const views = viewsValues[j];
const state = merge(initialState, {
_persist,
views,
});
const result = cleanUpState(state);
expect(result.views.root.hydrationComplete).toBe(!_persist); // eslint-disable-line no-underscore-dangle
}
}
});
});
describe('cleanUpPostsInChannel', () => {