File size: 2,759 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import path from 'path'
import { createNext, FileRef, NextInstance } from 'e2e-utils'
import {
  fetchViaHTTP,
  findPort,
  initNextServerScript,
  killApp,
  retry,
} from 'next-test-utils'

describe('global-default-cache-handler', () => {
  let appPort: number
  let server: any
  let output = ''
  let next: NextInstance

  beforeAll(async () => {
    next = await createNext({
      files: new FileRef(__dirname),
      skipStart: true,
    })
    await next.build()

    const standaloneServer = '.next/standalone/server.js'
    await next.patchFile(
      standaloneServer,
      `
      globalThis[Symbol.for('@next/cache-handlers')] = {
        DefaultCache: {
          get(cacheKey, softTags) {
            console.log('symbol get', cacheKey, softTags)
          },
          
          set(cacheKey, entry) {
            console.log('symbol set', cacheKey)
          },
        
          refreshTags() {
            console.log('symbol refreshTags')
          },

          getExpiration(...tags) {
            console.log('symbol getExpiration', tags)
          },
        
          expireTags(...tags) {
            console.log('symbol expireTags', tags)
          }
        }
      }
      ${await next.readFile(standaloneServer)}`
    )

    appPort = await findPort()

    require('console').error(
      'starting standalone mode',
      path.join(next.testDir, standaloneServer)
    )

    server = await initNextServerScript(
      path.join(next.testDir, standaloneServer),
      /- Local:/,
      {
        ...process.env,
        PORT: `${appPort}`,
      },
      undefined,
      {
        cwd: next.testDir,
        shouldRejectOnError: true,
        onStdout(data) {
          output += data
        },
        onStderr(data) {
          output += data
        },
      }
    )
  })
  afterAll(async () => {
    await next.destroy()
    await killApp(server)
  })

  it('should use global symbol for default cache handler', async () => {
    const res = await fetchViaHTTP(appPort, '/')
    expect(res.status).toBe(200)

    await retry(() => {
      expect(output).toContain('symbol get')
      expect(output).toContain('symbol set')
    })
  })

  it('should call expireTags on global default cache handler', async () => {
    const res = await fetchViaHTTP(appPort, '/revalidate-tag', { tag: 'tag1' })
    expect(res.status).toBe(200)

    await retry(() => {
      expect(output).toContain('symbol expireTags')
      expect(output).toContain('tag1')
    })
  })

  it('should call refreshTags on global default cache handler', async () => {
    const res = await fetchViaHTTP(appPort, '/', {})
    expect(res.status).toBe(200)

    await retry(async () => {
      expect(output).toContain('symbol refreshTags')
    })
  })
})