-
-
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): split immutibility checks in state and action checks (#1894
- Loading branch information
1 parent
2fb8d67
commit c59c211
Showing
17 changed files
with
359 additions
and
288 deletions.
There are no files selected for viewing
23 changes: 0 additions & 23 deletions
23
modules/store/spec/meta-reducers/action_serialization_reducer.spec.ts
This file was deleted.
Oops, something went wrong.
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
111 changes: 111 additions & 0 deletions
111
modules/store/spec/meta-reducers/serialization_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,111 @@ | ||
import { serializationCheckMetaReducer } from '../../src/meta-reducers'; | ||
|
||
describe('serializationCheckMetaReducer:', () => { | ||
const serializables: Record<string, any> = { | ||
number: { value: 4 }, | ||
boolean: { value: true }, | ||
string: { value: 'foobar' }, | ||
array: { value: [1, 2, 3] }, | ||
object: { value: {} }, | ||
nested: { value: { number: 7, array: ['n', 'g', 'r', 'x'] } }, | ||
null: { value: null }, | ||
undefined: { value: undefined }, | ||
}; | ||
|
||
const unSerializables: Record<string, any> = { | ||
date: { value: new Date() }, | ||
map: { value: new Map() }, | ||
set: { value: new Set() }, | ||
class: { value: new class {}() }, | ||
function: { value: () => {} }, | ||
}; | ||
|
||
describe('serializable:', () => { | ||
Object.keys(serializables).forEach(key => { | ||
it(`action with ${key} should not throw`, () => { | ||
expect(() => | ||
invokeActionReducer({ type: 'valid', payload: serializables[key] }) | ||
).not.toThrow(); | ||
}); | ||
|
||
it(`state with ${key} should not throw`, () => { | ||
expect(() => invokeStateReducer(serializables[key])).not.toThrow(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('unserializable:', () => { | ||
Object.keys(unSerializables).forEach(key => { | ||
it(`action with ${key} should throw`, () => { | ||
expect(() => | ||
invokeActionReducer({ type: 'valid', payload: unSerializables[key] }) | ||
).toThrow(); | ||
}); | ||
|
||
it(`state with ${key} should throw`, () => { | ||
expect(() => invokeStateReducer(unSerializables[key])).toThrow(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('actions: ', () => { | ||
it('should not throw if check is off', () => { | ||
expect(() => | ||
invokeActionReducer({ type: 'valid', payload: unSerializables }, false) | ||
); | ||
}); | ||
|
||
it('should log the path that is not serializable', () => { | ||
expect(() => | ||
invokeActionReducer({ | ||
type: 'valid', | ||
payload: { foo: { bar: unSerializables['date'] } }, | ||
}) | ||
).toThrowError( | ||
/Detected unserializable action at "payload.foo.bar.value"/ | ||
); | ||
}); | ||
}); | ||
|
||
describe('state: ', () => { | ||
it('should not throw if check is off', () => { | ||
expect(() => invokeStateReducer(unSerializables, false)).not.toThrow(); | ||
}); | ||
|
||
it('should log the path that is not serializable', () => { | ||
expect(() => | ||
invokeStateReducer({ | ||
foo: { bar: unSerializables['date'] }, | ||
}) | ||
).toThrowError(/Detected unserializable state at "foo.bar.value"/); | ||
}); | ||
|
||
it('should not throw if state is null', () => { | ||
expect(() => invokeStateReducer(null)).toThrowError( | ||
/Detected unserializable state at "root"/ | ||
); | ||
}); | ||
|
||
it('should not throw if state is undefined', () => { | ||
expect(() => invokeStateReducer(undefined)).toThrowError( | ||
/Detected unserializable state at "root"/ | ||
); | ||
}); | ||
}); | ||
|
||
function invokeActionReducer(action: any, checkIsOn = true) { | ||
serializationCheckMetaReducer(state => state, { | ||
action: checkIsOn, | ||
state: false, | ||
})(undefined, action); | ||
} | ||
|
||
function invokeStateReducer(nextState?: any, checkIsOn = true) { | ||
serializationCheckMetaReducer(() => nextState, { | ||
state: checkIsOn, | ||
action: false, | ||
})(undefined, { | ||
type: 'invokeReducer', | ||
}); | ||
} | ||
}); |
25 changes: 0 additions & 25 deletions
25
modules/store/spec/meta-reducers/state_serialization_reducer.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.