File size: 2,181 Bytes
c2b7eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { noop } from '@internal/listenerMiddleware/utils'
import { AClockworkOrange } from './fixtures/book'

describe('Entity utils', () => {
  describe(`selectIdValue()`, () => {
    const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(noop)

    beforeEach(() => {
      vi.resetModules() // this is important - it clears the cache
      vi.stubEnv('NODE_ENV', 'development')
    })

    afterEach(() => {
      vi.unstubAllEnvs()
      vi.clearAllMocks()
    })

    afterAll(() => {
      vi.restoreAllMocks()
    })

    it('should not warn when key does exist', async () => {
      const { selectIdValue } = await import('../utils')

      selectIdValue(AClockworkOrange, (book: any) => book.id)
      expect(consoleWarnSpy).not.toHaveBeenCalled()
    })

    it('should warn when key does not exist in dev mode', async () => {
      const { selectIdValue } = await import('../utils')

      expect(process.env.NODE_ENV).toBe('development')

      selectIdValue(AClockworkOrange, (book: any) => book.foo)

      expect(consoleWarnSpy).toHaveBeenCalledOnce()
    })

    it('should warn when key is undefined in dev mode', async () => {
      const { selectIdValue } = await import('../utils')

      expect(process.env.NODE_ENV).toBe('development')

      const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
      selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)

      expect(consoleWarnSpy).toHaveBeenCalledOnce()
    })

    it('should not warn when key does not exist in prod mode', async () => {
      vi.stubEnv('NODE_ENV', 'production')

      const { selectIdValue } = await import('../utils')

      selectIdValue(AClockworkOrange, (book: any) => book.foo)

      expect(consoleWarnSpy).not.toHaveBeenCalled()
    })

    it('should not warn when key is undefined in prod mode', async () => {
      vi.stubEnv('NODE_ENV', 'production')

      const { selectIdValue } = await import('../utils')

      const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
      selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)

      expect(consoleWarnSpy).not.toHaveBeenCalled()
    })
  })
})