forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatPattern-test.js
More file actions
249 lines (197 loc) · 9.41 KB
/
formatPattern-test.js
File metadata and controls
249 lines (197 loc) · 9.41 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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('with a bad pattern', function () {
describe('that is missing an end param', function () {
const pattern = '/comments/(:id'
describe('with no value given to the formatPattern function', function () {
it('throws an error', function () {
expect(function () {
formatPattern(pattern, {})
}).toThrow(/Path "\/comments\/\(:id" is missing end paren at segment ":id"/)
})
})
describe('with value given to the formatPattern function', function () {
it('throws an error', function () {
expect(function () {
formatPattern(pattern, { id: '1' })
}).toThrow(/Path "\/comments\/\(:id" is missing end paren/)
})
})
})
})
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 a param is optional with addtional text prior to the params', function () {
const pattern = '/search(/forum/:id)'
it('returns the correct path when param is supplied', function () {
expect(formatPattern(pattern, { id:'123' })).toEqual('/search/forum/123')
})
it('returns the correct path when param is not supplied', function () {
expect(formatPattern(pattern, { })).toEqual('/search')
})
})
describe('and a param is optional with multiple segments and addtional text prior to the params', function () {
const pattern = '/search(/forum/:forum_id)(/comment/:comment_id)'
it('returns the correct path when param is supplied', function () {
expect(formatPattern(pattern, { forum_id: '123', comment_id: '456' })).toEqual('/search/forum/123/comment/456')
})
it('returns the correct path when forum_id param is supplied', function () {
expect(formatPattern(pattern, { forum_id: '123' })).toEqual('/search/forum/123')
})
it('returns the correct path when comment_id param is supplied', function () {
expect(formatPattern(pattern, { comment_id: '456' })).toEqual('/search/comment/456')
})
it('returns the correct path when param is not supplied', function () {
expect(formatPattern(pattern, { })).toEqual('/search')
})
})
describe('and a param is optional with multiple segments in the optional part', function () {
const pattern = '/search(/forum/:forum_id/comment/:comment_id)'
it('returns the correct path when param is supplied', function () {
expect(formatPattern(pattern, { forum_id:'123', comment_id: '456' })).toEqual('/search/forum/123/comment/456')
})
it('returns the correct path when forum_id param is not supplied', function () {
expect(formatPattern(pattern, { forum_id:'123' })).toEqual('/search')
})
it('returns the correct path when comment_id param is not supplied 2', function () {
expect(formatPattern(pattern, { comment_id:'456' })).toEqual('/search')
})
it('returns the correct path when param is not supplied', function () {
expect(formatPattern(pattern, { })).toEqual('/search')
})
})
describe('and a param is optional with nested optional segments with addtional text prior to the params', function () {
const pattern = '/search(/forum/:forum_id(/comment/:comment_id))'
it('returns the correct path when params are supplied', function () {
expect(formatPattern(pattern, { forum_id:'123', comment_id: '456' })).toEqual('/search/forum/123/comment/456')
})
it('returns the correct path when forum_id param is supplied', function () {
expect(formatPattern(pattern, { forum_id:'123' })).toEqual('/search/forum/123')
})
it('returns the correct path when comment_id param is supplied', function () {
expect(formatPattern(pattern, { comment_id: '456' })).toEqual('/search')
})
it('returns the correct path when param is not supplied', function () {
expect(formatPattern(pattern, { })).toEqual('/search')
})
})
describe('and a param is optional with nested optional segments with addtional text prior to the params in different order', function () {
const pattern = '/search((/forum/:forum_id)/comment/:comment_id)'
it('returns the correct path when params are supplied', function () {
expect(formatPattern(pattern, { forum_id:'123', comment_id: '456' })).toEqual('/search/forum/123/comment/456')
})
it('returns the correct path when comment_id param is supplied', function () {
expect(formatPattern(pattern, { comment_id: '456' })).toEqual('/search/comment/456')
})
it('returns the correct path when forum_id param is supplied', function () {
expect(formatPattern(pattern, { forum_id:'123' })).toEqual('/search')
})
it('returns the correct path when param is not supplied', function () {
expect(formatPattern(pattern, { })).toEqual('/search')
})
})
describe('and a param is parentheses escaped', function () {
const pattern = '/comments\\(:id\\)'
it('returns the correct path when param is supplied', function () {
expect(formatPattern(pattern, { id:'123' })).toEqual('/comments(123)')
})
})
describe('and a param is parentheses escaped with additional param', function () {
const pattern = '/comments\\(:id\\)/:mode'
it('returns the correct path when param is supplied', function () {
expect(formatPattern(pattern, { id:'123', mode: 'edit' })).toEqual('/comments(123)/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%20two/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')
})
})
})