File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
__tests__/unit/controllers/resource-store
controllers/resource-store Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -903,6 +903,36 @@ describe('resource store', () => {
903
903
expect ( spy ) . toBeCalledTimes ( 0 ) ;
904
904
} ) ;
905
905
} ) ;
906
+
907
+ it ( 'should set expiresAt to pending state if prefetch' , async ( ) => {
908
+ ( getDefaultStateSlice as any ) . mockImplementation ( ( ) => ( {
909
+ ...BASE_DEFAULT_STATE_SLICE ,
910
+ } ) ) ;
911
+ ( getExpiresAt as any ) . mockImplementation ( ( ) => expiresAt ) ;
912
+
913
+ const spy = jest . spyOn ( storeState , 'setState' ) ;
914
+
915
+ await actions . getResourceFromRemote (
916
+ mockResource ,
917
+ mockRouterStoreContext ,
918
+ { ...mockOptions , prefetch : true }
919
+ ) ;
920
+
921
+ expect ( spy ) . toHaveBeenNthCalledWith ( 1 , {
922
+ context : { } ,
923
+ data : {
924
+ [ type ] : {
925
+ [ key ] : {
926
+ ...BASE_DEFAULT_STATE_SLICE ,
927
+ loading : true ,
928
+ accessedAt : undefined ,
929
+ expiresAt,
930
+ promise : expect . any ( Promise ) ,
931
+ } ,
932
+ } ,
933
+ } ,
934
+ } ) ;
935
+ } ) ;
906
936
} ) ;
907
937
908
938
describe ( 'cleanExpiredResources' , ( ) => {
Original file line number Diff line number Diff line change @@ -127,6 +127,12 @@ export const actions: Actions = {
127
127
context
128
128
) ,
129
129
accessedAt : getAccessedAt ( ) ,
130
+ // prevent resource from being cleaned prematurely and traigger more network requests.
131
+ ...( options . prefetch
132
+ ? {
133
+ expiresAt : getExpiresAt ( PREFETCH_MAX_AGE ) ,
134
+ }
135
+ : { } ) ,
130
136
} ;
131
137
132
138
dispatch ( setResourceState ( type , key , pending ) ) ;
You can’t perform that action at this time.
0 commit comments