@@ -1350,22 +1350,36 @@ describe('state', function () {
1350
1350
1351
1351
describe ( "typed parameter handling" , function ( ) {
1352
1352
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
+
1354
1366
beforeEach ( function ( ) {
1355
1367
$stateProvider . state ( {
1356
1368
name : "types" ,
1357
1369
url : "/types/{p1:string}/{p2:date}" ,
1358
1370
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 ,
1362
1374
}
1363
1375
} ) ;
1376
+
1364
1377
$stateProvider . state ( {
1365
1378
name : "types.substate" ,
1366
- url : "/sub/{p3[]:int }/{p4:json}?{p5:bool}" ,
1379
+ url : "/sub/{p3[]}/{p4:json}?{p5:bool}" ,
1367
1380
params : {
1368
- "p3[]" : [ 10 ]
1381
+ "p3[]" : substateDefaults [ 'p3[]' ] ,
1382
+ p4 : substateDefaults . p4 ,
1369
1383
}
1370
1384
} ) ;
1371
1385
} ) ;
@@ -1449,22 +1463,45 @@ describe('state', function () {
1449
1463
expect ( $state . params . nonurl && $state . params . nonurl . errorscope ) . toBe ( $rootScope ) ;
1450
1464
} ) ) ;
1451
1465
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
+ } ) ;
1457
1469
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' } } ;
1459
1473
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
+ } ) ;
1460
1480
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
+ } ) ;
1464
1485
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
+ } ) ;
1468
1505
1469
1506
it ( 'should support non-url parameters' , inject ( function ( $state , $q , $stateParams ) {
1470
1507
$state . transitionTo ( A ) ; $q . flush ( ) ;
0 commit comments