-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
131 lines (110 loc) · 3.99 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { assertEquals } from "@std/assert";
import { encode, decode } from "./mod.ts";
Deno.test("encode() handles simple data", () => {
const data = { email: "john@doe.com", password: "johndoe123" };
const actual = encode(data);
const expected = new FormData();
expected.append("email", "john@doe.com");
expected.append("password", "johndoe123");
assertEquals(actual, expected);
});
Deno.test("encode() omits null and undefined values", () => {
const data = { email: null, password: undefined };
const actual = encode(data);
assertEquals(actual, new FormData());
});
Deno.test("encode() handles primitives as strings", () => {
const actual = encode({ amount: 10 });
const expected = new FormData();
expected.append("amount", "10");
assertEquals(actual, expected);
});
Deno.test("encode() handles arrays", () => {
const data = { tags: ["foo", "bar"] };
const actual = encode(data);
const expected = new FormData();
expected.append("tags[0]", "foo");
expected.append("tags[1]", "bar");
assertEquals(actual, expected);
});
Deno.test("encode() handles complex data", () => {
const data = { address: { street: "john doe st.", tags: ["foo", "bar"] } };
const actual = encode(data);
const expected = new FormData();
expected.append("address[street]", "john doe st.");
expected.append("address[tags][0]", "foo");
expected.append("address[tags][1]", "bar");
assertEquals(actual, expected);
});
Deno.test("decode() handles simple FormData", () => {
const formData = new FormData();
formData.append("email", "john@doe.com");
formData.append("password", "johndoe123");
const actual = decode(formData);
const expected = { email: "john@doe.com", password: "johndoe123" };
assertEquals(actual, expected);
});
Deno.test("decode() handles primitive values", () => {
const formData = new FormData();
formData.append("amount", "10");
const actual = decode(formData);
const expected = { amount: "10" };
assertEquals(actual, expected);
});
Deno.test("decode() handles arrays", () => {
const formData = new FormData();
formData.append("tags[0]", "foo");
formData.append("tags[1]", "bar");
const actual = decode(formData);
const expected = { tags: ["foo", "bar"] };
assertEquals(actual, expected);
});
Deno.test("decode() handles complex nested data", () => {
const formData = new FormData();
formData.append("address[street]", "john doe st.");
formData.append("address[tags][0]", "foo");
formData.append("address[tags][1]", "bar");
const actual = decode(formData);
const expected = {
address: { street: "john doe st.", tags: ["foo", "bar"] },
};
assertEquals(actual, expected);
});
Deno.test("decode() handles nested objects inside arrays", () => {
const formData = new FormData();
formData.append("users[0][name]", "john doe");
formData.append("users[1][name]", "jane doe");
formData.append("users[0][tags][0]", "foo");
const actual = decode(formData);
const expected = {
users: [{ name: "john doe", tags: ["foo"] }, { name: "jane doe" }],
};
assertEquals(actual, expected);
});
Deno.test("decode() uses `keep` option", () => {
const formData = new FormData();
formData.append("id", "1");
formData.append("name", "");
formData.append("address[street]", " ");
const actual = decode(formData, { emptyString: "keep" });
const expected = { id: "1", name: "", address: { street: "" } };
assertEquals(actual, expected);
});
Deno.test("decode() uses `set null` option", () => {
const formData = new FormData();
formData.append("id", "1");
formData.append("name", "");
formData.append("address[street]", " ");
const actual = decode(formData, { emptyString: "set null" });
const expected = { id: "1", name: null, address: { street: null } };
assertEquals(actual, expected);
});
Deno.test("decode() uses `set undefined` option", () => {
const formData = new FormData();
formData.append("id", "1");
formData.append("name", "");
formData.append("address[street]", " ");
const actual = decode(formData, { emptyString: "set undefined" });
const expected = { id: "1", address: {} };
assertEquals(actual, expected);
});