-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirect-mock.test.ts
47 lines (39 loc) · 1.65 KB
/
direct-mock.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
"use strict";
import { describe, it } from "jsr:@std/testing/bdd";
import { spy } from "jsr:@std/testing/mock";
import { expect } from "jsr:@std/expect";
// import createFetchCache from "fetch-mock-cache/runtimes/deno.ts"
// import MemoryStore from "fetch-mock-cache/stores/memory.ts";
import createFetchCache from "../../../../src/runtimes/deno.ts";
import MemoryStore from "../../../../src/stores/memory.ts";
import FsStore from "../../../../src/stores/fs.ts";
describe("deno - direct mock", () => {
const url = "http://echo.jsontest.com/key/value/one/two";
const expectedResponse = { one: "two", key: "value" };
it("memoryStore", async () => {
const fetchCache = createFetchCache({ Store: MemoryStore });
const originalFetch = globalThis.fetch;
globalThis.fetch = spy(fetchCache);
for (let i = 0; i < 2; i++) {
const response = await fetch(url);
const data = await response.json();
const expectedCacheHeader = i === 0 ? "MISS" : "HIT";
expect(response.headers.get("X-FMC-Cache")).toBe(expectedCacheHeader);
expect(data).toEqual(expectedResponse);
}
globalThis.fetch = originalFetch;
});
it("fs store", async () => {
const fetchCache = createFetchCache({ Store: FsStore });
const originalFetch = globalThis.fetch;
globalThis.fetch = spy(fetchCache);
for (let i = 0; i < 2; i++) {
const response = await fetch(url);
const data = await response.json();
const expectedCacheHeader = i === 0 ? "MISS" : "HIT";
expect(response.headers.get("X-FMC-Cache")).toBe(expectedCacheHeader);
expect(data).toEqual(expectedResponse);
}
globalThis.fetch = originalFetch;
});
});