Skip to content

Commit c36a1d0

Browse files
Merge pull request #76 from storybookjs/copilot/fix-cachelife-export-error
Add cacheLife and cacheTag mocks to next/cache
2 parents 71a20b1 + 697aca7 commit c36a1d0

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
3-
.env
3+
.env
4+
package-lock.json

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# dependencies
44
/node_modules
5+
package-lock.json
56
/.pnp
67
.pnp.js
78
.yarn/install-state.gz
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
5+
import type { Meta, StoryObj } from "@storybook/nextjs-vite";
6+
import { cacheLife, cacheTag } from "@storybook/nextjs-vite/cache.mock";
7+
import { expect } from "storybook/test";
8+
import CacheComponent from "./Cache";
9+
10+
export default {
11+
component: CacheComponent,
12+
parameters: {
13+
layout: "centered",
14+
},
15+
} as Meta<typeof CacheComponent>;
16+
17+
type Story = StoryObj<typeof CacheComponent>;
18+
19+
export const Default: Story = {
20+
args: {
21+
profile: "default",
22+
tags: ["my-tag"],
23+
},
24+
play: async () => {
25+
await expect(cacheLife).toHaveBeenCalledWith("default");
26+
await expect(cacheTag).toHaveBeenCalledWith("my-tag");
27+
},
28+
};
29+
30+
export const WithDaysProfile: Story = {
31+
args: {
32+
profile: "days",
33+
tags: ["posts", "user-posts"],
34+
},
35+
play: async () => {
36+
await expect(cacheLife).toHaveBeenCalledWith("days");
37+
await expect(cacheTag).toHaveBeenCalledWith("posts");
38+
await expect(cacheTag).toHaveBeenCalledWith("user-posts");
39+
},
40+
};
41+
42+
export const WithWeeksProfile: Story = {
43+
args: {
44+
profile: "weeks",
45+
tags: ["static-content"],
46+
},
47+
play: async () => {
48+
await expect(cacheLife).toHaveBeenCalledWith("weeks");
49+
await expect(cacheTag).toHaveBeenCalledWith("static-content");
50+
},
51+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { cacheLife, cacheTag } from "next/cache";
2+
3+
interface CacheComponentProps {
4+
profile?:
5+
| "default"
6+
| "seconds"
7+
| "minutes"
8+
| "hours"
9+
| "days"
10+
| "weeks"
11+
| "max";
12+
tags?: string[];
13+
}
14+
15+
export default function CacheComponent({
16+
profile = "default",
17+
tags = ["my-tag"],
18+
}: CacheComponentProps) {
19+
// Apply cache life profile
20+
cacheLife(profile);
21+
22+
// Apply cache tags
23+
for (const tag of tags) {
24+
cacheTag(tag);
25+
}
26+
27+
return (
28+
<div>
29+
<h3>Cache Component</h3>
30+
<p>
31+
<strong>Cache Life Profile:</strong> {profile}
32+
</p>
33+
<p>
34+
<strong>Cache Tags:</strong> {tags.join(", ")}
35+
</p>
36+
</div>
37+
);
38+
}

src/plugins/next-mocks/alias/cache/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,29 @@ const unstable_noStore: Mock<Procedure> = fn().mockName(
2323
);
2424
const refresh: Mock<Procedure> = fn().mockName("next/cache::refresh");
2525

26+
// mock utilities/overrides (as of Next v15.0.0)
27+
const cacheLife: Mock<Procedure> = fn().mockName("next/cache::cacheLife");
28+
const cacheTag: Mock<Procedure> = fn().mockName("next/cache::cacheTag");
29+
30+
// deprecated wrappers (as of Next v16.0.0)
31+
const unstable_cacheLife: Mock<Procedure> = fn()
32+
.mockName("next/cache::unstable_cacheLife")
33+
.mockImplementation((...args) => cacheLife(...args));
34+
const unstable_cacheTag: Mock<Procedure> = fn()
35+
.mockName("next/cache::unstable_cacheTag")
36+
.mockImplementation((...args) => cacheTag(...args));
37+
2638
const cacheExports = {
2739
unstable_cache,
2840
revalidateTag,
2941
revalidatePath,
3042
updateTag,
3143
refresh,
3244
unstable_noStore,
45+
cacheLife,
46+
cacheTag,
47+
unstable_cacheLife,
48+
unstable_cacheTag,
3349
};
3450

3551
export default cacheExports;
@@ -40,4 +56,8 @@ export {
4056
unstable_noStore,
4157
refresh,
4258
updateTag,
59+
cacheLife,
60+
cacheTag,
61+
unstable_cacheLife,
62+
unstable_cacheTag,
4363
};

0 commit comments

Comments
 (0)