-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.ts
61 lines (57 loc) · 1.57 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
// deno-lint-ignore-file require-await
import { MongoClient, ObjectId, UUID } from "./mod.ts";
import { assertEquals } from "./test_deps.ts";
Deno.test("Sample Test", async () => {
const fetchMock = Promise.withResolvers<{ url: string; init: RequestInit }>();
const client = new MongoClient({
endpoint: "https://data.mongodb-api.com/app/data-abc/endpoint/data/v1",
dataSource: "dataSource",
auth: {
apiKey: "API_KEY",
},
fetch: (async (url: string, init: RequestInit) => {
fetchMock.resolve({ url, init });
return {
ok: true,
text: async () => JSON.stringify({ ok: true }),
};
}) as typeof fetch,
});
const _id = new ObjectId();
client
.database("db-name")
.collection("c-name")
.insertOne({
_id,
foo: "bar",
uuid: new UUID("408ebbdc-2651-4aa4-8298-3aef14e78f7e"),
});
const { url, init } = await fetchMock.promise;
assertEquals(
url,
"https://data.mongodb-api.com/app/data-abc/endpoint/data/v1/action/insertOne",
);
assertEquals(init.method, "POST");
assertEquals(
new Headers(init.headers).get("Content-Type"),
"application/ejson",
);
assertEquals(new Headers(init.headers).get("api-key"), "API_KEY");
assertEquals(await new Request(url, init).json(), {
collection: "c-name",
database: "db-name",
dataSource: "dataSource",
document: {
_id: {
$oid: _id.toHexString(),
},
foo: "bar",
uuid: {
$binary: {
base64: "QI673CZRSqSCmDrvFOePfg==",
subType: "04",
},
},
},
});
});