-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswr-cache.test.ts
More file actions
125 lines (106 loc) · 5.58 KB
/
swr-cache.test.ts
File metadata and controls
125 lines (106 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { act, renderHook, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useSwrCache } from './swr-cache';
// Focus/poll revalidation is exercised separately; these isolate the
// core stale-while-revalidate contract by disabling the focus listener
// (and leaving the poll interval at its 0 default).
const opts = { refetchOnFocus: false as const };
beforeEach(() => {
window.localStorage.clear();
});
describe('useSwrCache', () => {
it('fetches on mount when there is no cached value', async () => {
const fetcher = vi.fn().mockResolvedValue({ v: 'fresh' });
const { result } = renderHook(() => useSwrCache({ cacheKey: 'k1', fetcher, ...opts }));
// No cache → starts loading.
expect(result.current.loading).toBe(true);
await waitFor(() => expect(result.current.data).toEqual({ v: 'fresh' }));
expect(result.current.loading).toBe(false);
expect(result.current.error).toBeNull();
expect(fetcher).toHaveBeenCalledTimes(1);
});
it('serves the cached value immediately, then revalidates to fresh', async () => {
window.localStorage.setItem('k2', JSON.stringify({ v: 'cached' }));
const fetcher = vi.fn().mockResolvedValue({ v: 'fresh' });
const { result } = renderHook(() => useSwrCache({ cacheKey: 'k2', fetcher, ...opts }));
// No spinner flash: data is present on the very first render (the
// page renders a spinner only when `loading && !data`).
expect(result.current.data).toEqual({ v: 'cached' });
// Background revalidation then swaps in the canonical value.
await waitFor(() => expect(result.current.data).toEqual({ v: 'fresh' }));
});
it('keeps the last cached value when the fetch fails (never blanks the view)', async () => {
window.localStorage.setItem('k3', JSON.stringify({ v: 'cached' }));
const fetcher = vi.fn().mockRejectedValue(new Error('boom'));
const { result } = renderHook(() => useSwrCache({ cacheKey: 'k3', fetcher, ...opts }));
await waitFor(() => expect(result.current.error).not.toBeNull());
expect(result.current.data).toEqual({ v: 'cached' });
});
it('refresh() re-invokes the fetcher', async () => {
const fetcher = vi.fn().mockResolvedValue({ v: 'x' });
const { result } = renderHook(() => useSwrCache({ cacheKey: 'k4', fetcher, ...opts }));
await waitFor(() => expect(result.current.data).toEqual({ v: 'x' }));
await act(async () => {
await result.current.refresh();
});
expect(fetcher).toHaveBeenCalledTimes(2);
});
it('writes the fetched value into localStorage', async () => {
const fetcher = vi.fn().mockResolvedValue({ v: 'persisted' });
const { result } = renderHook(() => useSwrCache({ cacheKey: 'k5', fetcher, ...opts }));
await waitFor(() => expect(result.current.data).toEqual({ v: 'persisted' }));
const raw = window.localStorage.getItem('k5');
expect(raw).not.toBeNull();
expect(JSON.parse(raw as string)).toEqual({ v: 'persisted' });
});
it('blanks to a loading state on a key change by default (#416)', async () => {
// Detail-style: a new key with no cache must not keep the previous
// record's data on screen — it would flash the wrong record.
const fetcher = vi.fn().mockResolvedValue({ v: 'a' });
const { result, rerender } = renderHook(
({ k }: { k: string }) => useSwrCache({ cacheKey: k, fetcher, ...opts }),
{ initialProps: { k: 'kc-a' } },
);
await waitFor(() => expect(result.current.data).toEqual({ v: 'a' }));
fetcher.mockResolvedValueOnce({ v: 'b' });
rerender({ k: 'kc-b' });
// New uncached key → blanked + loading until the fetch lands.
expect(result.current.data).toBeNull();
expect(result.current.loading).toBe(true);
await waitFor(() => expect(result.current.data).toEqual({ v: 'b' }));
});
it('keeps the previous data on a key change when keepPreviousData is set (#368)', async () => {
// List-style: a filter change (new key, no cache) keeps the prior
// rows on screen and shows a foreground load, so the page chrome
// stays put and only the table skeletons.
const fetcher = vi.fn().mockResolvedValue({ v: 'page1' });
const { result, rerender } = renderHook(
({ k }: { k: string }) =>
useSwrCache({ cacheKey: k, fetcher, keepPreviousData: true, ...opts }),
{ initialProps: { k: 'kp-1' } },
);
await waitFor(() => expect(result.current.data).toEqual({ v: 'page1' }));
fetcher.mockResolvedValueOnce({ v: 'page2' });
rerender({ k: 'kp-2' });
// Previous data retained (not blanked) while loading the new key.
expect(result.current.data).toEqual({ v: 'page1' });
expect(result.current.loading).toBe(true);
await waitFor(() => expect(result.current.data).toEqual({ v: 'page2' }));
});
it('adopts the new key cached value immediately even with keepPreviousData', async () => {
window.localStorage.setItem('kp-b', JSON.stringify({ v: 'cached-b' }));
const fetcher = vi.fn().mockResolvedValue({ v: 'fresh-a' });
const { result, rerender } = renderHook(
({ k }: { k: string }) =>
useSwrCache({ cacheKey: k, fetcher, keepPreviousData: true, ...opts }),
{ initialProps: { k: 'kp-a' } },
);
await waitFor(() => expect(result.current.data).toEqual({ v: 'fresh-a' }));
fetcher.mockResolvedValueOnce({ v: 'fresh-b' });
rerender({ k: 'kp-b' });
// The new key has a cache → show it at once (no stale-a, no blank),
// then revalidate to the canonical value.
expect(result.current.data).toEqual({ v: 'cached-b' });
await waitFor(() => expect(result.current.data).toEqual({ v: 'fresh-b' }));
});
});