Skip to content

Commit 8fb0975

Browse files
committed
Fix some issues that were left over from the switch to weakMapMemoize
- Looks like some of the code in `computationComparisons.spec.tsx` wasn't properly updated after we switched the default memoization function from `lruMemoize` to `weakMapMemoize`. These have been updated to their correct implementations. - Correct residual naming inconsistencies from the previous `defaultMemoize` to `lruMemoize` renaming. All references to `default` have been updated to `lru`.
1 parent 62feb04 commit 8fb0975

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

test/computationComparisons.spec.tsx

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import rtl from '@testing-library/react'
6-
import React, { useLayoutEffect, useMemo } from 'react'
6+
import { memo, useLayoutEffect, useMemo } from 'react'
77
import type { TypedUseSelectorHook } from 'react-redux'
88
import { Provider, shallowEqual, useSelector } from 'react-redux'
99
import type { OutputSelector } from 'reselect'
@@ -14,7 +14,7 @@ import {
1414
weakMapMemoize
1515
} from 'reselect'
1616
import type { RootState, Todo } from './testUtils'
17-
import { addTodo, setupStore, toggleCompleted } from './testUtils'
17+
import { setupStore, toggleCompleted } from './testUtils'
1818

1919
describe('Computations and re-rendering with React components', () => {
2020
let store: ReturnType<typeof setupStore>
@@ -27,37 +27,40 @@ describe('Computations and re-rendering with React components', () => {
2727
})
2828

2929
type SelectTodoIds = OutputSelector<
30-
[(state: RootState) => RootState['todos']],
30+
[(state: RootState) => Todo[]],
3131
number[],
3232
typeof lruMemoize | typeof weakMapMemoize,
3333
typeof lruMemoize | typeof weakMapMemoize
3434
>
3535

3636
type SelectTodoById = OutputSelector<
37-
[
38-
(state: RootState) => RootState['todos'],
39-
(state: RootState, id: number) => number
40-
],
37+
[(state: RootState) => Todo[], (state: RootState, id: number) => number],
4138
readonly [todo: Todo | undefined],
4239
typeof lruMemoize | typeof weakMapMemoize,
4340
typeof lruMemoize | typeof weakMapMemoize
4441
>
4542

4643
const selectTodos = (state: RootState) => state.todos
47-
const mapTodoIds = (todos: RootState['todos']) => todos.map(({ id }) => id)
48-
const selectTodoId = (todos: RootState, id: number) => id
49-
const mapTodoById = (todos: RootState['todos'], id: number) => {
44+
const mapTodoIds = (todos: Todo[]) => todos.map(({ id }) => id)
45+
const selectTodoId = (state: RootState, id: number) => id
46+
const mapTodoById = (todos: Todo[], id: number) => {
5047
// Intentionally return this wrapped in an array to force a new reference each time
5148
return [todos.find(todo => todo.id === id)] as const
5249
}
5350

54-
const selectTodoIdsDefault = createSelector([selectTodos], mapTodoIds)
55-
console.log(`selectTodoIdsDefault name: ${selectTodoIdsDefault.name}`)
51+
const selectTodoIdsLru = createSelector([selectTodos], mapTodoIds, {
52+
argsMemoize: lruMemoize,
53+
memoize: lruMemoize
54+
})
5655

57-
const selectTodoIdsResultEquality = createSelector(
56+
const selectTodoIdsLruResultEquality = createSelector(
5857
[selectTodos],
5958
mapTodoIds,
60-
{ memoizeOptions: { resultEqualityCheck: shallowEqual } }
59+
{
60+
memoizeOptions: { resultEqualityCheck: shallowEqual },
61+
memoize: lruMemoize,
62+
argsMemoize: lruMemoize
63+
}
6164
)
6265

6366
const selectTodoIdsWeakMap = createSelector([selectTodos], mapTodoIds, {
@@ -75,16 +78,18 @@ describe('Computations and re-rendering with React components', () => {
7578
}
7679
)
7780

78-
const selectTodoByIdDefault = createSelector(
81+
const selectTodoByIdLru = createSelector(
7982
[selectTodos, selectTodoId],
80-
mapTodoById
83+
mapTodoById,
84+
{ memoize: lruMemoize, argsMemoize: lruMemoize }
8185
)
8286

83-
const selectTodoByIdResultEquality = createSelector(
87+
const selectTodoByIdLruResultEquality = createSelector(
8488
[selectTodos, selectTodoId],
8589
mapTodoById,
8690
{
8791
memoize: lruMemoize,
92+
argsMemoize: lruMemoize,
8893
memoizeOptions: { resultEqualityCheck: shallowEqual, maxSize: 500 }
8994
}
9095
)
@@ -101,7 +106,7 @@ describe('Computations and re-rendering with React components', () => {
101106
let listRenders = 0
102107
let listItemMounts = 0
103108

104-
const TodoListItem = React.memo(function TodoListItem({
109+
const TodoListItem = memo(function TodoListItem({
105110
id,
106111
selectTodoById
107112
}: {
@@ -150,11 +155,11 @@ describe('Computations and re-rendering with React components', () => {
150155
}
151156

152157
const testCases: [string, SelectTodoIds, SelectTodoById][] = [
153-
['default', selectTodoIdsDefault, selectTodoByIdDefault],
158+
['lru', selectTodoIdsLru, selectTodoByIdLru],
154159
[
155-
'resultEquality',
156-
selectTodoIdsResultEquality,
157-
selectTodoByIdResultEquality
160+
'lruResultEquality',
161+
selectTodoIdsLruResultEquality,
162+
selectTodoByIdLruResultEquality
158163
],
159164
['weakMap', selectTodoIdsWeakMap, selectTodoByIdWeakMap],
160165

0 commit comments

Comments
 (0)