Skip to content

Commit

Permalink
fix(utils): fix compose method breaking initial state with meta reducers
Browse files Browse the repository at this point in the history
Closes ngrx#247
  • Loading branch information
Reko Jokelainen committed Aug 8, 2017
1 parent 84390a1 commit 6e88f1d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
34 changes: 34 additions & 0 deletions modules/store/spec/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,40 @@ describe(`Store Modules`, () => {
});
});

describe(`: With initial state`, () => {
const initialState: RootState = { fruit: 'banana' };
const reducerMap: ActionReducerMap<RootState> = { fruit: rootFruitReducer };
const noopMetaReducer = (r: Function) => (state: any, action: any) => {
console.log('ACTION', state);
return r(state, action);
};

const testWithMetaReducers = (metaReducers: any[]) => () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot(reducerMap, { initialState, metaReducers }),
],
});
store = TestBed.get(Store);
});
it('should have initial state', () => {
store.take(1).subscribe((s: any) => {
expect(s).toEqual(initialState);
});
});
};

describe(
'should add initial state with no meta reducers',
testWithMetaReducers([])
);
describe(
'should add initial state with a simple no-op meta reducer',
testWithMetaReducers([noopMetaReducer])
);
});

describe(`: Nested`, () => {
@NgModule({
imports: [StoreModule.forFeature('a', featureAReducer)],
Expand Down
4 changes: 2 additions & 2 deletions modules/store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ export function compose<A, B, C, D, E, F>(
b: (i: A) => B
): (i: A) => F;
export function compose(...functions: any[]) {
return function(arg: any) {
return function(...arg: any[]) {
if (functions.length === 0) {
return arg;
}

const last = functions[functions.length - 1];
const rest = functions.slice(0, -1);

return rest.reduceRight((composed, fn) => fn(composed), last(arg));
return rest.reduceRight((composed, fn) => fn(composed), last(...arg));
};
}

Expand Down

0 comments on commit 6e88f1d

Please sign in to comment.