@@ -23,6 +23,7 @@ var urlencode = utils.urlencode;
23
23
var htmlTreeAsString = utils . htmlTreeAsString ;
24
24
var htmlElementAsString = utils . htmlElementAsString ;
25
25
var parseUrl = utils . parseUrl ;
26
+ var safeJoin = utils . safeJoin ;
26
27
27
28
describe ( 'utils' , function ( ) {
28
29
describe ( 'isUndefined' , function ( ) {
@@ -421,4 +422,35 @@ describe('utils', function() {
421
422
) ;
422
423
} ) ;
423
424
} ) ;
425
+
426
+ describe ( 'safeJoin' , function ( ) {
427
+ it ( 'should return empty string if not-array input provided' , function ( ) {
428
+ assert . equal ( safeJoin ( 'asd' ) , '' ) ;
429
+ assert . equal ( safeJoin ( undefined ) , '' ) ;
430
+ assert . equal ( safeJoin ( { foo : 123 } ) , '' ) ;
431
+ } ) ;
432
+
433
+ it ( 'should default to comma, as regular join() call' , function ( ) {
434
+ assert . equal ( safeJoin ( [ 'a' , 'b' , 'c' ] ) , 'a,b,c' ) ;
435
+ } ) ;
436
+
437
+ it ( 'should stringify complex values, as regular String() call' , function ( ) {
438
+ assert . equal (
439
+ safeJoin ( [ 1 , 'a' , { foo : 42 } , [ 1 , 2 , 3 ] ] , ' ' ) ,
440
+ '1 a [object Object] 1,2,3'
441
+ ) ;
442
+ } ) ;
443
+
444
+ it ( 'should still work with unserializeable values' , function ( ) {
445
+ function Foo ( ) { }
446
+ Foo . prototype . toString = function ( ) {
447
+ throw Error ( 'whoops!' ) ;
448
+ } ;
449
+
450
+ assert . equal (
451
+ safeJoin ( [ new Foo ( ) , 'abc' , new Foo ( ) , 42 ] , ' X ' ) ,
452
+ '[value cannot be serialized] X abc X [value cannot be serialized] X 42'
453
+ ) ;
454
+ } ) ;
455
+ } ) ;
424
456
} ) ;
0 commit comments