/
formatPattern-test.js
119 lines (97 loc) · 4.05 KB
/
formatPattern-test.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
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
116
117
118
119
import expect from 'expect'
import { formatPattern } from '../PatternUtils'
describe('formatPattern', function () {
describe('when a pattern does not have dynamic segments', function () {
const pattern = '/a/b/c'
it('returns the pattern', function () {
expect(formatPattern(pattern, {})).toEqual(pattern)
})
})
describe('when a pattern has dynamic segments', function () {
const pattern = '/comments/:id/edit'
describe('and a param is missing', function () {
it('throws an Error', function () {
expect(function () {
formatPattern(pattern, {})
}).toThrow(Error)
})
})
describe('and a param is optional', function () {
const pattern = '/comments/(:id)/edit'
it('returns the correct path when param is supplied', function () {
expect(formatPattern(pattern, { id:'123' })).toEqual('/comments/123/edit')
})
it('returns the correct path when param is not supplied', function () {
expect(formatPattern(pattern, {})).toEqual('/comments/edit')
})
})
describe('and a param and forward slash are optional', function () {
const pattern = '/comments(/:id)/edit'
it('returns the correct path when param is supplied', function () {
expect(formatPattern(pattern, { id:'123' })).toEqual('/comments/123/edit')
})
it('returns the correct path when param is not supplied', function () {
expect(formatPattern(pattern, {})).toEqual('/comments/edit')
})
})
describe('and all params are present', function () {
it('returns the correct path', function () {
expect(formatPattern(pattern, { id: 'abc' })).toEqual('/comments/abc/edit')
})
it('returns the correct path when the value is 0', function () {
expect(formatPattern(pattern, { id: 0 })).toEqual('/comments/0/edit')
})
})
describe('and some params have special URL encoding', function () {
it('returns the correct path', function () {
expect(formatPattern(pattern, { id: 'one, two' })).toEqual('/comments/one%2C+two/edit')
})
})
describe('and a param has a forward slash', function () {
it('preserves the forward slash', function () {
expect(formatPattern(pattern, { id: 'the/id' })).toEqual('/comments/the%2Fid/edit')
})
})
describe('and some params contain dots', function () {
it('returns the correct path', function () {
expect(formatPattern(pattern, { id: 'alt.black.helicopter' })).toEqual('/comments/alt.black.helicopter/edit')
})
})
describe('and some params contain special characters', function () {
it('returns the correct path', function () {
expect(formatPattern(pattern, { id: '?not=confused&with=query#string' })).toEqual('/comments/%3Fnot%3Dconfused%26with%3Dquery%23string/edit')
})
})
})
describe('when a pattern has one splat', function () {
it('returns the correct path', function () {
expect(formatPattern('/a/*/d', { splat: 'b/c' })).toEqual('/a/b/c/d')
})
})
describe('when a pattern has multiple splats', function () {
it('returns the correct path', function () {
expect(formatPattern('/a/*/c/*', { splat: [ 'b', 'd' ] })).toEqual('/a/b/c/d')
})
it('complains if not given enough splat values', function () {
expect(function () {
formatPattern('/a/*/c/*', { splat: [ 'b' ] })
}).toThrow(Error)
})
})
describe('when a pattern has a greedy splat', function () {
it('returns the correct path', function () {
expect(formatPattern('/a/**/d', { splat: 'b/c/d' })).toEqual('/a/b/c/d/d')
expect(formatPattern('/a/**/d/**', { splat: [ 'b/c/d', 'e' ] })).toEqual('/a/b/c/d/d/e')
})
it('complains if not given enough splat values', function () {
expect(function () {
formatPattern('/a/**/d/**', { splat: [ 'b/c/d' ] })
}).toThrow(Error)
})
})
describe('when a pattern has dots', function () {
it('returns the correct path', function () {
expect(formatPattern('/foo.bar.baz')).toEqual('/foo.bar.baz')
})
})
})