1
1
require ( './helper' ) ;
2
2
var Path = require ( '../modules/helpers/Path' ) ;
3
3
4
+ describe ( 'Path.extractParamNames' , function ( ) {
5
+ describe ( 'when a pattern contains no dynamic segments' , function ( ) {
6
+ it ( 'returns an empty array' , function ( ) {
7
+ expect ( Path . extractParamNames ( 'a/b/c' ) ) . toEqual ( [ ] ) ;
8
+ } ) ;
9
+ } ) ;
10
+
11
+ describe ( 'when a pattern contains :a and :b dynamic segments' , function ( ) {
12
+ it ( 'returns the correct names' , function ( ) {
13
+ expect ( Path . extractParamNames ( '/comments/:a/:b/edit' ) ) . toEqual ( [ 'a' , 'b' ] ) ;
14
+ } ) ;
15
+ } ) ;
16
+
17
+ describe ( 'when a pattern has a *' , function ( ) {
18
+ it ( 'uses the name "splat"' , function ( ) {
19
+ expect ( Path . extractParamNames ( '/files/*.jpg' ) ) . toEqual ( [ 'splat' ] ) ;
20
+ } ) ;
21
+ } ) ;
22
+ } ) ;
23
+
4
24
describe ( 'Path.extractParams' , function ( ) {
5
25
describe ( 'when a pattern does not have dynamic segments' , function ( ) {
6
26
var pattern = 'a/b/c' ;
@@ -19,11 +39,11 @@ describe('Path.extractParams', function () {
19
39
} ) ;
20
40
21
41
describe ( 'when a pattern has dynamic segments' , function ( ) {
22
- var pattern = 'comments/:id/edit' ;
42
+ var pattern = 'comments/:id.:ext /edit' ;
23
43
24
44
describe ( 'and the path matches' , function ( ) {
25
45
it ( 'returns an object with the params' , function ( ) {
26
- expect ( Path . extractParams ( pattern , 'comments/abc/edit' ) ) . toEqual ( { id : 'abc' } ) ;
46
+ expect ( Path . extractParams ( pattern , 'comments/abc.js /edit' ) ) . toEqual ( { id : 'abc' , ext : 'js ' } ) ;
27
47
} ) ;
28
48
} ) ;
29
49
@@ -35,7 +55,7 @@ describe('Path.extractParams', function () {
35
55
36
56
describe ( 'and the path matches with a segment containing a .' , function ( ) {
37
57
it ( 'returns an object with the params' , function ( ) {
38
- expect ( Path . extractParams ( pattern , 'comments/foo.bar/edit' ) ) . toEqual ( { id : 'foo. bar' } ) ;
58
+ expect ( Path . extractParams ( pattern , 'comments/foo.bar/edit' ) ) . toEqual ( { id : 'foo' , ext : ' bar' } ) ;
39
59
} ) ;
40
60
} ) ;
41
61
} ) ;
@@ -73,38 +93,37 @@ describe('Path.extractParams', function () {
73
93
} ) ;
74
94
75
95
describe ( 'when a pattern has a *' , function ( ) {
76
- var pattern = '/files/*.jpg' ;
77
-
78
96
describe ( 'and the path matches' , function ( ) {
79
97
it ( 'returns an object with the params' , function ( ) {
80
- expect ( Path . extractParams ( pattern , '/files/my/photo.jpg' ) ) . toEqual ( { splat : 'my/photo' } ) ;
98
+ expect ( Path . extractParams ( '/files/*' , '/files/my/photo.jpg' ) ) . toEqual ( { splat : 'my/photo.jpg' } ) ;
99
+ expect ( Path . extractParams ( '/files/*' , '/files/my/photo.jpg.zip' ) ) . toEqual ( { splat : 'my/photo.jpg.zip' } ) ;
100
+ expect ( Path . extractParams ( '/files/*.jpg' , '/files/my/photo.jpg' ) ) . toEqual ( { splat : 'my/photo' } ) ;
81
101
} ) ;
82
102
} ) ;
83
103
84
104
describe ( 'and the path does not match' , function ( ) {
85
105
it ( 'returns null' , function ( ) {
86
- expect ( Path . extractParams ( pattern , '/files/my/photo.png' ) ) . toBe ( null ) ;
106
+ expect ( Path . extractParams ( '/files/*.jpg' , '/files/my/photo.png' ) ) . toBe ( null ) ;
87
107
} ) ;
88
108
} ) ;
89
109
} ) ;
90
- } ) ;
91
110
92
- describe ( 'Path.extractParamNames' , function ( ) {
93
- describe ( 'when a pattern contains no dynamic segments' , function ( ) {
94
- it ( 'returns an empty array' , function ( ) {
95
- expect ( Path . extractParamNames ( 'a/b/c' ) ) . toEqual ( [ ] ) ;
96
- } ) ;
97
- } ) ;
111
+ describe ( 'when a pattern has a ?' , function ( ) {
112
+ var pattern = '/archive/?:name?' ;
98
113
99
- describe ( 'when a pattern contains :a and :b dynamic segments' , function ( ) {
100
- it ( 'returns the correct names' , function ( ) {
101
- expect ( Path . extractParamNames ( '/comments/:a/:b/edit' ) ) . toEqual ( [ 'a' , 'b' ] ) ;
114
+ describe ( 'and the path matches' , function ( ) {
115
+ it ( 'returns an object with the params' , function ( ) {
116
+ expect ( Path . extractParams ( pattern , '/archive' ) ) . toEqual ( { name : undefined } ) ;
117
+ expect ( Path . extractParams ( pattern , '/archive/' ) ) . toEqual ( { name : undefined } ) ;
118
+ expect ( Path . extractParams ( pattern , '/archive/foo' ) ) . toEqual ( { name : 'foo' } ) ;
119
+ expect ( Path . extractParams ( pattern , '/archivefoo' ) ) . toEqual ( { name : 'foo' } ) ;
120
+ } ) ;
102
121
} ) ;
103
- } ) ;
104
122
105
- describe ( 'when a pattern has a *' , function ( ) {
106
- it ( 'uses the name "splat"' , function ( ) {
107
- expect ( Path . extractParamNames ( '/files/*.jpg' ) ) . toEqual ( [ 'splat' ] ) ;
123
+ describe ( 'and the path does not match' , function ( ) {
124
+ it ( 'returns null' , function ( ) {
125
+ expect ( Path . extractParams ( pattern , '/archiv' ) ) . toBe ( null ) ;
126
+ } ) ;
108
127
} ) ;
109
128
} ) ;
110
129
} ) ;
@@ -151,12 +170,22 @@ describe('Path.injectParams', function () {
151
170
} ) ;
152
171
} ) ;
153
172
} ) ;
173
+
174
+ describe ( 'when a pattern has multiple splats' , function ( ) {
175
+ it ( 'returns the correct path' , function ( ) {
176
+ expect ( Path . injectParams ( '/a/*/c/*' , { splat : [ 'b' , 'd' ] } ) ) . toEqual ( '/a/b/c/d' ) ;
177
+ } ) ;
178
+ } ) ;
154
179
} ) ;
155
180
156
181
describe ( 'Path.extractQuery' , function ( ) {
157
182
describe ( 'when the path contains a query string' , function ( ) {
158
183
it ( 'returns the parsed query object' , function ( ) {
159
- expect ( Path . extractQuery ( '/a/b/c?id=def&show=true' ) ) . toEqual ( { id : 'def' , show : 'true' } ) ;
184
+ expect ( Path . extractQuery ( '/?id=def&show=true' ) ) . toEqual ( { id : 'def' , show : 'true' } ) ;
185
+ } ) ;
186
+
187
+ it ( 'properly handles arrays' , function ( ) {
188
+ expect ( Path . extractQuery ( '/?id%5B%5D=a&id%5B%5D=b' ) ) . toEqual ( { id : [ 'a' , 'b' ] } ) ;
160
189
} ) ;
161
190
} ) ;
162
191
@@ -177,6 +206,10 @@ describe('Path.withQuery', function () {
177
206
it ( 'appends the query string' , function ( ) {
178
207
expect ( Path . withQuery ( '/a/b/c' , { id : 'def' } ) ) . toEqual ( '/a/b/c?id=def' ) ;
179
208
} ) ;
209
+
210
+ it ( 'merges two query strings' , function ( ) {
211
+ expect ( Path . withQuery ( '/path?a=b' , { c : [ 'd' , 'e' ] } ) ) . toEqual ( '/path?a=b&c%5B0%5D=d&c%5B1%5D=e' ) ;
212
+ } ) ;
180
213
} ) ;
181
214
182
215
describe ( 'Path.normalize' , function ( ) {
0 commit comments