Skip to content

Commit 0d5e587

Browse files
authored
fix(use-dataloader): move cacheKeyPrefix logic into hook to trigger reload (#203)
* fix(use-dataloader): move cacheKeyPrefix logic into hook to trigger reload * test: correct tests
1 parent 6444b2e commit 0d5e587

File tree

4 files changed

+71
-71
lines changed

4 files changed

+71
-71
lines changed

packages/use-dataloader/src/DataLoaderProvider.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ const DataLoaderProvider = ({ children, cacheKeyPrefix }) => {
3434
if (key && typeof key === 'string' && newData) {
3535
setCachedData(actualCachedData => ({
3636
...actualCachedData,
37-
[`${cacheKeyPrefix ? `${cacheKeyPrefix}-` : ''}${key}`]: newData,
37+
[key]: newData,
3838
}))
3939
}
4040
},
41-
[setCachedData, cacheKeyPrefix],
41+
[setCachedData],
4242
)
4343

4444
const addReload = useCallback(
@@ -76,13 +76,13 @@ const DataLoaderProvider = ({ children, cacheKeyPrefix }) => {
7676
if (key && typeof key === 'string') {
7777
setCachedData(actualCachedData => {
7878
const tmp = actualCachedData
79-
delete tmp[`${cacheKeyPrefix ? `${cacheKeyPrefix}-` : ''}${key}`]
79+
delete tmp[key]
8080

8181
return tmp
8282
})
8383
}
8484
},
85-
[setCachedData, cacheKeyPrefix],
85+
[setCachedData],
8686
)
8787
const clearAllCachedData = useCallback(() => {
8888
setCachedData({})
@@ -100,20 +100,13 @@ const DataLoaderProvider = ({ children, cacheKeyPrefix }) => {
100100
)
101101
}, [])
102102

103-
const getCachedData = useCallback(
104-
key => {
105-
if (key) {
106-
return (
107-
cachedData.current[
108-
`${cacheKeyPrefix ? `${cacheKeyPrefix}-` : ''}${key}`
109-
] || undefined
110-
)
111-
}
103+
const getCachedData = useCallback(key => {
104+
if (key) {
105+
return cachedData.current[key] || undefined
106+
}
112107

113-
return cachedData.current
114-
},
115-
[cacheKeyPrefix],
116-
)
108+
return cachedData.current
109+
}, [])
117110

