Flatten misc reducer

This commit is contained in:
Josh Johnson 2019-12-23 17:13:59 +00:00
parent 5694a2a651
commit ac4a1b54b9
5 changed files with 13 additions and 21 deletions

View file

@ -4,6 +4,7 @@ import rootReducer from '.';
import groups from './groups'; import groups from './groups';
import choices from './choices'; import choices from './choices';
import items from './items'; import items from './items';
import loading from './loading';
describe('reducers/rootReducer', () => { describe('reducers/rootReducer', () => {
const store = createStore(rootReducer); const store = createStore(rootReducer);
@ -14,6 +15,7 @@ describe('reducers/rootReducer', () => {
expect(state.groups).to.equal(groups(undefined, {} as any)); expect(state.groups).to.equal(groups(undefined, {} as any));
expect(state.choices).to.equal(choices(undefined, {} as any)); expect(state.choices).to.equal(choices(undefined, {} as any));
expect(state.items).to.equal(items(undefined, {} as any)); expect(state.items).to.equal(items(undefined, {} as any));
expect(state.loading).to.equal(loading(undefined, {} as any));
}); });
describe('CLEAR_ALL', () => { describe('CLEAR_ALL', () => {
@ -33,9 +35,7 @@ describe('reducers/rootReducer', () => {
items: [], items: [],
groups: [], groups: [],
choices: [], choices: [],
general: { loading: false,
loading: false,
},
}); });
}); });
}); });

View file

@ -2,23 +2,21 @@ import { combineReducers } from 'redux';
import items from './items'; import items from './items';
import groups from './groups'; import groups from './groups';
import choices from './choices'; import choices from './choices';
import general from './general'; import loading from './loading';
import { cloneObject } from '../lib/utils'; import { cloneObject } from '../lib/utils';
export const defaultState = { export const defaultState = {
groups: [], groups: [],
items: [], items: [],
choices: [], choices: [],
general: { loading: false,
loading: false,
},
}; };
const appReducer = combineReducers({ const appReducer = combineReducers({
items, items,
groups, groups,
choices, choices,
general, loading,
}); });
const rootReducer = (passedState, action): object => { const rootReducer = (passedState, action): object => {

View file

@ -1,16 +1,14 @@
import { expect } from 'chai'; import { expect } from 'chai';
import general, { defaultState } from './general'; import general, { defaultState } from './loading';
describe('reducers/general', () => { describe('reducers/loading', () => {
it('should return same state when no action matches', () => { it('should return same state when no action matches', () => {
expect(general(defaultState, {} as any)).to.equal(defaultState); expect(general(defaultState, {} as any)).to.equal(defaultState);
}); });
describe('SET_IS_LOADING', () => { describe('SET_IS_LOADING', () => {
it('sets loading state', () => { it('sets loading state', () => {
const expectedState = { const expectedState = true;
loading: true,
};
const actualState = general(undefined, { const actualState = general(undefined, {
type: 'SET_IS_LOADING', type: 'SET_IS_LOADING',

View file

@ -1,21 +1,17 @@
import { SetIsLoadingAction } from '../actions/misc'; import { SetIsLoadingAction } from '../actions/misc';
import { State } from '../interfaces'; import { State } from '../interfaces';
export const defaultState = { export const defaultState = false;
loading: false,
};
type ActionTypes = SetIsLoadingAction; type ActionTypes = SetIsLoadingAction;
const general = ( const general = (
state = defaultState, state = defaultState,
action: ActionTypes, action: ActionTypes,
): State['general'] => { ): State['loading'] => {
switch (action.type) { switch (action.type) {
case 'SET_IS_LOADING': { case 'SET_IS_LOADING': {
return { return action.isLoading;
loading: action.isLoading,
};
} }
default: { default: {

View file

@ -120,7 +120,7 @@ export default class Store {
* Get loading state from store * Get loading state from store
*/ */
isLoading(): boolean { isLoading(): boolean {
return this.state.general.loading; return this.state.loading;
} }
/** /**