| 1 |
import { noop } from '@internal/listenerMiddleware/utils' |
| 2 |
import { AClockworkOrange } from './fixtures/book' |
| 3 |
|
| 4 |
describe('Entity utils', () => { |
| 5 |
describe(`selectIdValue()`, () => { |
| 6 |
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(noop) |
| 7 |
|
| 8 |
beforeEach(() => { |
| 9 |
vi.resetModules() // this is important - it clears the cache |
| 10 |
vi.stubEnv('NODE_ENV', 'development') |
| 11 |
}) |
| 12 |
|
| 13 |
afterEach(() => { |
| 14 |
vi.unstubAllEnvs() |
| 15 |
vi.clearAllMocks() |
| 16 |
}) |
| 17 |
|
| 18 |
afterAll(() => { |
| 19 |
vi.restoreAllMocks() |
| 20 |
}) |
| 21 |
|
| 22 |
it('should not warn when key does exist', async () => { |
| 23 |
const { selectIdValue } = await import('../utils') |
| 24 |
|
| 25 |
selectIdValue(AClockworkOrange, (book: any) => book.id) |
| 26 |
expect(consoleWarnSpy).not.toHaveBeenCalled() |
| 27 |
}) |
| 28 |
|
| 29 |
it('should warn when key does not exist in dev mode', async () => { |
| 30 |
const { selectIdValue } = await import('../utils') |
| 31 |
|
| 32 |
expect(process.env.NODE_ENV).toBe('development') |
| 33 |
|
| 34 |
selectIdValue(AClockworkOrange, (book: any) => book.foo) |
| 35 |
|
| 36 |
expect(consoleWarnSpy).toHaveBeenCalledOnce() |
| 37 |
}) |
| 38 |
|
| 39 |
it('should warn when key is undefined in dev mode', async () => { |
| 40 |
const { selectIdValue } = await import('../utils') |
| 41 |
|
| 42 |
expect(process.env.NODE_ENV).toBe('development') |
| 43 |
|
| 44 |
const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined } |
| 45 |
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id) |
| 46 |
|
| 47 |
expect(consoleWarnSpy).toHaveBeenCalledOnce() |
| 48 |
}) |
| 49 |
|
| 50 |
it('should not warn when key does not exist in prod mode', async () => { |
| 51 |
vi.stubEnv('NODE_ENV', 'production') |
| 52 |
|
| 53 |
const { selectIdValue } = await import('../utils') |
| 54 |
|
| 55 |
selectIdValue(AClockworkOrange, (book: any) => book.foo) |
| 56 |
|
| 57 |
expect(consoleWarnSpy).not.toHaveBeenCalled() |
| 58 |
}) |
| 59 |
|
| 60 |
it('should not warn when key is undefined in prod mode', async () => { |
| 61 |
vi.stubEnv('NODE_ENV', 'production') |
| 62 |
|
| 63 |
const { selectIdValue } = await import('../utils') |
| 64 |
|
| 65 |
const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined } |
| 66 |
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id) |
| 67 |
|
| 68 |
expect(consoleWarnSpy).not.toHaveBeenCalled() |
| 69 |
}) |
| 70 |
}) |
| 71 |
}) |