Skip to content
This repository has been archived by the owner on Jan 10, 2018. It is now read-only.

Added support for nesting in the combineReducers utility method. #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/edge_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require('reflect-metadata');
import {Store, Dispatcher, State, Action, combineReducers} from '../src/index';
import {StoreModule} from '../src/ng2';
import {Observable} from 'rxjs/Observable';
import {ReflectiveInjector, provide} from '@angular/core';
import {ReflectiveInjector} from '@angular/core';

import {todos, todoCount} from './fixtures/edge_todos';

Expand Down
2 changes: 1 addition & 1 deletion spec/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require('es6-shim');
require('reflect-metadata');
import {Store, StoreModule, Action, combineReducers, INITIAL_REDUCER, INITIAL_STATE} from '../src/index';
import {Observable} from 'rxjs/Observable';
import {ReflectiveInjector, provide} from '@angular/core';
import {ReflectiveInjector} from '@angular/core';
import 'rxjs/add/observable/combineLatest';

import {counterReducer, INCREMENT, DECREMENT, RESET} from './fixtures/counter';
Expand Down
2 changes: 1 addition & 1 deletion spec/state_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require('es6-shim');
require('reflect-metadata');
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {ReflectiveInjector, provide} from '@angular/core';
import {ReflectiveInjector} from '@angular/core';

import {Dispatcher, State, Reducer, Action, provideStore, StoreModule} from '../src/index';

Expand Down
107 changes: 99 additions & 8 deletions spec/store_spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
declare var describe, it, expect, hot, cold, expectObservable, expectSubscriptions, console, beforeEach;
require('es6-shim');
require('reflect-metadata');
import 'rxjs/add/operator/take';
import {Store, Dispatcher, State, Action, combineReducers, StoreModule} from '../src/index';
import {Observable} from 'rxjs/Observable';
import {ReflectiveInjector, provide} from '@angular/core';

import {counterReducer, INCREMENT, DECREMENT, RESET} from './fixtures/counter';
import "rxjs/add/operator/take";
import {Store, Dispatcher, combineReducers, StoreModule} from "../src/index";
import {ReflectiveInjector} from "@angular/core";
import {counterReducer, INCREMENT, DECREMENT, RESET} from "./fixtures/counter";

interface TestAppSchema {
counter1: number;
counter2: number;
counter3: number;
}

interface Todo { }
interface TestAppSchema2 {
counter1: number;
counter2: TestAppSchemaCounter2;
}

interface TestAppSchemaCounter2 {
counter2a: number;
}

interface Todo {
}

interface TodoAppSchema {
visibilityFilter: string;
todos: Todo[];
}



describe('ngRx Store', () => {

describe('basic store actions', function() {
Expand Down Expand Up @@ -194,4 +201,88 @@ describe('ngRx Store', () => {
expect(dispatcherSubscription.isUnsubscribed).toBe(false);
});
});

describe('store with nested reducer tree', () => {
let injector: ReflectiveInjector;
let store: Store<TestAppSchema2>;
let dispatcher: Dispatcher;

beforeEach(() => {
const rootReducer = combineReducers({
counter1: counterReducer,
counter2: {
counter2a: counterReducer
}
});

const initialValue =
{
counter1: 0,
counter2: {
counter2a: 1
}
};

injector = ReflectiveInjector.resolveAndCreate([
StoreModule.provideStore(rootReducer, initialValue).providers
]);

store = injector.get(Store);
dispatcher = injector.get(Dispatcher);
});

const actionSequence = '--a--b--c--d--e';
const actionValues = {
a: {type: INCREMENT},
b: {type: INCREMENT},
c: {type: DECREMENT},
d: {type: RESET},
e: {type: INCREMENT}
};

it('should create a nested reducer structure', () => {
const storeStructure = store.select(s => s);

store.take(1).subscribe((state) => {
expect(state).toEqual({counter1: 0, counter2: {counter2a: 1}});
});
});

it('should let you select the nested state via string or function', function () {

const counterSteps = hot(actionSequence, actionValues);

counterSteps.subscribe((action) => store.dispatch(action));

const counterStateWithString = store.select('counter2','counter2a');
const counterStateWithFunc = store.select(s => s.counter2.counter2a);

const stateSequence = 'i-v--w--x--y--z';
const counter2aValues = {i: 1, v: 2, w: 3, x: 2, y: 0, z: 1};

expectObservable(counterStateWithString).toBe(stateSequence, counter2aValues);
expectObservable(counterStateWithFunc).toBe(stateSequence, counter2aValues);
});

it('should increment and decrement counter2', function () {

const counterSteps = hot(actionSequence, actionValues);

counterSteps.subscribe((action) => store.dispatch(action));

const counterState = store.select('counter2');

const stateSequence = 'i-v--w--x--y--z';
const counter1Values = {
i: {counter2a: 1},
v: {counter2a: 2},
w: {counter2a: 3},
x: {counter2a: 2},
y: {counter2a: 0},
z: {counter2a: 1}
};

expectObservable(counterState).toBe(stateSequence, counter1Values);
});
});
});
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export function combineReducers(reducers: any): ActionReducer<any> {
const key = reducerKeys[i];
if (typeof reducers[key] === 'function') {
finalReducers[key] = reducers[key];
} else {
finalReducers[key] = combineReducers(reducers[key]);
}
}

Expand Down