File size: 1,782 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import { createSelector, lruMemoize } from 'reselect'
import type { StateA, StateAB } from 'testTypes'
describe('createSelector exposed utils', () => {
test('resetRecomputations', () => {
const selector = createSelector(
(state: StateA) => state.a,
a => a,
{
memoize: lruMemoize,
argsMemoize: lruMemoize,
devModeChecks: { identityFunctionCheck: 'never' }
}
)
expect(selector({ a: 1 })).toBe(1)
expect(selector({ a: 1 })).toBe(1)
expect(selector.recomputations()).toBe(1)
expect(selector({ a: 2 })).toBe(2)
expect(selector.recomputations()).toBe(2)
selector.resetRecomputations()
expect(selector.recomputations()).toBe(0)
expect(selector({ a: 1 })).toBe(1)
expect(selector({ a: 1 })).toBe(1)
expect(selector.recomputations()).toBe(1)
expect(selector({ a: 2 })).toBe(2)
expect(selector.recomputations()).toBe(2)
})
test('export last function as resultFunc', () => {
const lastFunction = () => {}
const selector = createSelector((state: StateA) => state.a, lastFunction)
expect(selector.resultFunc).toBe(lastFunction)
})
test('export dependencies as dependencies', () => {
const dependency1 = (state: StateA) => {
state.a
}
const dependency2 = (state: StateA) => {
state.a
}
const selector = createSelector(dependency1, dependency2, () => {})
expect(selector.dependencies).toEqual([dependency1, dependency2])
})
test('export lastResult function', () => {
const selector = createSelector(
(state: StateAB) => state.a,
(state: StateAB) => state.b,
(a, b) => a + b
)
const result = selector({ a: 1, b: 2 })
expect(result).toBe(3)
expect(selector.lastResult()).toBe(3)
})
})
|