1+ import { NextModeTagCache } from "@opennextjs/aws/types/overrides" ;
2+ import { beforeEach , describe , expect , it , vi } from "vitest" ;
3+
4+ import { softTagFilter , withFilter } from "./tag-cache-filter" ;
5+
6+ const mockedTagCache = {
7+ name : "mocked" ,
8+ mode : "nextMode" ,
9+ getPathsByTags : vi . fn ( ) ,
10+ hasBeenRevalidated : vi . fn ( ) ,
11+ writeTags : vi . fn ( ) ,
12+ } satisfies NextModeTagCache ;
13+
14+ const filterFn = ( tag : string ) => tag . startsWith ( "valid_" ) ;
15+
16+ describe ( "withFilter" , ( ) => {
17+ beforeEach ( ( ) => {
18+ vi . clearAllMocks ( ) ;
19+ } ) ;
20+
21+ it ( "should filter out tags based on writeTags" , async ( ) => {
22+ const tagCache = withFilter ( {
23+ originalTagCache : mockedTagCache ,
24+ filterFn,
25+ } ) ;
26+
27+ const tags = [ "valid_tag" , "invalid_tag" ] ;
28+
29+ await tagCache . writeTags ( tags ) ;
30+ expect ( mockedTagCache . writeTags ) . toHaveBeenCalledWith ( [ "valid_tag" ] ) ;
31+ } )
32+
33+ it ( 'should not call writeTags if no tags are valid' , async ( ) => {
34+ const tagCache = withFilter ( {
35+ originalTagCache : mockedTagCache ,
36+ filterFn,
37+ } ) ;
38+ const tags = [ "invalid_tag" ] ;
39+ await tagCache . writeTags ( tags ) ;
40+ expect ( mockedTagCache . writeTags ) . not . toHaveBeenCalled ( ) ;
41+ } )
42+
43+ it ( "should filter out tags based on hasBeenRevalidated" , async ( ) => {
44+ const tagCache = withFilter ( {
45+ originalTagCache : mockedTagCache ,
46+ filterFn,
47+ } ) ;
48+
49+ const tags = [ "valid_tag" , "invalid_tag" ] ;
50+ const lastModified = Date . now ( ) ;
51+
52+ await tagCache . hasBeenRevalidated ( tags , lastModified ) ;
53+ expect ( mockedTagCache . hasBeenRevalidated ) . toHaveBeenCalledWith ( [ "valid_tag" ] , lastModified ) ;
54+ }
55+ )
56+
57+ it ( 'should not call hasBeenRevalidated if no tags are valid' , async ( ) => {
58+ const tagCache = withFilter ( {
59+ originalTagCache : mockedTagCache ,
60+ filterFn,
61+ } ) ;
62+ const tags = [ "invalid_tag" ] ;
63+ const lastModified = Date . now ( ) ;
64+ await tagCache . hasBeenRevalidated ( tags , lastModified ) ;
65+ expect ( mockedTagCache . hasBeenRevalidated ) . not . toHaveBeenCalled ( ) ;
66+ } )
67+
68+ it ( "should filter out tags based on getPathsByTags" , async ( ) => {
69+ const tagCache = withFilter ( {
70+ originalTagCache : mockedTagCache ,
71+ filterFn,
72+ } ) ;
73+
74+ const tags = [ "valid_tag" , "invalid_tag" ] ;
75+
76+ await tagCache . getPathsByTags ?.( tags ) ;
77+ expect ( mockedTagCache . getPathsByTags ) . toHaveBeenCalledWith ( [ "valid_tag" ] ) ;
78+ }
79+ )
80+
81+ it ( 'should not call getPathsByTags if no tags are valid' , async ( ) => {
82+ const tagCache = withFilter ( {
83+ originalTagCache : mockedTagCache ,
84+ filterFn,
85+ } ) ;
86+ const tags = [ "invalid_tag" ] ;
87+ await tagCache . getPathsByTags ?.( tags ) ;
88+ expect ( mockedTagCache . getPathsByTags ) . not . toHaveBeenCalled ( ) ;
89+ } )
90+
91+ it ( 'should return the correct name' , ( ) => {
92+ const tagCache = withFilter ( {
93+ originalTagCache : mockedTagCache ,
94+ filterFn,
95+ } ) ;
96+
97+ expect ( tagCache . name ) . toBe ( 'filtered-mocked' ) ;
98+ }
99+ )
100+
101+ it ( "should not create a function if getPathsByTags is not defined" , async ( ) => {
102+ const tagCache = withFilter ( {
103+ originalTagCache : {
104+ ...mockedTagCache ,
105+ getPathsByTags : undefined ,
106+ } ,
107+ filterFn,
108+ } ) ;
109+
110+ expect ( tagCache . getPathsByTags ) . toBeUndefined ( ) ;
111+ }
112+ )
113+
114+ it ( "should properly filter soft tags" , ( ) => {
115+ const tagCache = withFilter ( {
116+ originalTagCache : mockedTagCache ,
117+ filterFn : softTagFilter ,
118+ } ) ;
119+
120+ tagCache . writeTags ( [ "valid_tag" , "_N_T_/" , "_N_T_/test" , "_N_T_/layout" ] ) ;
121+ expect ( mockedTagCache . writeTags ) . toHaveBeenCalledWith ( [ "valid_tag" ] ) ;
122+ } )
123+ } ) ;
0 commit comments