Skip to content

Commit 2f3cd20

Browse files
committed
Add tests for cache adapter
1 parent 2c2b343 commit 2f3cd20

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"**/.turbo": true
44
},
55
"tailwindCSS.classAttributes": ["class", "className", "tw"],
6-
"prettier.trailingComma": "all"
6+
"prettier.trailingComma": "all",
7+
"editor.formatOnSave": true,
78
}

packages/tests-unit/tests/adapters/cache.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { hasCacheExtension } from "@opennextjs/aws/adapters/cache.js";
1+
import S3Cache, { hasCacheExtension } from "@opennextjs/aws/adapters/cache.js";
2+
import { vi } from "vitest";
23

34
describe("hasCacheExtension", () => {
45
it("Should returns true if has an extension and it is a CacheExtension", () => {
@@ -13,3 +14,58 @@ describe("hasCacheExtension", () => {
1314
expect(hasCacheExtension("hello,json")).toBeFalsy();
1415
});
1516
});
17+
18+
describe("S3Cache", () => {
19+
let cache: S3Cache;
20+
21+
const getFetchCacheSpy = vi.spyOn(S3Cache.prototype, "getFetchCache");
22+
const getIncrementalCache = vi.spyOn(
23+
S3Cache.prototype,
24+
"getIncrementalCache",
25+
);
26+
27+
beforeEach(() => {
28+
cache = new S3Cache({
29+
_appDir: false,
30+
_requestHeaders: undefined as never,
31+
});
32+
33+
globalThis.disableIncrementalCache = false;
34+
vi.clearAllMocks();
35+
});
36+
37+
it("Should return null when incremental cache is disabled", async () => {
38+
globalThis.disableIncrementalCache = true;
39+
40+
const result = await cache.get("key");
41+
42+
expect(result).toBeNull();
43+
});
44+
45+
it("Should return null for cache miss", async () => {
46+
const result = await cache.get("key");
47+
48+
expect(result).toBeNull();
49+
});
50+
51+
it("Should retrieve cache from fetch cache when fetch cache is true", async () => {
52+
await cache.get("key", { fetchCache: true });
53+
54+
expect(getFetchCacheSpy).toHaveBeenCalled();
55+
});
56+
57+
it("Should retrieve cache from fetch cache when hint is fetch", async () => {
58+
await cache.get("key", { kindHint: "fetch" });
59+
60+
expect(getFetchCacheSpy).toHaveBeenCalled();
61+
});
62+
63+
it.each(["app", "page", undefined])(
64+
"Should retrieve cache from incremental cache when hint is not fetch: %s",
65+
async (kindHint) => {
66+
await cache.get("key", { kindHint: kindHint as any });
67+
68+
expect(getIncrementalCache).toHaveBeenCalled();
69+
},
70+
);
71+
});

0 commit comments

Comments
 (0)