@@ -47,6 +47,34 @@ describe('Path.extractParams', function () {
4747 } ) ;
4848 } ) ;
4949
50+ describe ( 'and the pattern is optional' , function ( ) {
51+ var pattern = 'comments/:id?/edit'
52+
53+ describe ( 'and the path matches with supplied param' , function ( ) {
54+ it ( 'returns an object with the params' , function ( ) {
55+ expect ( Path . extractParams ( pattern , 'comments/123/edit' ) ) . toEqual ( { id : '123' } ) ;
56+ } ) ;
57+ } ) ;
58+ describe ( 'and the path matches without supplied param' , function ( ) {
59+ it ( 'returns an object with param set to null' , function ( ) {
60+ expect ( Path . extractParams ( pattern , 'comments//edit' ) ) . toEqual ( { id : null } ) ;
61+ } ) ;
62+ } ) ;
63+ } ) ;
64+ describe ( 'and the pattern and forward slash are optional' , function ( ) {
65+ var pattern = 'comments/:id?/?edit'
66+
67+ describe ( 'and the path matches with supplied param' , function ( ) {
68+ it ( 'returns an object with the params' , function ( ) {
69+ expect ( Path . extractParams ( pattern , 'comments/123/edit' ) ) . toEqual ( { id : '123' } ) ;
70+ } ) ;
71+ } ) ;
72+ describe ( 'and the path matches without supplied param' , function ( ) {
73+ it ( 'returns an object with param set to null' , function ( ) {
74+ expect ( Path . extractParams ( pattern , 'comments/edit' ) ) . toEqual ( { id : null } ) ;
75+ } ) ;
76+ } ) ;
77+ } ) ;
5078 describe ( 'and the path does not match' , function ( ) {
5179 it ( 'returns null' , function ( ) {
5280 expect ( Path . extractParams ( pattern , 'users/123' ) ) . toBe ( null ) ;
@@ -166,6 +194,30 @@ describe('Path.injectParams', function () {
166194 } ) ;
167195 } ) ;
168196
197+ describe ( 'and a param is optional' , function ( ) {
198+ var pattern = 'comments/:id?/edit' ;
199+
200+ it ( 'returns the correct path when param is supplied' , function ( ) {
201+ expect ( Path . injectParams ( pattern , { id :'123' } ) ) . toEqual ( 'comments/123/edit' ) ;
202+ } ) ;
203+
204+ it ( 'returns the correct path when param is not supplied' , function ( ) {
205+ expect ( Path . injectParams ( pattern , { } ) ) . toEqual ( 'comments//edit' ) ;
206+ } ) ;
207+ } ) ;
208+
209+ describe ( 'and a param and forward slash are optional' , function ( ) {
210+ var pattern = 'comments/:id?/?edit' ;
211+
212+ it ( 'returns the correct path when param is supplied' , function ( ) {
213+ expect ( Path . injectParams ( pattern , { id :'123' } ) ) . toEqual ( 'comments/123/edit' ) ;
214+ } ) ;
215+
216+ it ( 'returns the correct path when param is not supplied' , function ( ) {
217+ expect ( Path . injectParams ( pattern , { } ) ) . toEqual ( 'comments/edit' ) ;
218+ } ) ;
219+ } ) ;
220+
169221 describe ( 'and all params are present' , function ( ) {
170222 it ( 'returns the correct path' , function ( ) {
171223 expect ( Path . injectParams ( pattern , { id : 'abc' } ) ) . toEqual ( 'comments/abc/edit' ) ;
0 commit comments