Skip to content

Commit da1a0c0

Browse files
feat(store): add ngrxMockEnvironment function to control output during testing (#2513)
Closes #2363
1 parent 534aa09 commit da1a0c0

File tree

7 files changed

+51
-5
lines changed

7 files changed

+51
-5
lines changed

modules/store/spec/flags.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { isNgrxMockEnvironment, setNgrxMockEnvironment } from '../src/flags';
2+
3+
describe(`Store flags`, () => {
4+
describe(`isNgrxMockEnvironment()`, () => {
5+
it(`should return false by default`, () => {
6+
expect(isNgrxMockEnvironment()).toBe(false);
7+
});
8+
9+
it(`should return the correct flag`, () => {
10+
setNgrxMockEnvironment(true);
11+
expect(isNgrxMockEnvironment()).toBe(true);
12+
13+
setNgrxMockEnvironment(false);
14+
expect(isNgrxMockEnvironment()).toBe(false);
15+
});
16+
});
17+
});

modules/store/spec/selector.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
MemoizedProjection,
1010
} from '@ngrx/store';
1111
import { map, distinctUntilChanged } from 'rxjs/operators';
12+
import { setNgrxMockEnvironment } from '../src';
1213

1314
describe('Selectors', () => {
1415
let countOne: number;
@@ -513,8 +514,8 @@ describe('Selectors', () => {
513514
});
514515
});
515516

516-
describe('should log when: ', () => {
517-
it('feature key does not exist', () => {
517+
describe('warning will ', () => {
518+
it('be logged when not in mock environment', () => {
518519
spyOn(ngCore, 'isDevMode').and.returnValue(true);
519520
const selector = createFeatureSelector('featureB');
520521

@@ -525,6 +526,15 @@ describe('Selectors', () => {
525526
/The feature name "featureB" does not exist/
526527
);
527528
});
529+
530+
it('not be logged when in mock environment', () => {
531+
setNgrxMockEnvironment(true);
532+
const selector = createFeatureSelector('featureB');
533+
534+
selector({ featureA: {} });
535+
536+
expect(warnSpy).not.toHaveBeenCalled();
537+
});
528538
});
529539
});
530540
});

modules/store/src/flags.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let _ngrxMockEnvironment = false;
2+
export function setNgrxMockEnvironment(value: boolean): void {
3+
_ngrxMockEnvironment = value;
4+
}
5+
export function isNgrxMockEnvironment(): boolean {
6+
return _ngrxMockEnvironment;
7+
}

modules/store/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export { createAction, props, union } from './action_creator';
1515
export { Store, select } from './store';
1616
export { combineReducers, compose, createReducerFactory } from './utils';
1717
export { ActionsSubject, INIT } from './actions_subject';
18+
export { setNgrxMockEnvironment, isNgrxMockEnvironment } from './flags';
1819
export {
1920
ReducerManager,
2021
ReducerObservable,

modules/store/src/selector.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Selector, SelectorWithProps } from './models';
22
import { isDevMode } from '@angular/core';
3+
import { isNgrxMockEnvironment } from '../public_api';
34

45
export type AnyFn = (...args: any[]) => any;
56

@@ -613,7 +614,7 @@ export function createFeatureSelector(
613614
): MemoizedSelector<any, any> {
614615
return createSelector((state: any) => {
615616
const featureState = state[featureName];
616-
if (isDevMode() && !(featureName in state)) {
617+
if (!isNgrxMockEnvironment() && isDevMode() && !(featureName in state)) {
617618
console.warn(
618619
`@ngrx/store: The feature name \"${featureName}\" does ` +
619620
'not exist in the state, therefore createFeatureSelector ' +

modules/store/testing/spec/mock_store.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TestBed, ComponentFixture } from '@angular/core/testing';
2-
import { skip, take, tap } from 'rxjs/operators';
2+
import { skip, take } from 'rxjs/operators';
33
import { MockStore, provideMockStore } from '@ngrx/store/testing';
44
import {
55
Store,
@@ -10,10 +10,12 @@ import {
1010
createFeatureSelector,
1111
} from '@ngrx/store';
1212
import { INCREMENT } from '../../spec/fixtures/counter';
13-
import { Component, OnInit } from '@angular/core';
13+
import { Component } from '@angular/core';
1414
import { Observable } from 'rxjs';
1515
import { By } from '@angular/platform-browser';
1616

17+
import * as store from '@ngrx/store';
18+
1719
interface TestAppSchema {
1820
counter1: number;
1921
counter2: number;
@@ -40,6 +42,8 @@ describe('Mock Store', () => {
4042
);
4143

4244
beforeEach(() => {
45+
spyOn(store, 'setNgrxMockEnvironment').and.callThrough();
46+
4347
TestBed.configureTestingModule({
4448
providers: [
4549
provideMockStore({
@@ -63,6 +67,10 @@ describe('Mock Store', () => {
6367
mockStore.resetSelectors();
6468
});
6569

70+
it('should set NgrxMockEnvironment', () => {
71+
expect(store.setNgrxMockEnvironment).toHaveBeenCalledWith(true);
72+
});
73+
6674
it('should provide the same instance with Store and MockStore', () => {
6775
const fromStore = TestBed.inject(Store);
6876
const fromMockStore = TestBed.inject(MockStore);

modules/store/testing/src/testing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ReducerManager,
77
StateObservable,
88
Store,
9+
setNgrxMockEnvironment,
910
} from '@ngrx/store';
1011
import { MockStore } from './mock_store';
1112
import { MockReducerManager } from './mock_reducer_manager';
@@ -20,6 +21,7 @@ export interface MockStoreConfig<T> {
2021
export function provideMockStore<T = any>(
2122
config: MockStoreConfig<T> = {}
2223
): Provider[] {
24+
setNgrxMockEnvironment(true);
2325
return [
2426
ActionsSubject,
2527
MockState,

0 commit comments

Comments
 (0)