Merge remote-tracking branch 'origin/develop' into record-enhancements

This commit is contained in:
Alex Gleason 2022-03-09 15:58:35 -06:00
commit 5ddc8542fd
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 27 additions and 5 deletions

View file

@ -0,0 +1,12 @@
import { Record as ImmutableRecord } from 'immutable';
import reducer from '..';
describe('root reducer', () => {
it('should return the initial state', () => {
const result = reducer(undefined, {});
expect(ImmutableRecord.isRecord(result)).toBe(true);
expect(result.accounts.get('')).toBe(undefined);
expect(result.instance.get('version')).toEqual('0.0.0');
});
});

View file

@ -1,4 +1,4 @@
import { Map as ImmutableMap } from 'immutable';
import { Record as ImmutableRecord } from 'immutable';
import { combineReducers } from 'redux-immutable';
import { AUTH_LOGGED_OUT } from 'soapbox/actions/auth';
@ -58,7 +58,7 @@ import timelines from './timelines';
import trends from './trends';
import user_lists from './user_lists';
const appReducer = combineReducers({
const reducers = {
dropdown_menu,
timelines,
meta,
@ -113,13 +113,23 @@ const appReducer = combineReducers({
pending_statuses,
aliases,
accounts_meta,
});
};
// Build a default state from all reducers: it has the key and `undefined`
const StateRecord = ImmutableRecord(
Object.keys(reducers).reduce((params, reducer) => {
params[reducer] = undefined;
return params;
}, {}),
);
const appReducer = combineReducers(reducers, StateRecord);
// Clear the state (mostly) when the user logs out
const logOut = (state = ImmutableMap()) => {
const logOut = (state = StateRecord()) => {
const whitelist = ['instance', 'soapbox', 'custom_emojis', 'auth'];
return ImmutableMap(
return StateRecord(
whitelist.reduce((acc, curr) => {
acc[curr] = state.get(curr);
return acc;