|
| 1 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 2 | +import { renderHook, waitFor } from '@testing-library/react-native'; |
| 3 | + |
| 4 | +import { useTweetDetail } from '@/hooks/tweets/useTweetDetail'; |
| 5 | +import { getTweetCache } from '@/libs/tweetCache'; |
| 6 | +import { getTweet } from '@/services/tweets'; |
| 7 | + |
| 8 | +// Mock dependencies |
| 9 | +jest.mock('@/services/tweets', () => ({ |
| 10 | + getTweet: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('@/libs/tweetCache', () => ({ |
| 14 | + getTweetCache: jest.fn(() => ({ |
| 15 | + setTweet: jest.fn(), |
| 16 | + setTweets: jest.fn(), |
| 17 | + })), |
| 18 | +})); |
| 19 | + |
| 20 | +describe('useTweetDetail', () => { |
| 21 | + let queryClient: QueryClient; |
| 22 | + const mockTweetCache = { |
| 23 | + setTweet: jest.fn(), |
| 24 | + setTweets: jest.fn(), |
| 25 | + }; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + queryClient = new QueryClient({ |
| 29 | + defaultOptions: { |
| 30 | + queries: { |
| 31 | + retry: false, |
| 32 | + retryDelay: 0, // No delay between retries for fast testing |
| 33 | + }, |
| 34 | + }, |
| 35 | + }); |
| 36 | + jest.clearAllMocks(); |
| 37 | + (getTweetCache as jest.Mock).mockReturnValue(mockTweetCache); |
| 38 | + }); |
| 39 | + |
| 40 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 41 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 42 | + ); |
| 43 | + |
| 44 | + it('fetches and caches tweet successfully', async () => { |
| 45 | + const mockTweet = { |
| 46 | + id: '123', |
| 47 | + content: 'test tweet', |
| 48 | + author: { id: 'u1', username: 'user1' }, |
| 49 | + rootTweet: { id: 'root1', content: 'root tweet' }, |
| 50 | + parentTweets: [{ id: 'p1', content: 'parent tweet' }], |
| 51 | + }; |
| 52 | + |
| 53 | + (getTweet as jest.Mock).mockResolvedValue({ |
| 54 | + success: true, |
| 55 | + data: mockTweet, |
| 56 | + }); |
| 57 | + |
| 58 | + const { result } = renderHook(() => useTweetDetail('123'), { wrapper }); |
| 59 | + |
| 60 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 61 | + |
| 62 | + expect(result.current.data).toEqual(mockTweet); |
| 63 | + expect(mockTweetCache.setTweet).toHaveBeenCalledWith(mockTweet); |
| 64 | + expect(mockTweetCache.setTweet).toHaveBeenCalledWith(mockTweet.rootTweet); |
| 65 | + expect(mockTweetCache.setTweets).toHaveBeenCalledWith(mockTweet.parentTweets); |
| 66 | + }); |
| 67 | + |
| 68 | + it('does not cache deleted root/parent tweets', async () => { |
| 69 | + const mockTweet = { |
| 70 | + id: '123', |
| 71 | + content: 'test tweet', |
| 72 | + rootTweet: { id: 'root1', isDeleted: true }, |
| 73 | + parentTweets: [ |
| 74 | + { id: 'p1', content: 'parent tweet' }, |
| 75 | + { id: 'p2', isDeleted: true }, |
| 76 | + ], |
| 77 | + }; |
| 78 | + |
| 79 | + (getTweet as jest.Mock).mockResolvedValue({ |
| 80 | + success: true, |
| 81 | + data: mockTweet, |
| 82 | + }); |
| 83 | + |
| 84 | + const { result } = renderHook(() => useTweetDetail('123'), { wrapper }); |
| 85 | + |
| 86 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 87 | + |
| 88 | + expect(mockTweetCache.setTweet).toHaveBeenCalledWith(mockTweet); |
| 89 | + expect(mockTweetCache.setTweet).not.toHaveBeenCalledWith( |
| 90 | + expect.objectContaining({ id: 'root1' }) |
| 91 | + ); |
| 92 | + expect(mockTweetCache.setTweets).toHaveBeenCalledWith([{ id: 'p1', content: 'parent tweet' }]); |
| 93 | + }); |
| 94 | + |
| 95 | + it('handles 404 error by preventing retry', async () => { |
| 96 | + interface ErrorWithStatus extends Error { |
| 97 | + status: number; |
| 98 | + } |
| 99 | + const error = new Error('Not found') as ErrorWithStatus; |
| 100 | + error.status = 404; |
| 101 | + (getTweet as jest.Mock).mockRejectedValue(error); |
| 102 | + |
| 103 | + const { result } = renderHook(() => useTweetDetail('123'), { wrapper }); |
| 104 | + |
| 105 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 106 | + expect(result.current.failureCount).toBe(1); // Should fail once and not retry |
| 107 | + }); |
| 108 | + |
| 109 | + it('throws error when response is not success', async () => { |
| 110 | + (getTweet as jest.Mock).mockResolvedValue({ |
| 111 | + success: false, |
| 112 | + }); |
| 113 | + |
| 114 | + const { result } = renderHook(() => useTweetDetail('123'), { wrapper }); |
| 115 | + |
| 116 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 117 | + expect(result.current.error).toEqual(new Error('Failed to fetch tweet')); |
| 118 | + }); |
| 119 | +}); |
0 commit comments