@@ -6,6 +6,11 @@ import DataLoaderProvider, { useDataLoaderContext } from '../DataLoaderProvider'
66const wrapper = ( { children } ) => (
77 < DataLoaderProvider > { children } </ DataLoaderProvider >
88)
9+
10+ const wrapperWithCacheKey = ( { children } ) => (
11+ < DataLoaderProvider cacheKeyPrefix = "sample" > { children } </ DataLoaderProvider >
12+ )
13+
914describe ( 'DataLoaderProvider' , ( ) => {
1015 test ( 'should render correctly' , async ( ) => {
1116 render ( < DataLoaderProvider > Test</ DataLoaderProvider > )
@@ -23,6 +28,22 @@ describe('DataLoaderProvider', () => {
2328 expect ( Object . values ( result . current . getCachedData ( ) ) . length ) . toBe ( 1 )
2429 expect ( result . current . getCachedData ( ) . test ) . toBe ( 'test' )
2530 } )
31+
32+ test ( 'should add cached data with cacheKeyPrefix' , async ( ) => {
33+ const { result } = renderHook ( useDataLoaderContext , {
34+ wrapper : wrapperWithCacheKey ,
35+ } )
36+ expect ( result . current . getCachedData ( ) ) . toStrictEqual ( { } )
37+
38+ act ( ( ) => {
39+ result . current . addCachedData ( 'test' , 'test' )
40+ result . current . addCachedData ( 3 , 'testWrong' )
41+ } )
42+
43+ expect ( Object . values ( result . current . getCachedData ( ) ) . length ) . toBe ( 1 )
44+ expect ( result . current . getCachedData ( ) [ 'sample-test' ] ) . toBe ( 'test' )
45+ } )
46+
2647 test ( 'should delete cached data' , async ( ) => {
2748 const { result } = renderHook ( useDataLoaderContext , { wrapper } )
2849
@@ -49,6 +70,34 @@ describe('DataLoaderProvider', () => {
4970 expect ( result . current . getCachedData ( ) ) . toStrictEqual ( { } )
5071 } )
5172
73+ test ( 'should delete cached data with cacheKeyPrefix' , async ( ) => {
74+ const { result } = renderHook ( useDataLoaderContext , {
75+ wrapper : wrapperWithCacheKey ,
76+ } )
77+
78+ act ( ( ) => {
79+ result . current . addCachedData ( 'testA' , 'testA' )
80+ result . current . addCachedData ( 'testB' , 'testB' )
81+ result . current . addCachedData ( 'testC' , 'testC' )
82+ result . current . addCachedData ( 'testD' , 'testD' )
83+ result . current . addCachedData ( 'testE' , 'testE' )
84+ } )
85+ expect ( result . current . getCachedData ( 'testA' ) ) . toBe ( 'testA' )
86+
87+ act ( ( ) => {
88+ result . current . clearCachedData ( )
89+ result . current . clearCachedData ( 'testA' )
90+ } )
91+ expect ( Object . values ( result . current . getCachedData ( ) ) . length ) . toBe ( 4 )
92+ expect ( result . current . getCachedData ( ) . testA ) . toBe ( undefined )
93+
94+ act ( ( ) => {
95+ result . current . clearAllCachedData ( )
96+ } )
97+ expect ( Object . values ( result . current . getCachedData ( ) ) . length ) . toBe ( 0 )
98+ expect ( result . current . getCachedData ( ) ) . toStrictEqual ( { } )
99+ } )
100+
52101 test ( 'should get cached data' , async ( ) => {
53102 const { result } = renderHook ( useDataLoaderContext , { wrapper } )
54103 expect ( result . current . getCachedData ( ) ) . toStrictEqual ( { } )
0 commit comments