-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
fetchOptionsGraphQL.test.mjs
57 lines (47 loc) · 1.69 KB
/
fetchOptionsGraphQL.test.mjs
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
// @ts-check
import "./test/polyfillFile.mjs";
import { deepStrictEqual, strictEqual } from "node:assert";
import { describe, it } from "node:test";
import fetchOptionsGraphQL from "./fetchOptionsGraphQL.mjs";
import assertBundleSize from "./test/assertBundleSize.mjs";
import assertInstanceOf from "./test/assertInstanceOf.mjs";
describe("Function `fetchOptionsGraphQL`.", { concurrency: true }, () => {
it("Bundle size.", async () => {
await assertBundleSize(
new URL("./fetchOptionsGraphQL.mjs", import.meta.url),
800,
);
});
it("Without files.", () => {
deepStrictEqual(fetchOptionsGraphQL({ query: "" }), {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: '{"query":""}',
});
});
it("With files.", () => {
const fileName = "a.txt";
const options = fetchOptionsGraphQL({
query: "",
variables: { a: new File(["a"], fileName) },
});
// See the GraphQL multipart request spec:
// https://github.com/jaydenseric/graphql-multipart-request-spec
strictEqual(options.method, "POST");
deepStrictEqual(options.headers, { Accept: "application/json" });
assertInstanceOf(options.body, FormData);
const formDataEntries = Array.from(options.body.entries());
strictEqual(formDataEntries.length, 3);
deepStrictEqual(formDataEntries[0], [
"operations",
'{"query":"","variables":{"a":null}}',
]);
deepStrictEqual(formDataEntries[1], ["map", '{"1":["variables.a"]}']);
strictEqual(formDataEntries[2][0], "1");
assertInstanceOf(formDataEntries[2][1], File);
strictEqual(formDataEntries[2][1].name, fileName);
});
});