1
1
$ ( function ( ) {
2
2
3
-
3
+
4
4
module ( "Serialize" ) ;
5
5
6
6
test ( "simple properties" , function ( ) {
7
7
equals ( $ . querystring ( { name : "John" } ) , "name=John" ) ;
8
8
equals ( $ . querystring ( { name : "John" , age :59 } ) , "name=John&age=59" ) ;
9
9
} ) ;
10
-
10
+
11
11
test ( "properties requiring URI encoding" , function ( ) {
12
12
equals ( $ . querystring ( { name : "John Doe" } ) , "name=John%20Doe" ) ;
13
13
equals ( $ . querystring ( { name : "John Doe" , garble : " /# #/ " } ) , "name=John%20Doe&garble=%20%2F%23%20%23%2F%20" ) ;
14
14
} ) ;
15
-
15
+
16
16
test ( "empty values" , function ( ) {
17
17
equals ( $ . querystring ( { } ) , "" , "Empty objects serialize to empty strings." ) ;
18
18
equals ( $ . querystring ( { name :null } ) , "" , "Properties with null values are omitted from resulting string." ) ;
19
19
equals ( $ . querystring ( { name :'' } ) , "name=" , 'Blank strings are included in result.' ) ;
20
20
equals ( $ . querystring ( { boring :false } ) , "boring=false" , '`false` literal is included in result.' ) ;
21
21
} ) ;
22
-
22
+
23
23
test ( "arrays" , function ( ) {
24
24
equals ( $ . querystring ( { people : [ 'Larry' , 'Curly' , 'Moe' ] } ) , "people[]=Larry&people[]=Curly&people[]=Moe" ) ;
25
25
} ) ;
26
-
26
+
27
27
test ( "simple objects" , function ( ) {
28
28
equals ( $ . querystring ( { person : { name :"John" , age :110 } } ) , "person[name]=John&person[age]=110" ) ;
29
29
} ) ;
30
-
30
+
31
31
test ( "nested objects & arrays" , function ( ) {
32
32
equals (
33
33
$ . querystring ( { person : { name :"John" , age :110 , drinks : [ 'Beer' , 'Whisky' , 'Wine' ] , location : { city : "New York" , state :"NY" } } } ) ,
34
34
"person[name]=John&person[age]=110&person[drinks][]=Beer&person[drinks][]=Whisky&person[drinks][]=Wine&person[location][city]=New%20York&person[location][state]=NY"
35
35
) ;
36
36
} ) ;
37
-
38
-
37
+
38
+
39
39
module ( "Parse" ) ;
40
-
40
+
41
41
test ( "simple properties" , function ( ) {
42
42
deepEqual ( $ . querystring ( "?name=John" ) , { name : "John" } , "With leading '?'" ) ;
43
43
deepEqual ( $ . querystring ( "name=John" ) , { name : "John" } , "Without leading '?'" ) ;
44
44
deepEqual ( $ . querystring ( "http://google.com/?name=John" ) , { name : "John" } , "With domains prefix." ) ;
45
45
} ) ;
46
-
46
+
47
47
test ( "coercion to native types" , function ( ) {
48
48
deepEqual ( $ . querystring ( '?boring=false' ) , { boring :false } , 'Should parse true/false to native boolean types.' ) ;
49
49
deepEqual ( $ . querystring ( '?awesome=true' ) , { awesome :true } , 'Should parse true/false to native boolean types.' ) ;
@@ -55,34 +55,57 @@ $(function() {
55
55
deepEqual ( $ . querystring ( "?name=John%20Doe" ) , { name : "John Doe" } ) ;
56
56
deepEqual ( $ . querystring ( "?name=John%20Doe&garble=%20%2F%23%20%23%2F%20" ) , { name : "John Doe" , garble : " /# #/ " } ) ;
57
57
} ) ;
58
-
58
+
59
59
test ( 'empty values' , function ( ) {
60
60
deepEqual ( $ . querystring ( '?awesome' ) , { awesome :"" } ) ;
61
61
deepEqual ( $ . querystring ( '?awesome=' ) , { awesome :"" } ) ;
62
62
} ) ;
63
-
63
+
64
64
test ( 'arrays' , function ( ) {
65
65
deepEqual (
66
66
$ . querystring ( "?drinks[]=Beer&drinks[]=Whisky&drinks[]=Wine" ) ,
67
67
{ drinks : [ 'Beer' , 'Whisky' , 'Wine' ] }
68
68
) ;
69
69
} ) ;
70
-
70
+
71
71
test ( 'simple objects' , function ( ) {
72
72
deepEqual ( $ . querystring ( "?person[name]=John" ) , { person : { name : "John" } } ) ;
73
73
} ) ;
74
-
74
+
75
75
test ( 'nested objects & arrays' , function ( ) {
76
76
deepEqual (
77
77
$ . querystring ( "?person[location][city]=NYC&&person[drinks][]=Beer&person[drinks][]=Whisky&person[drinks][]=Wine" ) ,
78
78
{ person : { location : { city :'NYC' } } , drinks : [ 'Beer' , 'Whisky' , 'Wine' ] }
79
- ) ;
79
+ ) ;
80
+ } ) ;
81
+
82
+ test ( 'missing querystring' , function ( ) {
83
+ deepEqual ( $ . querystring ( '/foo?' ) , { } , 'Should give empty object with ? only' )
84
+ deepEqual ( $ . querystring ( '/foo' ) , { } , 'Should give empty object with no ?' )
85
+ deepEqual ( $ . querystring ( 'http://google.com/foo?' ) , { } , 'Should give empty object with host/path and ?' )
86
+ deepEqual ( $ . querystring ( 'http://google.com/foo' ) , { } , 'Should give empty object with host/path and no ?' )
80
87
} ) ;
81
-
88
+
82
89
module ( "Element methods" ) ;
83
-
84
- test ( "get querystring from link" , function ( ) { } ) ;
85
- test ( "set link querystring" , function ( ) { } ) ;
90
+
91
+ test ( "get querystring from link" , function ( ) {
92
+ deepEqual ( $ ( '<a href="/foo?">link</a>' ) . querystring ( ) , { } , "With no querystring" ) ;
93
+ deepEqual ( $ ( '<a href="/foo?name=John">link</a>' ) . querystring ( ) , { name : "John" } , "With querystring" ) ;
94
+ deepEqual ( $ ( '<a href="http://google.com/?name=John">link</a>' ) . querystring ( ) , { name : "John" } , "With domains prefix." ) ;
95
+ // deepEqual($('<a href="/foo">link</a>').querystring(), {}, "With no ?");
96
+ } ) ;
97
+ test ( "set link querystring existing" , function ( ) {
98
+ equals (
99
+ $ ( '<a href="/foo?name=John">link</a>' ) . querystring ( { awesome :'value' } ) . attr ( 'href' ) ,
100
+ '/foo?name=John&awesome=value'
101
+ )
102
+ } ) ;
103
+ test ( "set link querystring non-existent" , function ( ) {
104
+ equals (
105
+ $ ( '<a href="/foo">link</a>' ) . querystring ( { awesome :'value' } ) . attr ( 'href' ) ,
106
+ '/foo?awesome=value'
107
+ )
108
+ } ) ;
86
109
// TODO: Tests for clearing, merging & using `form` elements.
87
-
88
- } ) ;
110
+
111
+ } ) ;
0 commit comments