File size: 10,427 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | import { shallowEqual } from 'react-redux'
import {
createSelector,
createSelectorCreator,
referenceEqualityCheck,
weakMapMemoize
} from 'reselect'
import type { RootState } from './testUtils'
import { localTest, setEnvToProd, toggleCompleted } from './testUtils'
// Construct 1E6 states for perf test outside of the perf test so as to not change the execute time of the test function
const numOfStates = 1_000_000
interface StateA {
a: number
}
interface StateAB {
a: number
b: number
}
interface StateSub {
sub: {
a: number
}
}
const states: StateAB[] = []
for (let i = 0; i < numOfStates; i++) {
states.push({ a: 1, b: 2 })
}
describe('Basic selector behavior with weakMapMemoize', () => {
const createSelector = createSelectorCreator(weakMapMemoize)
test('basic selector', () => {
// console.log('Selector test')
const selector = createSelector(
(state: StateA) => state.a,
a => a,
{ devModeChecks: { identityFunctionCheck: 'never' } }
)
const firstState = { a: 1 }
const firstStateNewPointer = { a: 1 }
const secondState = { a: 2 }
expect(selector(firstState)).toBe(1)
expect(selector(firstState)).toBe(1)
expect(selector.recomputations()).toBe(1)
expect(selector(firstStateNewPointer)).toBe(1)
expect(selector.recomputations()).toBe(1)
expect(selector(secondState)).toBe(2)
expect(selector.recomputations()).toBe(2)
})
test("don't pass extra parameters to inputSelector when only called with the state", () => {
const selector = createSelector(
(...params: any[]) => params.length,
a => a,
{ devModeChecks: { identityFunctionCheck: 'never' } }
)
expect(selector({})).toBe(1)
})
test('basic selector multiple keys', () => {
const selector = createSelector(
(state: StateAB) => state.a,
(state: StateAB) => state.b,
(a, b) => a + b
)
const state1 = { a: 1, b: 2 }
expect(selector(state1)).toBe(3)
expect(selector(state1)).toBe(3)
expect(selector.recomputations()).toBe(1)
const state2 = { a: 3, b: 2 }
expect(selector(state2)).toBe(5)
expect(selector(state2)).toBe(5)
expect(selector.recomputations()).toBe(2)
})
test('basic selector invalid input selector', () => {
expect(() =>
createSelector(
// @ts-ignore
(state: StateAB) => state.a,
function input2(state: StateAB) {
return state.b
},
'not a function',
(a: any, b: any) => a + b
)
).toThrow(
'createSelector expects all input-selectors to be functions, but received the following types: [function unnamed(), function input2(), string]'
)
expect(() =>
// @ts-ignore
createSelector((state: StateAB) => state.a, 'not a function')
).toThrow(
'createSelector expects an output function after the inputs, but received: [string]'
)
})
test('memoized composite arguments', () => {
const selector = createSelector(
(state: StateSub) => state.sub,
sub => sub.a
)
const state1 = { sub: { a: 1 } }
expect(selector(state1)).toEqual(1)
expect(selector(state1)).toEqual(1)
expect(selector.recomputations()).toBe(1)
const state2 = { sub: { a: 2 } }
expect(selector(state2)).toEqual(2)
expect(selector.recomputations()).toBe(2)
})
test('first argument can be an array', () => {
const selector = createSelector(
[state => state.a, state => state.b],
(a, b) => {
return a + b
}
)
expect(selector({ a: 1, b: 2 })).toBe(3)
expect(selector({ a: 1, b: 2 })).toBe(3)
expect(selector.recomputations()).toBe(1)
expect(selector({ a: 3, b: 2 })).toBe(5)
expect(selector.recomputations()).toBe(2)
})
test('can accept props', () => {
let called = 0
const selector = createSelector(
(state: StateAB) => state.a,
(state: StateAB) => state.b,
(state: StateAB, props: { c: number }) => props.c,
(a, b, c) => {
called++
return a + b + c
}
)
expect(selector({ a: 1, b: 2 }, { c: 100 })).toBe(103)
})
test('recomputes result after exception', () => {
let called = 0
const selector = createSelector(
(state: StateA) => state.a,
() => {
called++
throw Error('test error')
}
)
expect(() => selector({ a: 1 })).toThrow('test error')
expect(() => selector({ a: 1 })).toThrow('test error')
expect(called).toBe(2)
})
test('memoizes previous result before exception', () => {
let called = 0
const selector = createSelector(
(state: StateA) => state.a,
a => {
called++
if (a > 1) throw Error('test error')
return a
},
{ devModeChecks: { identityFunctionCheck: 'never' } }
)
const state1 = { a: 1 }
const state2 = { a: 2 }
expect(selector(state1)).toBe(1)
expect(() => selector(state2)).toThrow('test error')
expect(selector(state1)).toBe(1)
expect(called).toBe(2)
})
})
const isCoverage = process.env.COVERAGE
// don't run performance tests for coverage
describe.skipIf(isCoverage)('weakmapMemoize performance tests', () => {
beforeEach(setEnvToProd)
test('basic selector cache hit performance', () => {
const selector = createSelector(
(state: StateAB) => state.a,
(state: StateAB) => state.b,
(a, b) => a + b,
{ devModeChecks: { identityFunctionCheck: 'never' } }
)
const state1 = { a: 1, b: 2 }
const start = performance.now()
for (let i = 0; i < 1_000_000; i++) {
selector(state1)
}
const totalTime = performance.now() - start
expect(selector(state1)).toBe(3)
expect(selector.recomputations()).toBe(1)
// Expected a million calls to a selector with the same arguments to take less than 1 second
expect(totalTime).toBeLessThan(2000)
})
test('basic selector cache hit performance for state changes but shallowly equal selector args', () => {
const selector = createSelector(
(state: StateAB) => state.a,
(state: StateAB) => state.b,
(a, b) => a + b,
{
devModeChecks: {
identityFunctionCheck: 'never',
inputStabilityCheck: 'never'
}
}
)
const start = performance.now()
for (let i = 0; i < 1_000_000; i++) {
selector(states[i])
}
const totalTime = performance.now() - start
expect(selector(states[0])).toBe(3)
expect(selector.recomputations()).toBe(1)
// Expected a million calls to a selector with the same arguments to take less than 1 second
expect(totalTime).toBeLessThan(2000)
})
})
describe('weakMapMemoize integration with resultEqualityCheck', () => {
const createAppSelector = createSelector.withTypes<RootState>()
const resultEqualityCheck = vi
.fn(shallowEqual)
.mockName('resultEqualityCheck')
afterEach(() => {
resultEqualityCheck.mockClear()
})
localTest(
'resultEqualityCheck works correctly when set to shallowEqual',
({ store }) => {
const selectTodoIds = weakMapMemoize(
(state: RootState) => state.todos.map(({ id }) => id),
{ resultEqualityCheck }
)
const firstResult = selectTodoIds(store.getState())
store.dispatch(toggleCompleted(0))
const secondResult = selectTodoIds(store.getState())
expect(firstResult).toBe(secondResult)
expect(selectTodoIds.resultsCount()).toBe(1)
}
)
localTest(
'resultEqualityCheck is not called on the first output selector call',
({ store }) => {
const selectTodoIds = createAppSelector(
[state => state.todos],
todos => todos.map(({ id }) => id),
{
memoizeOptions: { resultEqualityCheck },
devModeChecks: { inputStabilityCheck: 'once' }
}
)
expect(selectTodoIds(store.getState())).to.be.an('array').that.is.not
.empty
expect(resultEqualityCheck).not.toHaveBeenCalled()
store.dispatch(toggleCompleted(0))
expect(selectTodoIds.lastResult()).toBe(selectTodoIds(store.getState()))
expect(resultEqualityCheck).toHaveBeenCalledOnce()
expect(selectTodoIds.memoizedResultFunc.resultsCount()).toBe(1)
expect(selectTodoIds.recomputations()).toBe(2)
expect(selectTodoIds.resultsCount()).toBe(2)
expect(selectTodoIds.dependencyRecomputations()).toBe(2)
store.dispatch(toggleCompleted(0))
expect(selectTodoIds.lastResult()).toBe(selectTodoIds(store.getState()))
expect(resultEqualityCheck).toHaveBeenCalledTimes(2)
expect(selectTodoIds.memoizedResultFunc.resultsCount()).toBe(1)
expect(selectTodoIds.recomputations()).toBe(3)
expect(selectTodoIds.resultsCount()).toBe(3)
expect(selectTodoIds.dependencyRecomputations()).toBe(3)
}
)
localTest(
'weakMapMemoize with resultEqualityCheck set to referenceEqualityCheck works the same as weakMapMemoize without resultEqualityCheck',
({ store }) => {
const resultEqualityCheck = vi
.fn(referenceEqualityCheck)
.mockName('resultEqualityCheck')
const selectTodoIdsWithResultEqualityCheck = weakMapMemoize(
(state: RootState) => state.todos.map(({ id }) => id),
{ resultEqualityCheck }
)
const firstResultWithResultEqualityCheck =
selectTodoIdsWithResultEqualityCheck(store.getState())
expect(resultEqualityCheck).not.toHaveBeenCalled()
store.dispatch(toggleCompleted(0))
const secondResultWithResultEqualityCheck =
selectTodoIdsWithResultEqualityCheck(store.getState())
expect(firstResultWithResultEqualityCheck).not.toBe(
secondResultWithResultEqualityCheck
)
expect(firstResultWithResultEqualityCheck).toStrictEqual(
secondResultWithResultEqualityCheck
)
expect(selectTodoIdsWithResultEqualityCheck.resultsCount()).toBe(2)
const selectTodoIds = weakMapMemoize((state: RootState) =>
state.todos.map(({ id }) => id)
)
const firstResult = selectTodoIds(store.getState())
store.dispatch(toggleCompleted(0))
const secondResult = selectTodoIds(store.getState())
expect(firstResult).not.toBe(secondResult)
expect(firstResult).toStrictEqual(secondResult)
expect(selectTodoIds.resultsCount()).toBe(2)
resultEqualityCheck.mockClear()
}
)
})
|