Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
181 additions
and
33 deletions.
There are no files selected for viewing
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
File renamed without changes.
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,112 @@ | ||
import { createDynamicSelector } from '../src'; | ||
import DebugInfoCheckUtil from './util/debugInfoCheckUtil'; | ||
|
||
describe('exceptions', () => { | ||
test('throws exceptions when uncaught', () => { | ||
const childSelector = createDynamicSelector((getState) => { | ||
return getState('a'); | ||
}); | ||
const parentSelector = createDynamicSelector( | ||
(_getState, { multiplier }: { multiplier: number }) => { | ||
const result = childSelector() * multiplier; | ||
if (result % 2) { | ||
throw new Error('Odd number!'); | ||
} | ||
return result; | ||
}, | ||
); | ||
|
||
const childSelectorCheck = new DebugInfoCheckUtil(childSelector); | ||
const parentSelectorCheck10 = new DebugInfoCheckUtil(parentSelector, { multiplier: 10 }); | ||
const parentSelectorCheck11 = new DebugInfoCheckUtil(parentSelector, { multiplier: 11 }); | ||
|
||
let state = { a: 1 }; | ||
|
||
expect(parentSelector(state, { multiplier: 10 })).toEqual(10); | ||
parentSelectorCheck10.expectInvoked('run'); | ||
childSelectorCheck.expectInvoked('run'); | ||
|
||
expect(() => parentSelector(state, { multiplier: 11 })).toThrowError('Odd number!'); | ||
parentSelectorCheck11.expectInvoked('aborted'); | ||
childSelectorCheck.expectInvoked('skipped'); | ||
|
||
// '10' and child are still cached, '11' is not | ||
expect(parentSelector.hasCachedResult(state, { multiplier: 10 })).toEqual(true); | ||
expect(parentSelector.getCachedResult(state, { multiplier: 10 })).toEqual(10); | ||
expect(childSelector.hasCachedResult(state)).toEqual(true); | ||
}); | ||
|
||
test('catch exception thrown by child', () => { | ||
const childSelector = createDynamicSelector((getState) => { | ||
const result = getState('a'); | ||
if (result % 2) { | ||
throw new Error('Odd number!'); | ||
} | ||
return result; | ||
}); | ||
const parentSelector = createDynamicSelector( | ||
(_getState, { multiplier }: { multiplier: number }) => { | ||
let result = 0; | ||
try { | ||
result = childSelector() * multiplier; | ||
} catch (e) {} | ||
|
||
return result; | ||
}, | ||
); | ||
|
||
let state = { a: 2 }; | ||
|
||
expect(parentSelector(state, { multiplier: 10 })).toEqual(20); | ||
|
||
state = { a: 3 }; | ||
|
||
expect(parentSelector(state, { multiplier: 10 })).toEqual(0); | ||
// Parent is cached, child is not | ||
expect(parentSelector.hasCachedResult(state, { multiplier: 10 })).toEqual(true); | ||
expect(parentSelector.getCachedResult(state, { multiplier: 10 })).toEqual(0); | ||
expect(childSelector.hasCachedResult(state)).toEqual(false); | ||
}); | ||
|
||
test('parent overrides exceptions with onError', () => { | ||
const childSelector = createDynamicSelector((getState) => { | ||
return getState('a'); | ||
}); | ||
const parentSelector = createDynamicSelector( | ||
(_getState, { multiplier }: { multiplier: number }) => { | ||
const result = childSelector() * multiplier; | ||
if (result % 2) { | ||
throw new Error('Odd number!'); | ||
} | ||
return result; | ||
}, | ||
{ | ||
onError: (_error, [_state, { multiplier }]) => { | ||
// Silly recovery: use the multiplier as the return value | ||
return multiplier; | ||
}, | ||
}, | ||
); | ||
|
||
const childSelectorCheck = new DebugInfoCheckUtil(childSelector); | ||
const parentSelectorCheck10 = new DebugInfoCheckUtil(parentSelector, { multiplier: 10 }); | ||
const parentSelectorCheck11 = new DebugInfoCheckUtil(parentSelector, { multiplier: 11 }); | ||
|
||
let state = { a: 3 }; | ||
|
||
expect(parentSelector(state, { multiplier: 10 })).toEqual(30); | ||
parentSelectorCheck10.expectInvoked('run'); | ||
childSelectorCheck.expectInvoked('run'); | ||
|
||
expect(parentSelector(state, { multiplier: 11 })).toEqual(11); | ||
parentSelectorCheck11.expectInvoked('aborted'); | ||
childSelectorCheck.expectInvoked('skipped'); | ||
|
||
// Since we overrode the value, everything is cached | ||
expect(parentSelector.hasCachedResult(state, { multiplier: 10 })).toEqual(true); | ||
expect(parentSelector.getCachedResult(state, { multiplier: 10 })).toEqual(30); | ||
expect(parentSelector.hasCachedResult(state, { multiplier: 11 })).toEqual(true); | ||
expect(parentSelector.getCachedResult(state, { multiplier: 11 })).toEqual(11); | ||
expect(childSelector.hasCachedResult(state)).toEqual(true); | ||
}); | ||
}); |