|
| 1 | +import { |
| 2 | + AsyncBoundary, |
| 3 | + DataProvider, |
| 4 | + GCPolicy, |
| 5 | + useSuspense, |
| 6 | +} from '@data-client/react'; |
| 7 | +import { MockResolver } from '@data-client/test'; |
| 8 | +import { render, screen, act } from '@testing-library/react'; |
| 9 | +import { ArticleResource } from '__tests__/new'; |
| 10 | +import '@testing-library/jest-dom'; |
| 11 | +import { useState } from 'react'; |
| 12 | + |
| 13 | +const mockGetList = jest.fn(); |
| 14 | +const mockGet = jest.fn(); |
| 15 | +const GC_INTERVAL = 5000; |
| 16 | + |
| 17 | +const ListView = ({ onSelect }: { onSelect: (id: number) => void }) => { |
| 18 | + const articles = useSuspense(ArticleResource.getList); |
| 19 | + return ( |
| 20 | + <div> |
| 21 | + {articles.map(article => ( |
| 22 | + <div key={article.id} onClick={() => onSelect(article.id ?? 0)}> |
| 23 | + {article.title} |
| 24 | + </div> |
| 25 | + ))} |
| 26 | + </div> |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +const DetailView = ({ id }: { id: number }) => { |
| 31 | + const article = useSuspense(ArticleResource.get, { id }); |
| 32 | + const [toggle, setToggle] = useState(false); |
| 33 | + |
| 34 | + return ( |
| 35 | + <div> |
| 36 | + <h3>{article.title}</h3> |
| 37 | + <p>{article.content}</p> |
| 38 | + <button onClick={() => setToggle(!toggle)}>Toggle Re-render</button> |
| 39 | + {toggle && <div>Toggle state: {toggle.toString()}</div>} |
| 40 | + </div> |
| 41 | + ); |
| 42 | +}; |
| 43 | + |
| 44 | +const TestComponent = () => { |
| 45 | + const [view, setView] = useState<'list' | 'detail'>('list'); |
| 46 | + const [selectedId, setSelectedId] = useState<number | null>(null); |
| 47 | + |
| 48 | + return ( |
| 49 | + <DataProvider gcPolicy={new GCPolicy({ intervalMS: GC_INTERVAL })}> |
| 50 | + <MockResolver |
| 51 | + fixtures={[ |
| 52 | + { |
| 53 | + endpoint: ArticleResource.getList, |
| 54 | + response: mockGetList, |
| 55 | + delay: 100, |
| 56 | + }, |
| 57 | + { endpoint: ArticleResource.get, response: mockGet, delay: 100 }, |
| 58 | + ]} |
| 59 | + > |
| 60 | + <div onClick={() => setView('list')}>Home</div> |
| 61 | + <AsyncBoundary fallback={<div>Loading...</div>}> |
| 62 | + {view === 'list' ? |
| 63 | + <ListView |
| 64 | + onSelect={id => { |
| 65 | + setSelectedId(id); |
| 66 | + setView('detail'); |
| 67 | + }} |
| 68 | + /> |
| 69 | + : selectedId !== null && <DetailView id={selectedId} />} |
| 70 | + </AsyncBoundary> |
| 71 | + </MockResolver> |
| 72 | + </DataProvider> |
| 73 | + ); |
| 74 | +}; |
| 75 | + |
| 76 | +test('switch between list and detail view', async () => { |
| 77 | + jest.useFakeTimers(); |
| 78 | + mockGetList.mockResolvedValue([ |
| 79 | + { id: 1, title: 'Article 1', content: 'Content 1' }, |
| 80 | + { id: 2, title: 'Article 2', content: 'Content 2' }, |
| 81 | + ]); |
| 82 | + mockGet.mockResolvedValue({ |
| 83 | + id: 1, |
| 84 | + title: 'Article 1', |
| 85 | + content: 'Content 1', |
| 86 | + }); |
| 87 | + |
| 88 | + render(<TestComponent />); |
| 89 | + |
| 90 | + // Initial render, should show list view |
| 91 | + expect(await screen.findByText('Article 1')).toBeInTheDocument(); |
| 92 | + |
| 93 | + // Switch to detail view |
| 94 | + act(() => { |
| 95 | + screen.getByText('Article 1').click(); |
| 96 | + }); |
| 97 | + |
| 98 | + // Detail view should render |
| 99 | + expect(await screen.findByText('Content 1')).toBeInTheDocument(); |
| 100 | + |
| 101 | + // Jest time pass to trigger sweep but not expired |
| 102 | + act(() => { |
| 103 | + jest.advanceTimersByTime(GC_INTERVAL); |
| 104 | + }); |
| 105 | + |
| 106 | + // Switch back to list view |
| 107 | + act(() => { |
| 108 | + screen.getByText('Home').click(); |
| 109 | + }); |
| 110 | + |
| 111 | + // List view should instantly render |
| 112 | + expect(await screen.findByText('Article 1')).toBeInTheDocument(); |
| 113 | + |
| 114 | + // Switch back to detail view |
| 115 | + act(() => { |
| 116 | + screen.getByText('Article 1').click(); |
| 117 | + }); |
| 118 | + |
| 119 | + // Jest time pass to expiry |
| 120 | + act(() => { |
| 121 | + jest.advanceTimersByTime(ArticleResource.getList.dataExpiryLength ?? 60000); |
| 122 | + }); |
| 123 | + expect(await screen.findByText('Content 1')).toBeInTheDocument(); |
| 124 | + |
| 125 | + // Re-render detail view to make sure it still renders |
| 126 | + act(() => { |
| 127 | + screen.getByText('Toggle Re-render').click(); |
| 128 | + }); |
| 129 | + expect(await screen.findByText('Toggle state: true')).toBeInTheDocument(); |
| 130 | + expect(await screen.findByText('Content 1')).toBeInTheDocument(); |
| 131 | + |
| 132 | + // Visit list view and see suspense fallback |
| 133 | + act(() => { |
| 134 | + screen.getByText('Home').click(); |
| 135 | + }); |
| 136 | + |
| 137 | + expect(screen.getByText('Loading...')).toBeInTheDocument(); |
| 138 | + jest.useRealTimers(); |
| 139 | +}); |
0 commit comments