Skip to content

Commit

Permalink
test: add collision test
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk committed Mar 22, 2024
1 parent 877d093 commit 8ba87ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import type {
DevModeChecksExecutionInfo
} from './types'

class NotFound {}

export const NOT_FOUND = new NotFound()
export const NOT_FOUND = Symbol('NOT_FOUND')
export type NOT_FOUND_TYPE = typeof NOT_FOUND

/**
Expand Down
32 changes: 32 additions & 0 deletions test/lruMemoize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,38 @@ describe(lruMemoize, () => {
expect(selector.resultFunc.clearCache).toBeUndefined()
})

test('cache miss identifier does not collide with state values', () => {
const state = ['NOT_FOUND', 'FOUND']

type State = typeof state

const createSelector = createSelectorCreator({
memoize: lruMemoize,
argsMemoize: lruMemoize
}).withTypes<State>()

const selector = createSelector(
[(state, id: number) => state[id]],
state => state,
{
argsMemoizeOptions: { maxSize: 10 },
memoizeOptions: { maxSize: 10 }
}
)

const firstResult = selector(state, 0)

expect(selector(state, 1)).toBe(selector(state, 1))

const secondResult = selector(state, 0)

expect(secondResult).toBe('NOT_FOUND')

expect(firstResult).toBe(secondResult)

expect(selector.recomputations()).toBe(2)
})

localTest(
'maxSize should default to 1 when set to a number that is less than 1',
({ state, store }) => {
Expand Down

0 comments on commit 8ba87ff

Please sign in to comment.