Skip to content

Commit 10b8ee4

Browse files
authored
Fix for initializing Stores with combined reducers (#225)
1 parent d2784e5 commit 10b8ee4

File tree

3 files changed

+111
-29
lines changed

3 files changed

+111
-29
lines changed

packages/frint-store/src/Store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BehaviorSubject } from 'rxjs';
44

55
function Store(options = {}) {
66
this.options = {
7-
initialState: null,
7+
initialState: undefined,
88
thunkArgument: null,
99
appendAction: false,
1010
reducer: state => state,

packages/frint-store/src/combineReducers.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ describe('frint-store › combineReducers', function () {
7070
]);
7171
});
7272

73+
it('combines multiple reducers with no given initial state', function () {
74+
const rootReducer = combineReducers({
75+
counter: counterReducer,
76+
color: colorReducer
77+
});
78+
const state = rootReducer(undefined, { type: 'DO_NOTHING' });
79+
80+
expect(state).to.deep.equal({
81+
counter: {
82+
value: 0,
83+
},
84+
color: {
85+
value: 'blue',
86+
},
87+
});
88+
});
89+
7390
it('throws error with reducer key name, when individual reducer errors', function () {
7491
const consoleCalls = [];
7592
const fakeConsole = {

packages/frint-store/src/createStore.spec.js

Lines changed: 93 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ describe('frint-store › createStore', function () {
310310
subscription.unsubscribe();
311311
});
312312

313-
it('handles combined reducers', function () {
313+
describe('handles combined reducers', function () {
314314
function counterReducer(state = { value: 0 }, action) {
315315
switch (action.type) {
316316
case 'INCREMENT_COUNTER':
@@ -342,39 +342,104 @@ describe('frint-store › createStore', function () {
342342
color: colorReducer,
343343
});
344344

345-
const Store = createStore({
346-
enableLogger: false,
347-
initialState: {
348-
counter: {
349-
value: 100,
345+
it('with given initial state', function () {
346+
const Store = createStore({
347+
enableLogger: false,
348+
initialState: {
349+
counter: {
350+
value: 100,
351+
},
352+
color: {
353+
value: 'red'
354+
}
350355
},
351-
color: {
352-
value: 'red'
353-
}
354-
},
355-
reducer: rootReducer,
356+
reducer: rootReducer,
357+
});
358+
const store = new Store();
359+
360+
const states = [];
361+
const subscription = store.getState$()
362+
.subscribe((state) => {
363+
states.push(state);
364+
});
365+
366+
store.dispatch({ type: 'INCREMENT_COUNTER' });
367+
store.dispatch({ type: 'INCREMENT_COUNTER' });
368+
store.dispatch({ type: 'DECREMENT_COUNTER' });
369+
store.dispatch({ type: 'SET_COLOR', color: 'green' });
370+
371+
expect(states).to.deep.equal([
372+
{ counter: { value: 100 }, color: { value: 'red' } }, // initial
373+
{ counter: { value: 101 }, color: { value: 'red' } }, // INCREMENT_COUNTER
374+
{ counter: { value: 102 }, color: { value: 'red' } }, // INCREMENT_COUNTER
375+
{ counter: { value: 101 }, color: { value: 'red' } }, // DECREMENT_COUNTER
376+
{ counter: { value: 101 }, color: { value: 'green' } } // SET_COLOR
377+
]);
378+
379+
subscription.unsubscribe();
356380
});
357-
const store = new Store();
358381

359-
const states = [];
360-
const subscription = store.getState$()
361-
.subscribe((state) => {
362-
states.push(state);
382+
it('with no given initial state', function () {
383+
const Store = createStore({
384+
enableLogger: false,
385+
reducer: rootReducer,
363386
});
387+
const store = new Store();
364388

365-
store.dispatch({ type: 'INCREMENT_COUNTER' });
366-
store.dispatch({ type: 'INCREMENT_COUNTER' });
367-
store.dispatch({ type: 'DECREMENT_COUNTER' });
368-
store.dispatch({ type: 'SET_COLOR', color: 'green' });
389+
const states = [];
390+
const subscription = store.getState$()
391+
.subscribe((state) => {
392+
states.push(state);
393+
});
369394

370-
expect(states).to.deep.equal([
371-
{ counter: { value: 100 }, color: { value: 'red' } }, // initial
372-
{ counter: { value: 101 }, color: { value: 'red' } }, // INCREMENT_COUNTER
373-
{ counter: { value: 102 }, color: { value: 'red' } }, // INCREMENT_COUNTER
374-
{ counter: { value: 101 }, color: { value: 'red' } }, // DECREMENT_COUNTER
375-
{ counter: { value: 101 }, color: { value: 'green' } } // SET_COLOR
376-
]);
395+
store.dispatch({ type: 'INCREMENT_COUNTER' });
396+
store.dispatch({ type: 'INCREMENT_COUNTER' });
397+
store.dispatch({ type: 'DECREMENT_COUNTER' });
398+
store.dispatch({ type: 'SET_COLOR', color: 'green' });
377399

378-
subscription.unsubscribe();
400+
expect(states).to.deep.equal([
401+
{ counter: { value: 0 }, color: { value: 'blue' } }, // initial
402+
{ counter: { value: 1 }, color: { value: 'blue' } }, // INCREMENT_COUNTER
403+
{ counter: { value: 2 }, color: { value: 'blue' } }, // INCREMENT_COUNTER
404+
{ counter: { value: 1 }, color: { value: 'blue' } }, // DECREMENT_COUNTER
405+
{ counter: { value: 1 }, color: { value: 'green' } } // SET_COLOR
406+
]);
407+
408+
subscription.unsubscribe();
409+
});
410+
411+
it('with partially given initial state for certain reducers', function () {
412+
const Store = createStore({
413+
enableLogger: false,
414+
initialState: {
415+
counter: {
416+
value: 100,
417+
},
418+
},
419+
reducer: rootReducer,
420+
});
421+
const store = new Store();
422+
423+
const states = [];
424+
const subscription = store.getState$()
425+
.subscribe((state) => {
426+
states.push(state);
427+
});
428+
429+
store.dispatch({ type: 'INCREMENT_COUNTER' });
430+
store.dispatch({ type: 'INCREMENT_COUNTER' });
431+
store.dispatch({ type: 'DECREMENT_COUNTER' });
432+
store.dispatch({ type: 'SET_COLOR', color: 'green' });
433+
434+
expect(states).to.deep.equal([
435+
{ counter: { value: 100 }, color: { value: 'blue' } }, // initial
436+
{ counter: { value: 101 }, color: { value: 'blue' } }, // INCREMENT_COUNTER
437+
{ counter: { value: 102 }, color: { value: 'blue' } }, // INCREMENT_COUNTER
438+
{ counter: { value: 101 }, color: { value: 'blue' } }, // DECREMENT_COUNTER
439+
{ counter: { value: 101 }, color: { value: 'green' } } // SET_COLOR
440+
]);
441+
442+
subscription.unsubscribe();
443+
});
379444
});
380445
});

0 commit comments

Comments
 (0)