Skip to content

Commit

Permalink
use an enum, and log arguments and inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed May 14, 2023
1 parent a163264 commit afc8bd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export { defaultMemoize, defaultEqualityCheck }

export type { DefaultMemoizeOptions }

type StabilityCheck = boolean | 'once'
type StabilityCheck = 'always' | 'once' | 'never'

let globalStabilityCheck: StabilityCheck = 'once'

Expand Down Expand Up @@ -159,7 +159,7 @@ export function createSelectorCreator<

if (
process.env.NODE_ENV !== 'production' &&
(finalStabilityCheck === true ||
(finalStabilityCheck === 'always' ||
(finalStabilityCheck === 'once' && firstRun))
) {
const paramsCopy = []
Expand All @@ -180,7 +180,12 @@ export function createSelectorCreator<
'An input selector returned a different result when passed same arguments.' +
'\nThis means your output selector will likely run more frequently than intended.' +
'\nAvoid returning a new reference inside your input selector, e.g.' +
'\n`createSelector([(arg1, arg2) => ({ arg1, arg2 })],(arg1, arg2) => {})`'
'\n`createSelector([(arg1, arg2) => ({ arg1, arg2 })],(arg1, arg2) => {})`',
{
arguments,
firstInputs: params,
secondInputs: paramsCopy
}
)
}

Expand Down
18 changes: 14 additions & 4 deletions test/inputStabilityCheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,35 @@ describe('inputStabilityCheck', () => {
expect(unstableInput).toHaveBeenCalledTimes(2)

expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('An input selector returned a different result')
expect.stringContaining('An input selector returned a different result'),
expect.objectContaining({
// IArguments isn't an array :(
arguments: expect.anything(),
firstInputs: expect.arrayContaining([
expect.objectContaining({ a: 1, b: 2 })
]),
secondInputs: expect.arrayContaining([
expect.objectContaining({ a: 1, b: 2 })
])
})
)
})

it('disables check if global setting is changed', () => {
setInputStabilityCheckEnabled(false)
setInputStabilityCheckEnabled('never')

expect(addNums(1, 2)).toBe(3)

expect(unstableInput).toHaveBeenCalledTimes(1)

expect(consoleSpy).not.toHaveBeenCalled()

setInputStabilityCheckEnabled(true)
setInputStabilityCheckEnabled('once')
})

it('disables check if specified in the selector options', () => {
const addNums = createSelector([unstableInput], ({ a, b }) => a + b, {
inputStabilityCheck: false
inputStabilityCheck: 'never'
})

expect(addNums(1, 2)).toBe(3)
Expand Down

0 comments on commit afc8bd3

Please sign in to comment.