-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryStirng.test.ts
66 lines (53 loc) · 1.75 KB
/
QueryStirng.test.ts
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
63
64
65
66
import {QueryString} from "../../../src";
describe("QueryString", () => {
test("Prevent pollution", () => {
const injectTest = {};
const injectKey = "__proto__[polluted]";
const qs = new QueryString();
qs.push(injectKey, true);
expect(Reflect.has(Reflect.get(injectTest, "__proto__"), "polluted")).toBeFalsy();
});
test("Base", () => {
const qs = new QueryString();
const field = "foo[bar][baz][0]";
const parsed = qs.parseField(field);
expect(parsed)
.toEqual(["foo", "bar", "baz", "0"]);
expect(qs.push(field, 1))
.toEqual({foo: {bar: {baz: [1]}}});
});
test("Array", () => {
const qs = new QueryString();
const map: [string, any][] = [
["foo[0]", 1],
["foo[1]", 2],
["foo[2]", 3],
];
for (const [field, value] of map) {
qs.push(field, value);
}
expect(qs.toObject()).toEqual({foo: [1, 2, 3]});
});
test("Nested array", () => {
const qs = new QueryString();
const map: [string, any][] = [
["foo[0][a]", 1],
["foo[0][b]", 2],
["foo[1][c]", 3],
["foo[1][d]", 4],
];
for (const [field, value] of map) {
qs.push(field, value);
}
expect(qs.toObject()).toEqual({foo: [{a: 1, b: 2}, {c: 3, d: 4}]});
});
test.each([
["foo", 1, {foo: 1}],
["foo[bar]", 1, {foo: {bar: 1}}],
["foo[bar][0]", 1, {foo: {bar: [1]}}],
["foo[0][bar]", 1, {foo: [{bar: 1}]}],
])("Variants", (field, value, res) => {
const qs = new QueryString([[field, value]]);
expect(qs.toObject()).toEqual(res);
});
});