Skip to content

Commit f8f328a

Browse files
committed
updated test
1 parent 4e25474 commit f8f328a

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ describe("CacheHandler", () => {
3232

3333
const tagCache = {
3434
name: "mock",
35+
mode: "original",
36+
hasBeenRevalidated: vi.fn(),
3537
getByTag: vi.fn(),
3638
getByPath: vi.fn(),
3739
getLastModified: vi
@@ -143,6 +145,17 @@ describe("CacheHandler", () => {
143145
expect(result).toBeNull();
144146
});
145147

148+
it("Should return null with nextMode tag cache that has been revalidated", async () => {
149+
tagCache.mode = "nextMode";
150+
tagCache.hasBeenRevalidated.mockResolvedValueOnce(true);
151+
152+
const result = await cache.get("key", { kind: "FETCH" });
153+
expect(getFetchCacheSpy).toHaveBeenCalled();
154+
expect(result).toBeNull();
155+
// Reset the tagCache mode
156+
tagCache.mode = "original";
157+
});
158+
146159
it("Should return null when incremental cache throws", async () => {
147160
incrementalCache.get.mockRejectedValueOnce(
148161
new Error("Error retrieving cache"),
@@ -181,6 +194,24 @@ describe("CacheHandler", () => {
181194
expect(result).toBeNull();
182195
});
183196

197+
it("Should return null with nextMode tag cache that has been revalidated", async () => {
198+
tagCache.mode = "nextMode";
199+
tagCache.hasBeenRevalidated.mockResolvedValueOnce(true);
200+
incrementalCache.get.mockResolvedValueOnce({
201+
value: {
202+
type: "route",
203+
},
204+
lastModified: Date.now(),
205+
});
206+
207+
const result = await cache.get("key", { kindHint: "app" });
208+
209+
expect(getIncrementalCache).toHaveBeenCalled();
210+
expect(result).toBeNull();
211+
// Reset the tagCache mode
212+
tagCache.mode = "original";
213+
});
214+
184215
it("Should return value when cache data type is route", async () => {
185216
incrementalCache.get.mockResolvedValueOnce({
186217
value: {
@@ -549,5 +580,39 @@ describe("CacheHandler", () => {
549580

550581
expect(invalidateCdnHandler.invalidatePaths).not.toHaveBeenCalled();
551582
});
583+
584+
it("Should only call writeTags for nextMode", async () => {
585+
globalThis.tagCache.mode = "nextMode";
586+
await cache.revalidateTag(["tag1", "tag2"]);
587+
588+
expect(tagCache.writeTags).toHaveBeenCalledTimes(1);
589+
expect(tagCache.writeTags).toHaveBeenCalledWith(["tag1", "tag2"]);
590+
expect(invalidateCdnHandler.invalidatePaths).not.toHaveBeenCalled();
591+
});
592+
593+
it("Should call writeTags and invalidateCdnHandler.invalidatePaths for nextMode that supports getPathsByTags", async () => {
594+
globalThis.tagCache.mode = "nextMode";
595+
globalThis.tagCache.getPathsByTags = vi
596+
.fn()
597+
.mockResolvedValueOnce(["/path"]);
598+
await cache.revalidateTag("tag");
599+
600+
expect(tagCache.writeTags).toHaveBeenCalledTimes(1);
601+
expect(tagCache.writeTags).toHaveBeenCalledWith(["tag"]);
602+
expect(invalidateCdnHandler.invalidatePaths).toHaveBeenCalledWith([
603+
{
604+
initialPath: "/path",
605+
rawPath: "/path",
606+
resolvedRoutes: [
607+
{
608+
type: "app",
609+
route: "/path",
610+
},
611+
],
612+
},
613+
]);
614+
// Reset the getPathsByTags
615+
globalThis.tagCache.getPathsByTags = undefined;
616+
});
552617
});
553618
});

packages/tests-unit/tests/core/routing/cacheInterceptor.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ globalThis.queue = queue;
8181
beforeEach(() => {
8282
vi.useFakeTimers().setSystemTime("2024-01-02T00:00:00Z");
8383
vi.clearAllMocks();
84+
globalThis.openNextConfig = {
85+
dangerous: {
86+
disableTagCache: false,
87+
disableIncrementalCache: false,
88+
},
89+
};
8490
});
8591

8692
describe("cacheInterceptor", () => {

0 commit comments

Comments
 (0)