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