Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store): add USER_RUNTIME_CHECKS public token #2006

Merged
merged 1 commit into from
Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion modules/store/spec/runtime_checks.spec.ts
Original file line number Diff line number Diff line change
@@ -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:', () => {
Expand Down Expand Up @@ -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<any>>(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<any>>(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<any>>(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[] = [];
Expand Down
1 change: 1 addition & 0 deletions modules/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export {
META_REDUCERS,
FEATURE_REDUCERS,
USER_PROVIDED_META_REDUCERS,
USER_RUNTIME_CHECKS,
} from './tokens';
export {
StoreModule,
Expand Down
14 changes: 13 additions & 1 deletion modules/store/src/runtime_checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
_USER_RUNTIME_CHECKS,
_ACTIVE_RUNTIME_CHECKS,
META_REDUCERS,
USER_RUNTIME_CHECKS,
} from './tokens';

export function createActiveRuntimeChecks(
Expand Down Expand Up @@ -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,
},
{
Expand All @@ -89,3 +95,9 @@ export function provideRuntimeChecks(
},
];
}

export function _runtimeChecksFactory(
runtimeChecks: RuntimeChecks
): RuntimeChecks {
return runtimeChecks;
}
10 changes: 9 additions & 1 deletion modules/store/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ export const _RESOLVED_META_REDUCERS = new InjectionToken<MetaReducer>(
);

/**
* 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<RuntimeChecks>(
'@ngrx/store User Runtime Checks Config'
);

/**
* Runtime checks defined by the user via forRoot()
*/
export const _USER_RUNTIME_CHECKS = new InjectionToken<RuntimeChecks>(
'@ngrx/store Internal User Runtime Checks Config'
Expand Down