Skip to content

Commit

Permalink
fix: 🐛 Cache is no longer called in fake mode (kolplattformen#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanObrink authored Feb 7, 2021
1 parent c379b05 commit f89f143
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/fake.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/useCalendar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ describe('useCalendar(child)', () => {

const { waitForNextUpdate } = renderHook(() => useCalendar(child), { wrapper })

await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)
Expand Down
1 change: 0 additions & 1 deletion src/useChildlist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ describe('useChildList()', () => {

const { waitForNextUpdate } = renderHook(() => useChildList(), { wrapper })

await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)
Expand Down
1 change: 0 additions & 1 deletion src/useClassmates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ describe('useClassmates(child)', () => {

const { waitForNextUpdate } = renderHook(() => useClassmates(child), { wrapper })

await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)
Expand Down
1 change: 0 additions & 1 deletion src/useMenu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ describe('useMenu(child)', () => {

const { waitForNextUpdate } = renderHook(() => useMenu(child), { wrapper })

await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)
Expand Down
1 change: 0 additions & 1 deletion src/useNews.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ describe('useNews(child)', () => {

const { waitForNextUpdate } = renderHook(() => useNews(child), { wrapper })

await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)
Expand Down
1 change: 0 additions & 1 deletion src/useNotifications.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ describe('useNotifications(child)', () => {

const { waitForNextUpdate } = renderHook(() => useNotifications(child), { wrapper })

await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)
Expand Down
1 change: 0 additions & 1 deletion src/useSchedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/useUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ describe('useUser()', () => {

const { waitForNextUpdate } = renderHook(() => useUser(), { wrapper })

await waitForNextUpdate()
await waitForNextUpdate()
await waitForNextUpdate()
await pause(20)
Expand Down

0 comments on commit f89f143

Please sign in to comment.