File size: 5,315 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import {
createSelector,
createSelectorCreator,
createStructuredSelector,
lruMemoize
} from 'reselect'
import type { LocalTestContext, RootState } from './testUtils'
import { setupStore } from './testUtils'
interface StateAB {
a: number
b: number
}
describe(createStructuredSelector, () => {
test('structured selector', () => {
const selector = createStructuredSelector({
x: (state: StateAB) => state.a,
y: (state: StateAB) => state.b
})
const firstResult = selector({ a: 1, b: 2 })
expect(firstResult).toEqual({ x: 1, y: 2 })
expect(selector({ a: 1, b: 2 })).toBe(firstResult)
const secondResult = selector({ a: 2, b: 2 })
expect(secondResult).toEqual({ x: 2, y: 2 })
expect(selector({ a: 2, b: 2 })).toBe(secondResult)
})
test('structured selector with invalid arguments', () => {
expect(() =>
createStructuredSelector(
// @ts-expect-error
(state: StateAB) => state.a,
(state: StateAB) => state.b
)
).toThrow(/expects first argument to be an object.*function/)
expect(() =>
createStructuredSelector({
a: state => state.b,
// @ts-expect-error
c: 'd'
})
).toThrow(
'createSelector expects all input-selectors to be functions, but received the following types: [function a(), string]'
)
})
test('structured selector with custom selector creator', () => {
const customSelectorCreator = createSelectorCreator(
lruMemoize,
(a, b) => a === b
)
const selector = createStructuredSelector(
{
x: (state: StateAB) => state.a,
y: (state: StateAB) => state.b
},
customSelectorCreator
)
const firstResult = selector({ a: 1, b: 2 })
expect(firstResult).toEqual({ x: 1, y: 2 })
expect(selector({ a: 1, b: 2 })).toBe(firstResult)
expect(selector({ a: 2, b: 2 })).toEqual({ x: 2, y: 2 })
})
})
describe<LocalTestContext>('structured selector created with createStructuredSelector', localTest => {
beforeEach<LocalTestContext>(context => {
const store = setupStore()
context.store = store
context.state = store.getState()
})
localTest(
'structured selector created with createStructuredSelector and createSelector are the same',
({ state }) => {
const structuredSelector = createStructuredSelector(
{
allTodos: (state: RootState) => state.todos,
allAlerts: (state: RootState) => state.alerts,
selectedTodo: (state: RootState, id: number) => state.todos[id]
},
createSelector
)
const selector = createSelector(
[
(state: RootState) => state.todos,
(state: RootState) => state.alerts,
(state: RootState, id: number) => state.todos[id]
],
(allTodos, allAlerts, selectedTodo) => {
return {
allTodos,
allAlerts,
selectedTodo
}
}
)
expect(selector(state, 1).selectedTodo.id).toBe(
structuredSelector(state, 1).selectedTodo.id
)
expect(structuredSelector.dependencies)
.to.be.an('array')
.with.lengthOf(selector.dependencies.length)
expect(
structuredSelector.resultFunc(state.todos, state.alerts, state.todos[0])
).toStrictEqual(
selector.resultFunc(state.todos, state.alerts, state.todos[0])
)
expect(
structuredSelector.memoizedResultFunc(
state.todos,
state.alerts,
state.todos[0]
)
).toStrictEqual(
selector.memoizedResultFunc(state.todos, state.alerts, state.todos[0])
)
expect(structuredSelector.argsMemoize).toBe(selector.argsMemoize)
expect(structuredSelector.memoize).toBe(selector.memoize)
expect(structuredSelector.recomputations()).toBe(
selector.recomputations()
)
expect(structuredSelector.lastResult()).toStrictEqual(
selector.lastResult()
)
expect(Object.keys(structuredSelector)).toStrictEqual(
Object.keys(selector)
)
}
)
localTest(
'structured selector invalid args can throw runtime errors',
({ state }) => {
const structuredSelector = createStructuredSelector(
{
allTodos: (state: RootState) => state.todos,
allAlerts: (state: RootState) => state.alerts,
selectedTodo: (
state: RootState,
id: number,
field: keyof RootState['todos'][number]
) => state.todos[id][field]
},
createSelector
)
const selector = createSelector(
[
(state: RootState) => state.todos,
(state: RootState) => state.alerts,
(
state: RootState,
id: number,
field: keyof RootState['todos'][number]
) => state.todos[id][field]
],
(allTodos, allAlerts, selectedTodo) => {
return {
allTodos,
allAlerts,
selectedTodo
}
}
)
// These two cases are the same.
// @ts-expect-error
expect(() => structuredSelector(state)).toThrowError(TypeError)
// @ts-expect-error
expect(() => selector(state)).toThrowError(TypeError)
}
)
})
|