diff --git a/modules/store/spec/runtime_checks.spec.ts b/modules/store/spec/runtime_checks.spec.ts index c2e26ff0dd..000d4463a6 100644 --- a/modules/store/spec/runtime_checks.spec.ts +++ b/modules/store/spec/runtime_checks.spec.ts @@ -1,8 +1,9 @@ import * as ngCore from '@angular/core'; import { TestBed, fakeAsync, flush } from '@angular/core/testing'; -import { Store, StoreModule, META_REDUCERS } from '..'; +import { Store, StoreModule, META_REDUCERS, USER_RUNTIME_CHECKS } from '..'; import { createActiveRuntimeChecks } from '../src/runtime_checks'; import { RuntimeChecks } from '../src/models'; +import * as metaReducers from '../src/meta-reducers'; describe('Runtime checks:', () => { describe('createActiveRuntimeChecks:', () => { @@ -68,6 +69,77 @@ describe('Runtime checks:', () => { }); }); + describe('USER_RUNTIME_CHECKS Token', () => { + it('should be possible to toggle runtime reducers via the Injection Token', () => { + const serializationCheckMetaReducerSpy = spyOn( + metaReducers, + 'serializationCheckMetaReducer' + ).and.callThrough(); + + TestBed.configureTestingModule({ + imports: [StoreModule.forRoot({})], + providers: [ + { + provide: USER_RUNTIME_CHECKS, + useValue: { + strictStateSerializability: true, + }, + }, + ], + }); + + const _store = TestBed.get>(Store); + expect(serializationCheckMetaReducerSpy).toHaveBeenCalled(); + }); + + it('should not create a meta reducer if not desired', () => { + const serializationCheckMetaReducerSpy = spyOn( + metaReducers, + 'serializationCheckMetaReducer' + ).and.callThrough(); + + TestBed.configureTestingModule({ + imports: [StoreModule.forRoot({})], + providers: [ + { + provide: USER_RUNTIME_CHECKS, + useValue: { + strictStateSerializability: false, + }, + }, + ], + }); + + const _store = TestBed.get>(Store); + expect(serializationCheckMetaReducerSpy).not.toHaveBeenCalled(); + }); + + it('should not create a meta reducer without config', () => { + const serializationCheckMetaReducerSpy = spyOn( + metaReducers, + 'serializationCheckMetaReducer' + ).and.callThrough(); + const immutabilityCheckMetaReducerSpy = spyOn( + metaReducers, + 'immutabilityCheckMetaReducer' + ).and.callThrough(); + + TestBed.configureTestingModule({ + imports: [StoreModule.forRoot({})], + providers: [ + { + provide: USER_RUNTIME_CHECKS, + useValue: {}, + }, + ], + }); + + const _store = TestBed.get>(Store); + expect(serializationCheckMetaReducerSpy).not.toHaveBeenCalled(); + expect(immutabilityCheckMetaReducerSpy).not.toHaveBeenCalled(); + }); + }); + describe('Registering custom meta-reducers:', () => { it('should invoke internal meta reducers before user defined meta reducers', () => { let logs: string[] = []; diff --git a/modules/store/src/index.ts b/modules/store/src/index.ts index be43ba62c2..2b4eb53639 100644 --- a/modules/store/src/index.ts +++ b/modules/store/src/index.ts @@ -42,6 +42,7 @@ export { META_REDUCERS, FEATURE_REDUCERS, USER_PROVIDED_META_REDUCERS, + USER_RUNTIME_CHECKS, } from './tokens'; export { StoreModule, diff --git a/modules/store/src/runtime_checks.ts b/modules/store/src/runtime_checks.ts index 9d7edddf14..0fe4b057e9 100644 --- a/modules/store/src/runtime_checks.ts +++ b/modules/store/src/runtime_checks.ts @@ -8,6 +8,7 @@ import { _USER_RUNTIME_CHECKS, _ACTIVE_RUNTIME_CHECKS, META_REDUCERS, + USER_RUNTIME_CHECKS, } from './tokens'; export function createActiveRuntimeChecks( @@ -71,8 +72,13 @@ export function provideRuntimeChecks( useValue: runtimeChecks, }, { - provide: _ACTIVE_RUNTIME_CHECKS, + provide: USER_RUNTIME_CHECKS, + useFactory: _runtimeChecksFactory, deps: [_USER_RUNTIME_CHECKS], + }, + { + provide: _ACTIVE_RUNTIME_CHECKS, + deps: [USER_RUNTIME_CHECKS], useFactory: createActiveRuntimeChecks, }, { @@ -89,3 +95,9 @@ export function provideRuntimeChecks( }, ]; } + +export function _runtimeChecksFactory( + runtimeChecks: RuntimeChecks +): RuntimeChecks { + return runtimeChecks; +} diff --git a/modules/store/src/tokens.ts b/modules/store/src/tokens.ts index 6709c6e9e7..7aa12edb47 100644 --- a/modules/store/src/tokens.ts +++ b/modules/store/src/tokens.ts @@ -63,7 +63,15 @@ export const _RESOLVED_META_REDUCERS = new InjectionToken( ); /** - * Runtime checks defined by the user + * Runtime checks defined by the user via an InjectionToken + * Defaults to `_USER_RUNTIME_CHECKS` + */ +export const USER_RUNTIME_CHECKS = new InjectionToken( + '@ngrx/store User Runtime Checks Config' +); + +/** + * Runtime checks defined by the user via forRoot() */ export const _USER_RUNTIME_CHECKS = new InjectionToken( '@ngrx/store Internal User Runtime Checks Config'