Skip to content

Commit

Permalink
feat(store): add ngrxMockEnvironment function to control output durin…
Browse files Browse the repository at this point in the history
…g testing (#2513)

Closes #2363
  • Loading branch information
evgenyfedorenko committed May 12, 2020
1 parent 534aa09 commit da1a0c0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
17 changes: 17 additions & 0 deletions modules/store/spec/flags.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { isNgrxMockEnvironment, setNgrxMockEnvironment } from '../src/flags';

describe(`Store flags`, () => {
describe(`isNgrxMockEnvironment()`, () => {
it(`should return false by default`, () => {
expect(isNgrxMockEnvironment()).toBe(false);
});

it(`should return the correct flag`, () => {
setNgrxMockEnvironment(true);
expect(isNgrxMockEnvironment()).toBe(true);

setNgrxMockEnvironment(false);
expect(isNgrxMockEnvironment()).toBe(false);
});
});
});
14 changes: 12 additions & 2 deletions modules/store/spec/selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
MemoizedProjection,
} from '@ngrx/store';
import { map, distinctUntilChanged } from 'rxjs/operators';
import { setNgrxMockEnvironment } from '../src';

describe('Selectors', () => {
let countOne: number;
Expand Down Expand Up @@ -513,8 +514,8 @@ describe('Selectors', () => {
});
});

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

Expand All @@ -525,6 +526,15 @@ describe('Selectors', () => {
/The feature name "featureB" does not exist/
);
});

it('not be logged when in mock environment', () => {
setNgrxMockEnvironment(true);
const selector = createFeatureSelector('featureB');

selector({ featureA: {} });

expect(warnSpy).not.toHaveBeenCalled();
});
});
});
});
Expand Down
7 changes: 7 additions & 0 deletions modules/store/src/flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let _ngrxMockEnvironment = false;
export function setNgrxMockEnvironment(value: boolean): void {
_ngrxMockEnvironment = value;
}
export function isNgrxMockEnvironment(): boolean {
return _ngrxMockEnvironment;
}
1 change: 1 addition & 0 deletions modules/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { createAction, props, union } from './action_creator';
export { Store, select } from './store';
export { combineReducers, compose, createReducerFactory } from './utils';
export { ActionsSubject, INIT } from './actions_subject';
export { setNgrxMockEnvironment, isNgrxMockEnvironment } from './flags';
export {
ReducerManager,
ReducerObservable,
Expand Down
3 changes: 2 additions & 1 deletion modules/store/src/selector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Selector, SelectorWithProps } from './models';
import { isDevMode } from '@angular/core';
import { isNgrxMockEnvironment } from '../public_api';

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

Expand Down Expand Up @@ -613,7 +614,7 @@ export function createFeatureSelector(
): MemoizedSelector<any, any> {
return createSelector((state: any) => {
const featureState = state[featureName];
if (isDevMode() && !(featureName in state)) {
if (!isNgrxMockEnvironment() && isDevMode() && !(featureName in state)) {
console.warn(
`@ngrx/store: The feature name \"${featureName}\" does ` +
'not exist in the state, therefore createFeatureSelector ' +
Expand Down
12 changes: 10 additions & 2 deletions modules/store/testing/spec/mock_store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { skip, take, tap } from 'rxjs/operators';
import { skip, take } from 'rxjs/operators';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import {
Store,
Expand All @@ -10,10 +10,12 @@ import {
createFeatureSelector,
} from '@ngrx/store';
import { INCREMENT } from '../../spec/fixtures/counter';
import { Component, OnInit } from '@angular/core';
import { Component } from '@angular/core';
import { Observable } from 'rxjs';
import { By } from '@angular/platform-browser';

import * as store from '@ngrx/store';

interface TestAppSchema {
counter1: number;
counter2: number;
Expand All @@ -40,6 +42,8 @@ describe('Mock Store', () => {
);

beforeEach(() => {
spyOn(store, 'setNgrxMockEnvironment').and.callThrough();

TestBed.configureTestingModule({
providers: [
provideMockStore({
Expand All @@ -63,6 +67,10 @@ describe('Mock Store', () => {
mockStore.resetSelectors();
});

it('should set NgrxMockEnvironment', () => {
expect(store.setNgrxMockEnvironment).toHaveBeenCalledWith(true);
});

it('should provide the same instance with Store and MockStore', () => {
const fromStore = TestBed.inject(Store);
const fromMockStore = TestBed.inject(MockStore);
Expand Down
2 changes: 2 additions & 0 deletions modules/store/testing/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ReducerManager,
StateObservable,
Store,
setNgrxMockEnvironment,
} from '@ngrx/store';
import { MockStore } from './mock_store';
import { MockReducerManager } from './mock_reducer_manager';
Expand All @@ -20,6 +21,7 @@ export interface MockStoreConfig<T> {
export function provideMockStore<T = any>(
config: MockStoreConfig<T> = {}
): Provider[] {
setNgrxMockEnvironment(true);
return [
ActionsSubject,
MockState,
Expand Down

0 comments on commit da1a0c0

Please sign in to comment.