11import type { EntityPath } from '@data-client/normalizr' ;
22import { jest } from '@jest/globals' ;
3+ import { has } from 'benchmark' ;
34
45import { GC } from '../../actionTypes' ;
56import Controller from '../../controller/Controller' ;
@@ -40,8 +41,22 @@ describe('GCPolicy', () => {
4041 const key = 'testEndpoint' ;
4142 const paths : EntityPath [ ] = [ { key : 'testEntity' , pk : '1' } ] ;
4243 const state = {
43- meta : { testEndpoint : { expiresAt : Date . now ( ) - 1000 } } ,
44- entityMeta : { testEntity : { '1' : { expiresAt : Date . now ( ) - 1000 } } } ,
44+ meta : {
45+ testEndpoint : {
46+ date : 0 ,
47+ fetchedAt : 0 ,
48+ expiresAt : 0 ,
49+ } ,
50+ } ,
51+ entityMeta : {
52+ testEntity : {
53+ '1' : {
54+ date : 0 ,
55+ fetchedAt : 0 ,
56+ expiresAt : 0 ,
57+ } ,
58+ } ,
59+ } ,
4560 } ;
4661 ( controller . getState as jest . Mock ) . mockReturnValue ( state ) ;
4762
@@ -68,8 +83,22 @@ describe('GCPolicy', () => {
6883 const key = 'testEndpoint' ;
6984 const paths : EntityPath [ ] = [ { key : 'testEntity' , pk : '1' } ] ;
7085 const state = {
71- meta : { testEndpoint : { expiresAt : Date . now ( ) - 1000 } } ,
72- entityMeta : { testEntity : { '1' : { expiresAt : Date . now ( ) - 1000 } } } ,
86+ meta : {
87+ testEndpoint : {
88+ date : 0 ,
89+ fetchedAt : 0 ,
90+ expiresAt : 0 ,
91+ } ,
92+ } ,
93+ entityMeta : {
94+ testEntity : {
95+ '1' : {
96+ date : 0 ,
97+ fetchedAt : 0 ,
98+ expiresAt : 0 ,
99+ } ,
100+ } ,
101+ } ,
73102 } ;
74103 ( controller . getState as jest . Mock ) . mockReturnValue ( state ) ;
75104
@@ -127,8 +156,22 @@ describe('GCPolicy', () => {
127156 const paths : EntityPath [ ] = [ { key : 'testEntity' , pk : '1' } ] ;
128157 const futureTime = Date . now ( ) + 1000 ;
129158 const state = {
130- meta : { testEndpoint : { expiresAt : futureTime } } ,
131- entityMeta : { testEntity : { '1' : { expiresAt : futureTime } } } ,
159+ meta : {
160+ testEndpoint : {
161+ date : futureTime - 100 ,
162+ fetchAt : futureTime - 100 ,
163+ expiresAt : futureTime ,
164+ } ,
165+ } ,
166+ entityMeta : {
167+ testEntity : {
168+ '1' : {
169+ date : futureTime - 100 ,
170+ fetchAt : futureTime - 100 ,
171+ expiresAt : futureTime ,
172+ } ,
173+ } ,
174+ } ,
132175 } ;
133176 ( controller . getState as jest . Mock ) . mockReturnValue ( state ) ;
134177
@@ -146,8 +189,22 @@ describe('GCPolicy', () => {
146189 // Fast forward time to past the futureTime
147190 jest . advanceTimersByTime ( 2000 ) ;
148191 ( controller . getState as jest . Mock ) . mockReturnValue ( {
149- meta : { testEndpoint : { expiresAt : Date . now ( ) - 1000 } } ,
150- entityMeta : { testEntity : { '1' : { expiresAt : Date . now ( ) - 1000 } } } ,
192+ meta : {
193+ testEndpoint : {
194+ date : 0 ,
195+ fetchedAt : 0 ,
196+ expiresAt : 0 ,
197+ } ,
198+ } ,
199+ entityMeta : {
200+ testEntity : {
201+ '1' : {
202+ date : 0 ,
203+ fetchedAt : 0 ,
204+ expiresAt : 0 ,
205+ } ,
206+ } ,
207+ } ,
151208 } ) ;
152209
153210 gcPolicy [ 'runSweep' ] ( ) ;
@@ -160,4 +217,42 @@ describe('GCPolicy', () => {
160217
161218 jest . useRealTimers ( ) ;
162219 } ) ;
220+
221+ it ( 'should support custom hasExpired' , ( ) => {
222+ jest . useFakeTimers ( ) ;
223+ gcPolicy = new GCPolicy ( { expiresAt : ( ) => 0 } ) ;
224+ gcPolicy . init ( controller ) ;
225+ const key = 'testEndpoint' ;
226+ const paths : EntityPath [ ] = [ { key : 'testEntity' , pk : '1' } ] ;
227+ const futureTime = Date . now ( ) + 1000 ;
228+ const state = {
229+ meta : {
230+ testEndpoint : {
231+ date : futureTime - 100 ,
232+ fetchAt : futureTime - 100 ,
233+ expiresAt : futureTime ,
234+ } ,
235+ } ,
236+ entityMeta : {
237+ testEntity : {
238+ '1' : {
239+ date : futureTime - 100 ,
240+ fetchAt : futureTime - 100 ,
241+ expiresAt : futureTime ,
242+ } ,
243+ } ,
244+ } ,
245+ } ;
246+ ( controller . getState as jest . Mock ) . mockReturnValue ( state ) ;
247+
248+ const countRef = gcPolicy . createCountRef ( { key, paths } ) ;
249+
250+ const decrement = countRef ( ) ;
251+ countRef ( ) ; // Increment again
252+ decrement ( ) ;
253+ decrement ( ) ; // Decrement twice
254+
255+ gcPolicy [ 'runSweep' ] ( ) ;
256+ expect ( controller . dispatch ) . toHaveBeenCalled ( ) ;
257+ } ) ;
163258} ) ;
0 commit comments