@@ -23,6 +23,7 @@ var urlencode = utils.urlencode;
2323var htmlTreeAsString = utils . htmlTreeAsString ;
2424var htmlElementAsString = utils . htmlElementAsString ;
2525var parseUrl = utils . parseUrl ;
26+ var safeJoin = utils . safeJoin ;
2627
2728describe ( 'utils' , function ( ) {
2829 describe ( 'isUndefined' , function ( ) {
@@ -421,4 +422,35 @@ describe('utils', function() {
421422 ) ;
422423 } ) ;
423424 } ) ;
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+ } ) ;
424456} ) ;
0 commit comments