Skip to content

Commit

Permalink
feat(store): make reducers accessible from ReducerManager (#3064)
Browse files Browse the repository at this point in the history
* feat(reducer-manager): permit to access reducers

* ref: rename reducerSnapshot to currentReducers

* test: apply check if getter for currentReducers works as expected
  • Loading branch information
GregOnNet authored Jul 6, 2021
1 parent 97643eb commit bf2bd1a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
35 changes: 35 additions & 0 deletions modules/store/spec/reducer_manager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TestBed } from '@angular/core/testing';
import { createReducer, ReducerManager, StoreModule } from '@ngrx/store';

describe(ReducerManager.name, () => {
it('should provide reducers being registered in store', () => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({
'feature-1': createReducer(0),
}),
],
});

const reducerManager = TestBed.inject(ReducerManager);

expect(Object.keys(reducerManager.currentReducers)).toContain('feature-1');
});

it('should provide reducers being registered at runtime', () => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({
'feature-1': createReducer(0),
}),
],
});

const reducerManager = TestBed.inject(ReducerManager);

reducerManager.addReducer('feature-2', createReducer(0));

expect(Object.keys(reducerManager.currentReducers)).toContain('feature-1');
expect(Object.keys(reducerManager.currentReducers)).toContain('feature-2');
});
});
5 changes: 4 additions & 1 deletion modules/store/src/reducer_manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Inject, Injectable, OnDestroy, Provider } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';

import { ActionsSubject } from './actions_subject';
import {
Action,
Expand All @@ -26,6 +25,10 @@ export const UPDATE = '@ngrx/store/update-reducers' as const;
export class ReducerManager
extends BehaviorSubject<ActionReducer<any, any>>
implements OnDestroy {
get currentReducers(): ActionReducerMap<any, any> {
return this.reducers;
}

constructor(
private dispatcher: ReducerManagerDispatcher,
@Inject(INITIAL_STATE) private initialState: any,
Expand Down

0 comments on commit bf2bd1a

Please sign in to comment.