11require ( './helper' ) ;
22var Path = require ( '../modules/helpers/Path' ) ;
33
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+
424describe ( 'Path.extractParams' , function ( ) {
525 describe ( 'when a pattern does not have dynamic segments' , function ( ) {
626 var pattern = 'a/b/c' ;
@@ -19,11 +39,11 @@ describe('Path.extractParams', function () {
1939 } ) ;
2040
2141 describe ( 'when a pattern has dynamic segments' , function ( ) {
22- var pattern = 'comments/:id/edit' ;
42+ var pattern = 'comments/:id.:ext /edit' ;
2343
2444 describe ( 'and the path matches' , function ( ) {
2545 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 ' } ) ;
2747 } ) ;
2848 } ) ;
2949
@@ -35,7 +55,7 @@ describe('Path.extractParams', function () {
3555
3656 describe ( 'and the path matches with a segment containing a .' , function ( ) {
3757 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' } ) ;
3959 } ) ;
4060 } ) ;
4161 } ) ;
@@ -73,38 +93,37 @@ describe('Path.extractParams', function () {
7393 } ) ;
7494
7595 describe ( 'when a pattern has a *' , function ( ) {
76- var pattern = '/files/*.jpg' ;
77-
7896 describe ( 'and the path matches' , function ( ) {
7997 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' } ) ;
81101 } ) ;
82102 } ) ;
83103
84104 describe ( 'and the path does not match' , function ( ) {
85105 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 ) ;
87107 } ) ;
88108 } ) ;
89109 } ) ;
90- } ) ;
91110
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?' ;
98113
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+ } ) ;
102121 } ) ;
103- } ) ;
104122
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+ } ) ;
108127 } ) ;
109128 } ) ;
110129} ) ;
@@ -151,12 +170,22 @@ describe('Path.injectParams', function () {
151170 } ) ;
152171 } ) ;
153172 } ) ;
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+ } ) ;
154179} ) ;
155180
156181describe ( 'Path.extractQuery' , function ( ) {
157182 describe ( 'when the path contains a query string' , function ( ) {
158183 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' ] } ) ;
160189 } ) ;
161190 } ) ;
162191
@@ -177,6 +206,10 @@ describe('Path.withQuery', function () {
177206 it ( 'appends the query string' , function ( ) {
178207 expect ( Path . withQuery ( '/a/b/c' , { id : 'def' } ) ) . toEqual ( '/a/b/c?id=def' ) ;
179208 } ) ;
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+ } ) ;
180213} ) ;
181214
182215describe ( 'Path.normalize' , function ( ) {
0 commit comments