-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathrewrite-link-path.js
72 lines (69 loc) · 2.96 KB
/
rewrite-link-path.js
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 { rewriteLinkPath } from "../rewrite-link-path"
beforeEach(() => {
global.__TRAILING_SLASH__ = ``
global.__PATH_PREFIX__ = undefined
})
const getRewriteLinkPath = (option = `legacy`, pathPrefix = undefined) => {
global.__TRAILING_SLASH__ = option
global.__PATH_PREFIX__ = pathPrefix
return rewriteLinkPath
}
describe(`rewriteLinkPath`, () => {
it(`handles always option`, () => {
expect(getRewriteLinkPath(`always`)(`/path`, `/`)).toBe(`/path/`)
expect(getRewriteLinkPath(`always`)(`/path/`, `/`)).toBe(`/path/`)
expect(getRewriteLinkPath(`always`)(`#hash`, `/`)).toBe(`/#hash`)
expect(getRewriteLinkPath(`always`)(`?query_param=hello`, `/`)).toBe(
`/?query_param=hello`
)
expect(getRewriteLinkPath(`always`)(`?query_param=hello#anchor`, `/`)).toBe(
`/?query_param=hello#anchor`
)
expect(getRewriteLinkPath(`always`)(`/path#hash`, `/`)).toBe(`/path/#hash`)
expect(getRewriteLinkPath(`always`)(`/path?query_param=hello`, `/`)).toBe(
`/path/?query_param=hello`
)
expect(
getRewriteLinkPath(`always`)(`/path?query_param=hello#anchor`, `/`)
).toBe(`/path/?query_param=hello#anchor`)
expect(getRewriteLinkPath(`always`, `/prefix`)(`/`, `/`)).toBe(`/prefix/`)
})
it(`handles never option`, () => {
expect(getRewriteLinkPath(`never`)(`/path`, `/`)).toBe(`/path`)
expect(getRewriteLinkPath(`never`)(`/path/`, `/`)).toBe(`/path`)
expect(getRewriteLinkPath(`always`)(`#hash`, `/`)).toBe(`/#hash`)
expect(getRewriteLinkPath(`always`)(`?query_param=hello`, `/`)).toBe(
`/?query_param=hello`
)
expect(getRewriteLinkPath(`always`)(`?query_param=hello#anchor`, `/`)).toBe(
`/?query_param=hello#anchor`
)
expect(getRewriteLinkPath(`never`)(`/path/#hash`, `/`)).toBe(`/path#hash`)
expect(getRewriteLinkPath(`never`)(`/path/?query_param=hello`, `/`)).toBe(
`/path?query_param=hello`
)
expect(
getRewriteLinkPath(`never`)(`/path/?query_param=hello#anchor`, `/`)
).toBe(`/path?query_param=hello#anchor`)
expect(getRewriteLinkPath(`never`, `/prefix`)(`/`, `/`)).toBe(`/prefix`)
})
it(`handles ignore option`, () => {
expect(getRewriteLinkPath(`ignore`)(`/path`, `/`)).toBe(`/path`)
expect(getRewriteLinkPath(`ignore`)(`/path/`, `/`)).toBe(`/path/`)
expect(getRewriteLinkPath(`always`)(`#hash`, `/`)).toBe(`/#hash`)
expect(getRewriteLinkPath(`always`)(`?query_param=hello`, `/`)).toBe(
`/?query_param=hello`
)
expect(getRewriteLinkPath(`always`)(`?query_param=hello#anchor`, `/`)).toBe(
`/?query_param=hello#anchor`
)
expect(getRewriteLinkPath(`ignore`)(`/path#hash`, `/`)).toBe(`/path#hash`)
expect(getRewriteLinkPath(`ignore`)(`/path?query_param=hello`, `/`)).toBe(
`/path?query_param=hello`
)
expect(
getRewriteLinkPath(`ignore`)(`/path?query_param=hello#anchor`, `/`)
).toBe(`/path?query_param=hello#anchor`)
expect(getRewriteLinkPath(`ignore`, `/prefix`)(`/`, `/`)).toBe(`/prefix/`)
})
})