fix: 🐛 Cache is no longer called in fake mode (#3)
This commit is contained in:
parent
c379b0575d
commit
f89f1431df
|
@ -27,6 +27,23 @@ describe('hooks with fake data', () => {
|
|||
|
||||
storage = createStorage({})
|
||||
})
|
||||
it('does not use cache', async () => {
|
||||
storage.cache.user = JSON.stringify({ user: 'cached' })
|
||||
await act(async () => {
|
||||
const {
|
||||
result,
|
||||
waitForNextUpdate,
|
||||
} = renderHook(() => useUser(), { wrapper })
|
||||
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
|
||||
expect(result.current.data).toEqual({
|
||||
firstName: 'Namn',
|
||||
lastName: 'Namnsson',
|
||||
})
|
||||
})
|
||||
})
|
||||
it('returns user', async () => {
|
||||
await act(async () => {
|
||||
const {
|
||||
|
|
11
src/hooks.ts
11
src/hooks.ts
|
@ -51,12 +51,13 @@ const hook = <T>(
|
|||
defaultValue,
|
||||
apiCall: apiCaller(api),
|
||||
}
|
||||
// Only get from cache first time
|
||||
if (state.status === 'pending') {
|
||||
extra.getFromCache = () => storage.getItem(key)
|
||||
}
|
||||
// Only save real data to cache
|
||||
|
||||
// Only use cache when not in fake mode
|
||||
if (!api.isFake) {
|
||||
// Only get from cache first time
|
||||
if (state.status === 'pending') {
|
||||
extra.getFromCache = () => storage.getItem(key)
|
||||
}
|
||||
extra.saveToCache = (value: string) => storage.setItem(key, value)
|
||||
}
|
||||
const action = loadAction<T>(entityName, extra)
|
||||
|
|
|
@ -137,7 +137,6 @@ describe('useCalendar(child)', () => {
|
|||
|
||||
const { waitForNextUpdate } = renderHook(() => useCalendar(child), { wrapper })
|
||||
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await pause(20)
|
||||
|
|
|
@ -123,7 +123,6 @@ describe('useChildList()', () => {
|
|||
|
||||
const { waitForNextUpdate } = renderHook(() => useChildList(), { wrapper })
|
||||
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await pause(20)
|
||||
|
|
|
@ -125,7 +125,6 @@ describe('useClassmates(child)', () => {
|
|||
|
||||
const { waitForNextUpdate } = renderHook(() => useClassmates(child), { wrapper })
|
||||
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await pause(20)
|
||||
|
|
|
@ -125,7 +125,6 @@ describe('useMenu(child)', () => {
|
|||
|
||||
const { waitForNextUpdate } = renderHook(() => useMenu(child), { wrapper })
|
||||
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await pause(20)
|
||||
|
|
|
@ -125,7 +125,6 @@ describe('useNews(child)', () => {
|
|||
|
||||
const { waitForNextUpdate } = renderHook(() => useNews(child), { wrapper })
|
||||
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await pause(20)
|
||||
|
|
|
@ -125,7 +125,6 @@ describe('useNotifications(child)', () => {
|
|||
|
||||
const { waitForNextUpdate } = renderHook(() => useNotifications(child), { wrapper })
|
||||
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await pause(20)
|
||||
|
|
|
@ -129,7 +129,6 @@ describe('useSchedule(child, from, to)', () => {
|
|||
|
||||
const { waitForNextUpdate } = renderHook(() => useSchedule(child, from, to), { wrapper })
|
||||
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await pause(20)
|
||||
|
|
|
@ -123,7 +123,6 @@ describe('useUser()', () => {
|
|||
|
||||
const { waitForNextUpdate } = renderHook(() => useUser(), { wrapper })
|
||||
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await waitForNextUpdate()
|
||||
await pause(20)
|
||||
|
|
Loading…
Reference in New Issue