fix: 🐛 Cache is no longer called in fake mode (#3)

This commit is contained in:
Johan Öbrink 2021-02-07 12:32:21 +01:00 committed by GitHub
parent c379b0575d
commit f89f1431df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 23 additions and 13 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -137,7 +137,6 @@ describe('useCalendar(child)', () => {
const { waitForNextUpdate } = renderHook(() => useCalendar(child), { wrapper })
await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)

View File

@ -123,7 +123,6 @@ describe('useChildList()', () => {
const { waitForNextUpdate } = renderHook(() => useChildList(), { wrapper })
await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)

View File

@ -125,7 +125,6 @@ describe('useClassmates(child)', () => {
const { waitForNextUpdate } = renderHook(() => useClassmates(child), { wrapper })
await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)

View File

@ -125,7 +125,6 @@ describe('useMenu(child)', () => {
const { waitForNextUpdate } = renderHook(() => useMenu(child), { wrapper })
await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)

View File

@ -125,7 +125,6 @@ describe('useNews(child)', () => {
const { waitForNextUpdate } = renderHook(() => useNews(child), { wrapper })
await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)

View File

@ -125,7 +125,6 @@ describe('useNotifications(child)', () => {
const { waitForNextUpdate } = renderHook(() => useNotifications(child), { wrapper })
await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)

View File

@ -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)

View File

@ -123,7 +123,6 @@ describe('useUser()', () => {
const { waitForNextUpdate } = renderHook(() => useUser(), { wrapper })
await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)