File size: 1,711 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
import { PrefixPathnameNormalizer } from './prefix'

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

    it('should return true if the pathname starts with the prefix', () => {
      const normalizer = new PrefixPathnameNormalizer('/foo')
      const pathnames = ['/foo', '/foo/bar', '/foo/bar/baz']
      for (const pathname of pathnames) {
        expect(normalizer.match(pathname)).toBe(true)
      }
    })
  })

  it('should throw if the prefix ends with a slash', () => {
    expect(() => new PrefixPathnameNormalizer('/foo/')).toThrow()
    expect(() => new PrefixPathnameNormalizer('/')).toThrow()
  })

  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 PrefixPathnameNormalizer('/foo')
      let pathnames = ['/bar', '/bar/foo', '/fooo/bar']
      for (const pathname of pathnames) {
        expect(normalizer.normalize(pathname)).toBe(pathname)
      }
    })

    it('should strip the prefix from the pathname when it matches', () => {
      const normalizer = new PrefixPathnameNormalizer('/foo')
      const pathnames = ['/foo', '/foo/bar', '/foo/bar/baz']
      for (const pathname of pathnames) {
        expect(normalizer.normalize(pathname)).toBe(
          pathname.substring(4) || '/'
        )
      }
    })
  })
})