From afc8bd34b91a0f7e625ff31e19083b12f4412fd8 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 14 May 2023 22:38:30 +0100 Subject: [PATCH] use an enum, and log arguments and inputs --- src/index.ts | 11 ++++++++--- test/inputStabilityCheck.spec.ts | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9ca945865..66067705c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,7 +38,7 @@ export { defaultMemoize, defaultEqualityCheck } export type { DefaultMemoizeOptions } -type StabilityCheck = boolean | 'once' +type StabilityCheck = 'always' | 'once' | 'never' let globalStabilityCheck: StabilityCheck = 'once' @@ -159,7 +159,7 @@ export function createSelectorCreator< if ( process.env.NODE_ENV !== 'production' && - (finalStabilityCheck === true || + (finalStabilityCheck === 'always' || (finalStabilityCheck === 'once' && firstRun)) ) { const paramsCopy = [] @@ -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 + } ) } diff --git a/test/inputStabilityCheck.spec.ts b/test/inputStabilityCheck.spec.ts index 5c3e005c4..be17bc427 100644 --- a/test/inputStabilityCheck.spec.ts +++ b/test/inputStabilityCheck.spec.ts @@ -32,12 +32,22 @@ 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) @@ -45,12 +55,12 @@ describe('inputStabilityCheck', () => { 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)