File size: 2,682 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
import { NextDataPathnameNormalizer } from './next-data'

describe('NextDataPathnameNormalizer', () => {
  describe('constructor', () => {
    it('should error when no buildID is provided', () => {
      expect(() => {
        new NextDataPathnameNormalizer('')
      }).toThrowErrorMatchingInlineSnapshot(`"Invariant: buildID is required"`)
    })
  })

  describe('match', () => {
    it('should return false if the pathname does not start with the prefix', () => {
      const normalizer = new NextDataPathnameNormalizer('build-id')
      const pathnames = ['/foo', '/foo/bar', '/fooo/bar']
      for (const pathname of pathnames) {
        expect(normalizer.match(pathname)).toBe(false)
      }
    })

    it('should return false if the pathname only ends with `.json`', () => {
      const normalizer = new NextDataPathnameNormalizer('build-id')
      const pathnames = ['/foo.json', '/foo/bar.json', '/fooo/bar.json']
      for (const pathname of pathnames) {
        expect(normalizer.match(pathname)).toBe(false)
      }
    })

    it('should return true if it matches', () => {
      const normalizer = new NextDataPathnameNormalizer('build-id')
      const pathnames = [
        '/_next/data/build-id/index.json',
        '/_next/data/build-id/foo.json',
        '/_next/data/build-id/foo/bar.json',
        '/_next/data/build-id/fooo/bar.json',
      ]
      for (const pathname of pathnames) {
        expect(normalizer.match(pathname)).toBe(true)
      }
    })
  })

  describe('normalize', () => {
    it('should return the same pathname if we are not matched and the pathname does not start with the prefix', () => {
      const normalizer = new NextDataPathnameNormalizer('build-id')
      const pathnames = ['/foo', '/foo/bar', '/fooo/bar']
      for (const pathname of pathnames) {
        expect(normalizer.normalize(pathname)).toBe(pathname)
      }
    })

    it('should strip the prefix and the `.json` extension from the pathname when it matches', () => {
      const normalizer = new NextDataPathnameNormalizer('build-id')
      const pathnames = [
        '/_next/data/build-id/foo.json',
        '/_next/data/build-id/foo/bar.json',
        '/_next/data/build-id/fooo/bar.json',
      ]
      for (const pathname of pathnames) {
        expect(normalizer.normalize(pathname)).toBe(
          pathname.substring(
            '/_next/data/build-id'.length,
            pathname.length - '.json'.length
          )
        )
      }
    })

    it('should normalize `/index` to `/`', () => {
      const normalizer = new NextDataPathnameNormalizer('build-id')
      expect(normalizer.normalize('/_next/data/build-id/index.json')).toBe('/')
    })
  })
})