@@ -1350,22 +1350,36 @@ describe('state', function () {
13501350
13511351 describe ( "typed parameter handling" , function ( ) {
13521352 var checkStateUrl ;
1353-
1353+ var nov15 = new Date ( 2014 , 10 , 15 ) ;
1354+
1355+ var defaults = {
1356+ p1 : [ 'defaultValue' ] ,
1357+ p2 : nov15 ,
1358+ nonurl : null ,
1359+ } ;
1360+
1361+ var substateDefaults = extend ( {
1362+ "p3[]" : [ 'a' ] ,
1363+ p4 : null ,
1364+ } , defaults ) ;
1365+
13541366 beforeEach ( function ( ) {
13551367 $stateProvider . state ( {
13561368 name : "types" ,
13571369 url : "/types/{p1:string}/{p2:date}" ,
13581370 params : {
1359- p1 : { value : [ "defaultValue" ] , array : true } ,
1360- p2 : new Date ( 2014 , 10 , 15 ) ,
1361- nonurl : null
1371+ p1 : { value : defaults . p1 , array : true } ,
1372+ p2 : defaults . p2 ,
1373+ nonurl : defaults . nonurl ,
13621374 }
13631375 } ) ;
1376+
13641377 $stateProvider . state ( {
13651378 name : "types.substate" ,
1366- url : "/sub/{p3[]:int }/{p4:json}?{p5:bool}" ,
1379+ url : "/sub/{p3[]}/{p4:json}?{p5:bool}" ,
13671380 params : {
1368- "p3[]" : [ 10 ]
1381+ "p3[]" : substateDefaults [ 'p3[]' ] ,
1382+ p4 : substateDefaults . p4 ,
13691383 }
13701384 } ) ;
13711385 } ) ;
@@ -1449,22 +1463,45 @@ describe('state', function () {
14491463 expect ( $state . params . nonurl && $state . params . nonurl . errorscope ) . toBe ( $rootScope ) ;
14501464 } ) ) ;
14511465
1452- it ( 'should map to/from the $location.url() and $stateParams' , inject ( function ( $state , $location , $q , $rootScope ) {
1453- var nov15 = new Date ( 2014 , 10 , 15 ) ;
1454- var defaults = { p1 : [ 'defaultValue' ] , p2 : nov15 , nonurl : null } ;
1455- var params = { p1 : [ "foo" ] , p2 : nov15 } ;
1456- var nonurl = { nonurl : { foo : 'bar' } } ;
1466+ it ( 'should map default param values to/from the $location.url() and $stateParams' , function ( ) {
1467+ checkStateUrl ( 'types' , '/types/defaultValue/2014-11-15' , { } , defaults ) ;
1468+ } ) ;
14571469
1458- checkStateUrl ( 'types' , '/types/defaultValue/2014-11-15' , { } , defaults ) ;
1470+ it ( 'should combine and map params and default param values to/from the $location.url() and $stateParams, except for nonurl params' , function ( ) {
1471+ var params = { p1 : [ "foo" ] } ;
1472+ var nonurl = { nonurl : { foo : 'bar' } } ;
14591473 checkStateUrl ( 'types' , "/types/foo/2014-11-15" , params , defaults , nonurl ) ;
1474+ } ) ;
1475+
1476+ it ( 'should map json param values to/from the $location.url() and $stateParams' , function ( ) {
1477+ var params = { p4 : { baz : "qux" } } ;
1478+ checkStateUrl ( 'types.substate' , "/types/defaultValue/2014-11-15/sub/a/%7B%22baz%22:%22qux%22%7D" , params , substateDefaults ) ;
1479+ } ) ;
14601480
1461- extend ( defaults , { "p3[]" : [ 10 ] } ) ;
1462- extend ( params , { p4 : { baz : "qux" } } ) ;
1463- checkStateUrl ( 'types.substate' , "/types/foo/2014-11-15/sub/10/%7B%22baz%22:%22qux%22%7D" , params , defaults , nonurl ) ;
1481+ it ( 'should combine and map array default param values and normal param values to/from the $location.url() and $stateParams' , function ( ) {
1482+ var params = { p1 : [ "foo" ] , p2 : nov15 , p4 : { baz : "qux" } } ;
1483+ checkStateUrl ( 'types.substate' , "/types/foo/2014-11-15/sub/a/%7B%22baz%22:%22qux%22%7D" , params , substateDefaults ) ;
1484+ } ) ;
14641485
1465- extend ( params , { p5 : true } ) ;
1466- checkStateUrl ( 'types.substate' , "/types/foo/2014-11-15/sub/10/%7B%22baz%22:%22qux%22%7D?p5=1" , params , defaults , nonurl ) ;
1467- } ) ) ;
1486+ it ( 'should map array default param values to/from the $location.url() and $stateParams' , function ( ) {
1487+ checkStateUrl ( 'types.substate' , "/types/defaultValue/2014-11-15/sub/a/null" , { } , substateDefaults ) ;
1488+ } ) ;
1489+
1490+ it ( 'should map multi-value array default param values to/from the $location.url() and $stateParams' , function ( ) {
1491+ var params = { "p3[]" : [ 'a' , 'b' ] } ;
1492+ var arrayDefaults = extend ( { } , substateDefaults , params ) ;
1493+ checkStateUrl ( 'types.substate' , "/types/defaultValue/2014-11-15/sub/a-b/null" , params , arrayDefaults ) ;
1494+ } ) ;
1495+
1496+ it ( 'should map boolean as integers to/from the $location.url() and $stateParams' , function ( ) {
1497+ var params = { p5 : true } ;
1498+ checkStateUrl ( 'types.substate' , "/types/defaultValue/2014-11-15/sub/a/null?p5=1" , params , substateDefaults ) ;
1499+ } ) ;
1500+
1501+ it ( 'should map all the things to/from the $location.url() and $stateParams' , function ( ) {
1502+ var params = { p1 : [ "foo" ] , p4 : { baz : "qux" } , p5 : true } ;
1503+ checkStateUrl ( 'types.substate' , "/types/foo/2014-11-15/sub/a/%7B%22baz%22:%22qux%22%7D?p5=1" , params , substateDefaults ) ;
1504+ } ) ;
14681505
14691506 it ( 'should support non-url parameters' , inject ( function ( $state , $q , $stateParams ) {
14701507 $state . transitionTo ( A ) ; $q . flush ( ) ;
0 commit comments