diff --git a/app/soapbox/reducers/__tests__/index-test.js b/app/soapbox/reducers/__tests__/index-test.js new file mode 100644 index 000000000..2b4a21266 --- /dev/null +++ b/app/soapbox/reducers/__tests__/index-test.js @@ -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'); + }); +}); diff --git a/app/soapbox/reducers/index.js b/app/soapbox/reducers/index.js index c7b48cc22..989a1fa1f 100644 --- a/app/soapbox/reducers/index.js +++ b/app/soapbox/reducers/index.js @@ -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;