-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSerializer.spec.ts
More file actions
61 lines (61 loc) · 2.03 KB
/
Serializer.spec.ts
File metadata and controls
61 lines (61 loc) · 2.03 KB
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
import { Blob } from "fetch-blob"
import { File } from "fetch-blob/file"
import { FormData as Form } from "formdata-polyfill/esm.min.js"
import { Serializer } from "."
globalThis.FormData = Form
globalThis.Blob = Blob
globalThis.File = File
describe("serializer", () => {
it("send standard form data", async () => {
const data = new FormData()
const file = new Blob([JSON.stringify({ test: "testing", tester: "potato" }, null, 2)], { type: "application/pdf" })
data.append("request", "value1")
data.append("remittanceAdvice", file)
const result = await Serializer.serialize(data, "multipart/form-data")
expect(result).toEqual(data)
})
it("FormData.to", async () => {
const data = { a: 123, b: "qwe", c: false, d: null, e: [123, 456], f: { g: 789 } }
const result = await Serializer.serialize(data, "multipart/form-data")
expect(result instanceof FormData).toBeTruthy()
})
it("FormData.to", async () => {
const data = {
a: 123,
b: {
c: new Blob([new TextEncoder().encode("The Power of Attraction.")], { type: "application/octet-stream" }),
c1: "attraction",
},
d: new Blob([new TextEncoder().encode("The Power of Attraction.")], { type: "application/octet-stream" }),
}
const serialized = await Serializer.serialize(data, "multipart/form-data")
const result = Object.fromEntries((serialized as FormData).entries())
expect(
Object.fromEntries(
await Promise.all(
Object.entries(result).map(async ([property, value]) => [
property,
!(value instanceof File)
? value
: value.type.startsWith("application/json")
? JSON.parse(await value.text())
: new Array(...new Uint8Array(await value.arrayBuffer())),
])
)
)
).toEqual({
"*": {
a: 123,
b: {
c1: "attraction",
},
},
"b.c": [
84, 104, 101, 32, 80, 111, 119, 101, 114, 32, 111, 102, 32, 65, 116, 116, 114, 97, 99, 116, 105, 111, 110, 46,
],
d: [
84, 104, 101, 32, 80, 111, 119, 101, 114, 32, 111, 102, 32, 65, 116, 116, 114, 97, 99, 116, 105, 111, 110, 46,
],
})
})
})