-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): add strictActionWithinNgZone runtime check (#2364)
Closes #2339
- Loading branch information
Stephen Cooper
authored
Feb 11, 2020
1 parent
234ce84
commit 4cae255
Showing
11 changed files
with
196 additions
and
14 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
modules/store/spec/meta-reducers/inNgZoneAssert_reducer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as ngCore from '@angular/core'; | ||
import { inNgZoneAssertMetaReducer } from '../../src/meta-reducers'; | ||
|
||
describe('inNgZoneAssertMetaReducer:', () => { | ||
it('should not throw if in NgZone', () => { | ||
ngCore.NgZone.isInAngularZone = jasmine | ||
.createSpy('isInAngularZone') | ||
.and.returnValue(true); | ||
expect(() => invokeActionReducer((state: any) => state)).not.toThrow(); | ||
expect(ngCore.NgZone.isInAngularZone).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw when not in NgZone', () => { | ||
ngCore.NgZone.isInAngularZone = jasmine | ||
.createSpy('isInAngularZone') | ||
.and.returnValue(false); | ||
expect(() => invokeActionReducer((state: any) => state)).toThrowError( | ||
`Action 'invoke' running outside NgZone. https://ngrx.io/guide/store/configuration/runtime-checks#strictactionwithinngzone` | ||
); | ||
expect(ngCore.NgZone.isInAngularZone).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call isInAngularZone when check is off', () => { | ||
ngCore.NgZone.isInAngularZone = jasmine.createSpy('isInAngularZone'); | ||
expect(() => | ||
invokeActionReducer((state: any) => state, false) | ||
).not.toThrow(); | ||
expect(ngCore.NgZone.isInAngularZone).not.toHaveBeenCalled(); | ||
}); | ||
|
||
function invokeActionReducer(reduce: Function, checkIsOn = true) { | ||
inNgZoneAssertMetaReducer((state, action) => reduce(state, action), { | ||
action: () => checkIsOn, | ||
})({}, { type: 'invoke' }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as ngCore from '@angular/core'; | ||
import { Action, ActionReducer } from '../models'; | ||
import { RUNTIME_CHECK_URL } from './utils'; | ||
|
||
export function inNgZoneAssertMetaReducer( | ||
reducer: ActionReducer<any, Action>, | ||
checks: { action: (action: Action) => boolean } | ||
) { | ||
return function(state: any, action: Action) { | ||
if (checks.action(action) && !ngCore.NgZone.isInAngularZone()) { | ||
throw new Error( | ||
`Action '${ | ||
action.type | ||
}' running outside NgZone. ${RUNTIME_CHECK_URL}#strictactionwithinngzone` | ||
); | ||
} | ||
return reducer(state, action); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { immutabilityCheckMetaReducer } from './immutability_reducer'; | ||
export { serializationCheckMetaReducer } from './serialization_reducer'; | ||
export { inNgZoneAssertMetaReducer } from './inNgZoneAssert_reducer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters