File tree Expand file tree Collapse file tree 5 files changed +112
-1
lines changed
src/plugins/next-mocks/alias/cache Expand file tree Collapse file tree 5 files changed +112
-1
lines changed Original file line number Diff line number Diff line change 11node_modules
22dist
3- .env
3+ .env
4+ package-lock.json
Original file line number Diff line number Diff line change 22
33# dependencies
44/node_modules
5+ package-lock.json
56/.pnp
67.pnp.js
78.yarn /install-state.gz
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -23,13 +23,29 @@ const unstable_noStore: Mock<Procedure> = fn().mockName(
2323) ;
2424const 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+
2638const 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
3551export 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} ;
You can’t perform that action at this time.
0 commit comments