Skip to content

Commit

Permalink
fix(store): remove store config from forFeature signature with slice (#…
Browse files Browse the repository at this point in the history
…3218)

Closes #3216

BREAKING CHANGES:

The `StoreConfig` argument is removed from the `StoreModule.forFeature` signature with `FeatureSlice`.

BEFORE:

The `StoreModule.forFeature` signature with `FeatureSlice` has `StoreConfig` as the second input argument, but the configuration isn't registered if passed.

AFTER:

The `StoreModule.forFeature` signature with `FeatureSlice` no longer has `StoreConfig` as the second input argument.
  • Loading branch information
markostanimirovic authored Nov 3, 2021
1 parent 62334f9 commit b1a64dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
13 changes: 7 additions & 6 deletions modules/store/spec/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ describe(`Store Modules`, () => {
);

// Trigger here is basically an action type used to trigger state update
const createDummyReducer = <T>(def: T, trigger: string): ActionReducer<T> => (
s = def,
{ type, payload }: any
) => (type === trigger ? payload : s);
const createDummyReducer =
<T>(def: T, trigger: string): ActionReducer<T> =>
(s = def, { type, payload }: any) =>
type === trigger ? payload : s;
const rootFruitReducer = createDummyReducer('apple', 'fruit');
const featureAReducer = createDummyReducer(5, 'a');
const featureBListReducer = createDummyReducer([1, 2, 3], 'bList');
Expand Down Expand Up @@ -104,7 +104,7 @@ describe(`Store Modules`, () => {
});
});

it(`should should use config.reducerFactory`, (done) => {
it(`should use config.reducerFactory`, (done) => {
store.dispatch({ type: 'fruit', payload: 'banana' });
store.dispatch({ type: 'a', payload: 42 });

Expand Down Expand Up @@ -241,11 +241,12 @@ describe(`Store Modules`, () => {
store = TestBed.inject(Store);
});

it('should set up a feature state', () => {
it('should set up a feature state', (done) => {
store.pipe(take(1)).subscribe((state: State) => {
expect(state).toEqual({
a: 5,
} as State);
done();
});
});
});
Expand Down
11 changes: 10 additions & 1 deletion modules/store/spec/types/store_module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { compilerOptions } from './utils';
describe('StoreModule', () => {
const expectSnippet = expecter(
(code) => `
import {StoreModule,ActionReducerMap,Action} from '@ngrx/store';
import { StoreModule, ActionReducerMap, Action, createReducer } from '@ngrx/store';
interface State {
featureA: object;
Expand Down Expand Up @@ -69,5 +69,14 @@ describe('StoreModule', () => {
/Type '{ notExisting: number; }' is not assignable to type 'InitialState<State>/
);
});

it('throws when store config is passed along with slice object', () => {
expectSnippet(`
StoreModule.forFeature(
{ name: 'feature', reducer: createReducer(0) },
{ initialState: 100, metaReducers: [metaReducer] }
);
`).toFail(/No overload matches this call/);
});
});
});
15 changes: 5 additions & 10 deletions modules/store/src/store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,15 @@ export class StoreModule {
config?: StoreConfig<T, V> | InjectionToken<StoreConfig<T, V>>
): ModuleWithProviders<StoreFeatureModule>;
static forFeature<T, V extends Action = Action>(
slice: FeatureSlice<T, V>,
config?: StoreConfig<T, V> | InjectionToken<StoreConfig<T, V>>
slice: FeatureSlice<T, V>
): ModuleWithProviders<StoreFeatureModule>;
static forFeature(
featureNameOrSlice: string | FeatureSlice<any, any>,
reducersOrConfig?:
reducers?:
| ActionReducerMap<any, any>
| InjectionToken<ActionReducerMap<any, any>>
| ActionReducer<any, any>
| InjectionToken<ActionReducer<any, any>>
| StoreConfig<any, any>
| InjectionToken<StoreConfig<any, any>>,
| InjectionToken<ActionReducer<any, any>>,
config: StoreConfig<any, any> | InjectionToken<StoreConfig<any, any>> = {}
): ModuleWithProviders<StoreFeatureModule> {
return {
Expand Down Expand Up @@ -257,15 +254,13 @@ export class StoreModule {
useValue:
featureNameOrSlice instanceof Object
? featureNameOrSlice.reducer
: reducersOrConfig,
: reducers,
},
{
provide: _FEATURE_REDUCERS_TOKEN,
multi: true,
useExisting:
reducersOrConfig instanceof InjectionToken
? reducersOrConfig
: _FEATURE_REDUCERS,
reducers instanceof InjectionToken ? reducers : _FEATURE_REDUCERS,
},
{
provide: FEATURE_REDUCERS,
Expand Down

0 comments on commit b1a64dd

Please sign in to comment.