118111
const getReloads = useCallback(key => {
119112
if (key) {
@@ -127,6 +120,7 @@ const DataLoaderProvider = ({ children, cacheKeyPrefix }) => {
127120
() => ({
128121
addCachedData,
129122
addReload,
123+
cacheKeyPrefix,
130124
clearAllCachedData,
131125
clearAllReloads,
132126
clearCachedData,
@@ -138,14 +132,15 @@ const DataLoaderProvider = ({ children, cacheKeyPrefix }) => {
138132
}),
139133
[
140134
addCachedData,
141-
clearReload,
135+
addReload,
136+
cacheKeyPrefix,
137+
clearAllCachedData,
138+
clearAllReloads,
142139
clearCachedData,
140+
clearReload,
143141
getCachedData,
144142
getReloads,
145-
addReload,
146143
reload,
147-
clearAllCachedData,
148-
clearAllReloads,
149144
reloadAll,
150145
],
151146
)

packages/use-dataloader/src/__tests__/DataLoaderProvider.test.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ const wrapper = ({ children }) => (
77
<DataLoaderProvider>{children}</DataLoaderProvider>
88
)
99

10-
const wrapperWithCacheKey = ({ children }) => (
11-
<DataLoaderProvider cacheKeyPrefix="sample">{children}</DataLoaderProvider>
12-
)
13-
1410
describe('DataLoaderProvider', () => {
1511
test('should render correctly', async () => {
1612
render(<DataLoaderProvider>Test</DataLoaderProvider>)
@@ -29,21 +25,6 @@ describe('DataLoaderProvider', () => {
2925
expect(result.current.getCachedData().test).toBe('test')
3026
})
3127

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-
4728
test('should delete cached data', async () => {
4829
const { result } = renderHook(useDataLoaderContext, { wrapper })
4930

@@ -70,34 +51,6 @@ describe('DataLoaderProvider', () => {
7051
expect(result.current.getCachedData()).toStrictEqual({})
7152
})
7253

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-
10154
test('should get cached data', async () => {
10255
const { result } = renderHook(useDataLoaderContext, { wrapper })
10356
expect(result.current.getCachedData()).toStrictEqual({})

packages/use-dataloader/src/__tests__/useDataLoader.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const wrapper = ({ children }) => (
1919
<DataLoaderProvider>{children}</DataLoaderProvider>
2020
)
2121

22+
const wrapperWithCacheKey = ({ children }) => (
23+
<DataLoaderProvider cacheKeyPrefix="sample">{children}</DataLoaderProvider>
24+
)
25+
2226
describe('useDataLoader', () => {
2327
test('should render correctly without options', async () => {
2428
const { result, waitForNextUpdate, rerender } = renderHook(
@@ -97,6 +101,40 @@ describe('useDataLoader', () => {
97101
expect(result.current.isLoading).toBe(false)
98102
})
99103

104+
test('should render and cache correctly with cacheKeyPrefix', async () => {
105+
const { result, waitForNextUpdate } = renderHook(
106+
props => [
107+
useDataLoader(props.key, props.method, props.config),
108+
useDataLoader(props.key, props.method, {
109+
...props.config,
110+
enabled: false,
111+
}),
112+
],
113+
{
114+
initialProps,
115+
wrapper: wrapperWithCacheKey,
116+
},
117+
)
118+
119+
expect(result.current[0].data).toBe(undefined)
120+
expect(result.current[0].isLoading).toBe(true)
121+
expect(result.current[1].data).toBe(undefined)
122+
expect(result.current[1].isIdle).toBe(true)
123+
await waitForNextUpdate()
124+
expect(result.current[0].data).toBe(true)
125+
expect(result.current[0].isSuccess).toBe(true)
126+
127+
act(() => {
128+
result.current[1].reload()
129+
})
130+
131+
expect(result.current[1].data).toBe(true)
132+
expect(result.current[1].isLoading).toBe(true)
133+
134+
await waitForNextUpdate()
135+
expect(result.current[1].isSuccess).toBe(true)
136+
})
137+
100138
test('should render correctly with enabled true', async () => {
101139
const { result, waitForNextUpdate } = renderHook(
102140
props => useDataLoader(props.key, props.method, props.config),

packages/use-dataloader/src/useDataLoader.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const Actions = {
4040
* @returns {useDataLoaderResult} hook result containing data, request state, and method to reload the data
4141
*/
4242
const useDataLoader = (
43-
key,
43+
fetchKey,
4444
method,
4545
{
4646
enabled = true,
@@ -51,15 +51,29 @@ const useDataLoader = (
5151
pollingInterval,
5252
} = {},
5353
) => {
54-
const { addReload, clearReload, getCachedData, addCachedData } =
55-
useDataLoaderContext()
54+
const {
55+
addReload,
56+
clearReload,
57+
getCachedData,
58+
addCachedData,
59+
cacheKeyPrefix,
60+
} = useDataLoaderContext()
5661
const [{ status, error }, dispatch] = useReducer(reducer, {
5762
error: undefined,
5863
status: StatusEnum.IDLE,
5964
})
6065

6166
const addReloadRef = useRef(addReload)
6267
const clearReloadRef = useRef(clearReload)
68+
69+
const key = useMemo(() => {
70+
if (!fetchKey || typeof fetchKey !== 'string') {
71+
return fetchKey
72+
}
73+
74+
return `${cacheKeyPrefix ? `${cacheKeyPrefix}-` : ''}${fetchKey}`
75+
}, [cacheKeyPrefix, fetchKey])
76+
6377
const previousDataRef = useRef()
6478
const isMountedRef = useRef(enabled)
6579
const methodRef = useRef(method)

0 commit comments

Comments
 (0)