-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial implementation for
noopCheck
- Loading branch information
1 parent
a39a97a
commit 67ea47b
Showing
10 changed files
with
358 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { createSelector, setGlobalNoopCheck } from 'reselect' | ||
import type { LocalTestContext, RootState } from './testUtils' | ||
import { localTest } from './testUtils' | ||
|
||
describe<LocalTestContext>('noopCheck', () => { | ||
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
const identityFunction = vi.fn(<T>(state: T) => state) | ||
const badSelector = createSelector( | ||
[(state: RootState) => state], | ||
identityFunction | ||
) | ||
|
||
afterEach(() => { | ||
consoleSpy.mockClear() | ||
identityFunction.mockClear() | ||
badSelector.clearCache() | ||
badSelector.memoizedResultFunc.clearCache() | ||
}) | ||
afterAll(() => { | ||
consoleSpy.mockRestore() | ||
}) | ||
localTest( | ||
'calls the result function twice, and warns to console if result is the same as argument', | ||
({ state }) => { | ||
const goodSelector = createSelector( | ||
[(state: RootState) => state], | ||
state => state.todos | ||
) | ||
|
||
expect(goodSelector(state)).toBe(state.todos) | ||
|
||
expect(consoleSpy).not.toHaveBeenCalled() | ||
|
||
expect(badSelector(state)).toBe(state) | ||
|
||
expect(identityFunction).toHaveBeenCalledTimes(2) | ||
|
||
expect(consoleSpy).toHaveBeenCalled() | ||
} | ||
) | ||
|
||
localTest('disables check if global setting is set to never', ({ state }) => { | ||
setGlobalNoopCheck('never') | ||
|
||
expect(badSelector(state)).toBe(state) | ||
|
||
expect(identityFunction).toHaveBeenCalledTimes(1) | ||
|
||
expect(consoleSpy).not.toHaveBeenCalled() | ||
|
||
setGlobalNoopCheck('once') | ||
}) | ||
|
||
localTest( | ||
'disables check if specified in the selector options', | ||
({ state }) => { | ||
const badSelector = createSelector( | ||
[(state: RootState) => state], | ||
identityFunction, | ||
{ noopCheck: 'never' } | ||
) | ||
|
||
expect(badSelector(state)).toBe(state) | ||
|
||
expect(identityFunction).toHaveBeenCalledTimes(1) | ||
|
||
expect(consoleSpy).not.toHaveBeenCalled() | ||
} | ||
) | ||
|
||
localTest('disables check in production', ({ state }) => { | ||
const originalEnv = process.env.NODE_ENV | ||
process.env.NODE_ENV = 'production' | ||
|
||
expect(badSelector(state)).toBe(state) | ||
|
||
expect(identityFunction).toHaveBeenCalledTimes(1) | ||
|
||
expect(consoleSpy).not.toHaveBeenCalled() | ||
|
||
process.env.NODE_ENV = originalEnv | ||
}) | ||
|
||
localTest('allows running the check only once', ({ state }) => { | ||
const badSelector = createSelector( | ||
[(state: RootState) => state], | ||
identityFunction, | ||
{ noopCheck: 'once' } | ||
) | ||
expect(badSelector(state)).toBe(state) | ||
|
||
expect(identityFunction).toHaveBeenCalledTimes(2) | ||
|
||
expect(consoleSpy).toHaveBeenCalledOnce() | ||
|
||
const newState = { ...state } | ||
|
||
expect(badSelector(newState)).toBe(newState) | ||
|
||
expect(identityFunction).toHaveBeenCalledTimes(3) | ||
|
||
expect(consoleSpy).toHaveBeenCalledOnce() | ||
}) | ||
|
||
localTest('uses the memoize provided', ({ state }) => { | ||
// console.log(setupStore) | ||
// const store = setupStore() | ||
// const state = store.getState() | ||
const badSelector = createSelector( | ||
[(state: RootState) => state.todos], | ||
identityFunction | ||
) | ||
expect(badSelector(state)).toBe(state.todos) | ||
|
||
expect(identityFunction).toHaveBeenCalledTimes(2) | ||
|
||
expect(consoleSpy).toHaveBeenCalledTimes(1) | ||
|
||
const newState = { ...state } | ||
|
||
expect(badSelector({ ...state })).not.toBe(state) | ||
|
||
// expect(identityFunction).toHaveBeenCalledTimes(3) | ||
|
||
expect(consoleSpy).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.