-
Notifications
You must be signed in to change notification settings - Fork 1
/
can-param-test.js
62 lines (51 loc) · 2.1 KB
/
can-param-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var QUnit = require("steal-qunit");
var param = require("./can-param");
QUnit.module("can-param");
QUnit.test("can-param -- naive mode", function(assert) {
assert.deepEqual( param( {foo: "bar", baz: "zed"} ), "foo=bar&baz=zed", "Regular object");
assert.deepEqual( param( {foo: {bar: "baz"}} ), encodeURI("foo[bar]=baz"), "Nested object");
assert.deepEqual( param( {foo: ["bar", "baz"]} ), encodeURI("foo[]=bar&foo[]=baz"), "Nested array");
assert.deepEqual( param( {foo: "bar & baz"} ), "foo=bar+%26+baz", "Spec chars values");
assert.equal(param({
age: {
or: [ {lte: 5}, null ]
}
}), encodeURI("age[or][0][lte]=5&age[or][1]=null"));
assert.deepEqual(param({
"undefined": undefined,
"null": null,
"NaN": NaN,
"true": true,
"false": false
}),"undefined=undefined&null=null&NaN=NaN&true=true&false=false","true, false, undefined, etc");
});
QUnit.test("can-param -- standards mode", function(assert) {
param.setStandardsMode(true);
assert.deepEqual( param( {foo: "bar", baz: "zed"} ), "foo=bar&baz=zed", "Regular object");
assert.deepEqual( param( {foo: {bar: "baz"}} ), encodeURI("foo[bar]=baz"), "Nested object");
assert.deepEqual( param( {foo: ["bar", "baz"]} ), encodeURI("foo[]=bar&foo[]=baz"), "Nested array");
assert.deepEqual( param( {foo: "bar & baz"} ), "foo=bar+%26+baz", "Spec chars values");
assert.equal(param({
age: {
or: [ {lte: 5}, null ]
}
}), encodeURI("age[or][0][lte]=5&age[or][1]="));
assert.deepEqual(param({
"undefined": undefined,
"null": null,
"NaN": NaN,
"true": true,
"false": false
}),"null=&NaN=NaN&true=true&false=false","true, false, undefined, etc");
param.setStandardsMode(false);
});
QUnit.test("Encoding arrays of objects includes indices", function(assert) {
var object = {items: [{name:'one'}, {name:'two'}]};
var out = param(object);
assert.equal(out, "items%5B0%5D%5Bname%5D=one&items%5B1%5D%5Bname%5D=two");
});
QUnit.test("Encoding array of primitives does not include indices", function(assert) {
var object = {items: ['one', 'two']};
var out = param(object);
assert.equal(out, "items%5B%5D=one&items%5B%5D=two");
});