Skip to content

Commit fa8da34

Browse files
timdeschryverbrandonroberts
authored andcommitted
feat(store): add USER_RUNTIME_CHECKS public token (#2006)
Closes #1973
1 parent 5e01e50 commit fa8da34

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

modules/store/spec/runtime_checks.spec.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as ngCore from '@angular/core';
22
import { TestBed, fakeAsync, flush } from '@angular/core/testing';
3-
import { Store, StoreModule, META_REDUCERS } from '..';
3+
import { Store, StoreModule, META_REDUCERS, USER_RUNTIME_CHECKS } from '..';
44
import { createActiveRuntimeChecks } from '../src/runtime_checks';
55
import { RuntimeChecks } from '../src/models';
6+
import * as metaReducers from '../src/meta-reducers';
67

78
describe('Runtime checks:', () => {
89
describe('createActiveRuntimeChecks:', () => {
@@ -68,6 +69,77 @@ describe('Runtime checks:', () => {
6869
});
6970
});
7071

72+
describe('USER_RUNTIME_CHECKS Token', () => {
73+
it('should be possible to toggle runtime reducers via the Injection Token', () => {
74+
const serializationCheckMetaReducerSpy = spyOn(
75+
metaReducers,
76+
'serializationCheckMetaReducer'
77+
).and.callThrough();
78+
79+
TestBed.configureTestingModule({
80+
imports: [StoreModule.forRoot({})],
81+
providers: [
82+
{
83+
provide: USER_RUNTIME_CHECKS,
84+
useValue: {
85+
strictStateSerializability: true,
86+
},
87+
},
88+
],
89+
});
90+
91+
const _store = TestBed.get<Store<any>>(Store);
92+
expect(serializationCheckMetaReducerSpy).toHaveBeenCalled();
93+
});
94+
95+
it('should not create a meta reducer if not desired', () => {
96+
const serializationCheckMetaReducerSpy = spyOn(
97+
metaReducers,
98+
'serializationCheckMetaReducer'
99+
).and.callThrough();
100+
101+
TestBed.configureTestingModule({
102+
imports: [StoreModule.forRoot({})],
103+
providers: [
104+
{
105+
provide: USER_RUNTIME_CHECKS,
106+
useValue: {
107+
strictStateSerializability: false,
108+
},
109+
},
110+
],
111+
});
112+
113+
const _store = TestBed.get<Store<any>>(Store);
114+
expect(serializationCheckMetaReducerSpy).not.toHaveBeenCalled();
115+
});
116+
117+
it('should not create a meta reducer without config', () => {
118+
const serializationCheckMetaReducerSpy = spyOn(
119+
metaReducers,
120+
'serializationCheckMetaReducer'
121+
).and.callThrough();
122+
const immutabilityCheckMetaReducerSpy = spyOn(
123+
metaReducers,
124+
'immutabilityCheckMetaReducer'
125+
).and.callThrough();
126+
127+
TestBed.configureTestingModule({
128+
imports: [StoreModule.forRoot({})],
129+
providers: [
130+
{
131+
provide: USER_RUNTIME_CHECKS,
132+
useValue: {},
133+
},
134+
],
135+
});
136+
137+
const _store = TestBed.get<Store<any>>(Store);
138+
expect(serializationCheckMetaReducerSpy).not.toHaveBeenCalled();
139+
expect(immutabilityCheckMetaReducerSpy).not.toHaveBeenCalled();
140+
});
141+
});
142+
71143
describe('Registering custom meta-reducers:', () => {
72144
it('should invoke internal meta reducers before user defined meta reducers', () => {
73145
let logs: string[] = [];

modules/store/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export {
4242
META_REDUCERS,
4343
FEATURE_REDUCERS,
4444
USER_PROVIDED_META_REDUCERS,
45+
USER_RUNTIME_CHECKS,
4546
} from './tokens';
4647
export {
4748
StoreModule,

modules/store/src/runtime_checks.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
_USER_RUNTIME_CHECKS,
99
_ACTIVE_RUNTIME_CHECKS,
1010
META_REDUCERS,
11+
USER_RUNTIME_CHECKS,
1112
} from './tokens';
1213

1314
export function createActiveRuntimeChecks(
@@ -71,8 +72,13 @@ export function provideRuntimeChecks(
7172
useValue: runtimeChecks,
7273
},
7374
{
74-
provide: _ACTIVE_RUNTIME_CHECKS,
75+
provide: USER_RUNTIME_CHECKS,
76+
useFactory: _runtimeChecksFactory,
7577
deps: [_USER_RUNTIME_CHECKS],
78+
},
79+
{
80+
provide: _ACTIVE_RUNTIME_CHECKS,
81+
deps: [USER_RUNTIME_CHECKS],
7682
useFactory: createActiveRuntimeChecks,
7783
},
7884
{
@@ -89,3 +95,9 @@ export function provideRuntimeChecks(
8995
},
9096
];
9197
}
98+
99+
export function _runtimeChecksFactory(
100+
runtimeChecks: RuntimeChecks
101+
): RuntimeChecks {
102+
return runtimeChecks;
103+
}

modules/store/src/tokens.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ export const _RESOLVED_META_REDUCERS = new InjectionToken<MetaReducer>(
6363
);
6464

6565
/**
66-
* Runtime checks defined by the user
66+
* Runtime checks defined by the user via an InjectionToken
67+
* Defaults to `_USER_RUNTIME_CHECKS`
68+
*/
69+
export const USER_RUNTIME_CHECKS = new InjectionToken<RuntimeChecks>(
70+
'@ngrx/store User Runtime Checks Config'
71+
);
72+
73+
/**
74+
* Runtime checks defined by the user via forRoot()
6775
*/
6876
export const _USER_RUNTIME_CHECKS = new InjectionToken<RuntimeChecks>(
6977
'@ngrx/store Internal User Runtime Checks Config'

0 commit comments

Comments
 (0)