Skip to content

Commit e4765d6

Browse files
timdeschryverbrandonroberts
authored andcommitted
fix(store): improve createFeatureSelector warning (#2163)
Closes #2116
1 parent 8c7c42c commit e4765d6

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

modules/store/spec/selector.spec.ts

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ describe('Selectors', () => {
455455
});
456456

457457
describe('createFeatureSelector', () => {
458-
let featureName = '@ngrx/router-store';
458+
let featureName = 'featureA';
459459
let featureSelector: (state: any) => number;
460460
let warnSpy: jasmine.Spy;
461461

@@ -481,50 +481,51 @@ describe('Selectors', () => {
481481
);
482482

483483
expect(featureState$).toBeObservable(expected$);
484-
expect(warnSpy).not.toHaveBeenCalled();
485484
});
486485

487-
it('should warn if the feature does not exist in the state', () => {
488-
spyOn(ngCore, 'isDevMode').and.returnValue(true);
486+
describe('Warning', () => {
487+
describe('should not log when: ', () => {
488+
it('the feature does exist', () => {
489+
spyOn(ngCore, 'isDevMode').and.returnValue(true);
490+
const selector = createFeatureSelector('featureA');
489491

490-
const state = { otherState: '' };
492+
selector({ featureA: {} });
491493

492-
const state$ = cold('a', { a: state });
493-
const expected$ = cold('a', { a: undefined });
494+
expect(warnSpy).not.toHaveBeenCalled();
495+
});
494496

495-
const featureState$ = state$.pipe(
496-
map(featureSelector),
497-
distinctUntilChanged()
498-
);
497+
it('the feature key exist but is falsy', () => {
498+
spyOn(ngCore, 'isDevMode').and.returnValue(true);
499+
const selector = createFeatureSelector('featureB');
499500

500-
expect(featureState$).toBeObservable(expected$);
501-
expect(warnSpy).toHaveBeenCalledWith(
502-
'The feature name "@ngrx/router-store" does not exist ' +
503-
'in the state, therefore createFeatureSelector cannot ' +
504-
'access it. Be sure it is imported in a loaded module using ' +
505-
"StoreModule.forRoot('@ngrx/router-store', ...) or " +
506-
"StoreModule.forFeature('@ngrx/router-store', ...). If the " +
507-
'default state is intended to be undefined, as is the case ' +
508-
'with router state, this development-only warning message can ' +
509-
'be ignored.'
510-
);
511-
});
501+
selector({ featureA: {}, featureB: undefined });
512502

513-
it('should not warn if not in development mode', () => {
514-
spyOn(ngCore, 'isDevMode').and.returnValue(false);
503+
expect(warnSpy).not.toHaveBeenCalled();
504+
});
515505

516-
const state = { otherState: '' };
506+
it('not in development mode', () => {
507+
spyOn(ngCore, 'isDevMode').and.returnValue(false);
508+
const selector = createFeatureSelector('featureB');
517509

518-
const state$ = cold('a', { a: state });
519-
const expected$ = cold('a', { a: undefined });
510+
selector({ featureA: {} });
520511

521-
const featureState$ = state$.pipe(
522-
map(featureSelector),
523-
distinctUntilChanged()
524-
);
512+
expect(warnSpy).not.toHaveBeenCalled();
513+
});
514+
});
525515

526-
expect(featureState$).toBeObservable(expected$);
527-
expect(warnSpy).not.toHaveBeenCalled();
516+
describe('should log when: ', () => {
517+
it('feature key does not exist', () => {
518+
spyOn(ngCore, 'isDevMode').and.returnValue(true);
519+
const selector = createFeatureSelector('featureB');
520+
521+
selector({ featureA: {} });
522+
523+
expect(warnSpy).toHaveBeenCalled();
524+
expect(warnSpy.calls.mostRecent().args[0]).toMatch(
525+
/The feature name "featureB" does not exist/
526+
);
527+
});
528+
});
528529
});
529530
});
530531

modules/store/src/selector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ export function createFeatureSelector(
605605
): MemoizedSelector<any, any> {
606606
return createSelector((state: any) => {
607607
const featureState = state[featureName];
608-
if (isDevMode() && featureState === undefined) {
608+
if (isDevMode() && !(featureName in state)) {
609609
console.warn(
610-
`The feature name \"${featureName}\" does ` +
610+
`@ngrx/store: The feature name \"${featureName}\" does ` +
611611
'not exist in the state, therefore createFeatureSelector ' +
612612
'cannot access it. Be sure it is imported in a loaded module ' +
613613
`using StoreModule.forRoot('${featureName}', ...) or ` +

0 commit comments

Comments
 (0)