Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | import { render, screen } from '@testing-library/react'; import { API } from '@/api'; import My from './index'; jest.mock('@/hooks/use-group/use-group-infinite-list', () => ({ useInfiniteScroll: jest.fn(), })); jest.mock('@/hooks/use-intersection-observer', () => ({ useIntersectionObserver: jest.fn(), })); jest.mock('@/api', () => ({ API: { groupService: { getMyGroups: jest.fn(), }, }, })); jest.mock('../meetings/index', () => ({ Meetings: jest.fn(() => <div data-testid='meetings' />), })); import { useInfiniteScroll } from '@/hooks/use-group/use-group-infinite-list'; import { useIntersectionObserver } from '@/hooks/use-intersection-observer'; const mockUseInfiniteScroll = useInfiniteScroll as jest.MockedFunction<typeof useInfiniteScroll>; const mockUseIntersectionObserver = useIntersectionObserver as jest.MockedFunction< typeof useIntersectionObserver >; describe('My', () => { const mockSentinelRef = { current: null }; let onIntersectCallback: (() => void) | null = null; beforeEach(() => { jest.clearAllMocks(); onIntersectCallback = null; mockUseIntersectionObserver.mockImplementation(({ onIntersect }) => { onIntersectCallback = onIntersect; return mockSentinelRef; }); mockUseInfiniteScroll.mockReturnValue({ items: [], nextCursor: null, error: null, fetchNextPage: jest.fn(), hasNextPage: false, isFetchingNextPage: false, isFetching: false, isLoading: false, completedMessage: '모든 나의 모임을 불러왔습니다.', refetch: jest.fn(), }); }); test('컴포넌트가 정상적으로 렌더링된다', () => { render(<My />); expect(screen.getByTestId('meetings')).toBeInTheDocument(); }); test('queryFn이 올바른 API를 호출한다', async () => { mockUseInfiniteScroll.mockReturnValue({ items: [], nextCursor: null, error: null, fetchNextPage: jest.fn(), hasNextPage: false, isFetchingNextPage: false, isFetching: false, isLoading: false, completedMessage: '', refetch: jest.fn(), }); render(<My />); const queryFn = mockUseInfiniteScroll.mock.calls[0][0].queryFn; await queryFn({ cursor: 0, size: 10 }); expect(API.groupService.getMyGroups).toHaveBeenCalledWith({ type: 'myPost', cursor: 0, size: 10, filter: 'ACTIVE', excludeStatuses: ['CLOSED'], }); }); test('intersection observer의 onIntersect가 호출되면 fetchNextPage가 호출된다', () => { const mockFetchNextPage = jest.fn(); mockUseInfiniteScroll.mockReturnValue({ items: [], nextCursor: 100, error: null, fetchNextPage: mockFetchNextPage, hasNextPage: true, isFetchingNextPage: false, isFetching: false, isLoading: false, completedMessage: '', refetch: jest.fn(), }); render(<My />); expect(onIntersectCallback).not.toBeNull(); onIntersectCallback?.(); expect(mockFetchNextPage).toHaveBeenCalledTimes(1); }); test('hasNextPage가 false일 때는 fetchNextPage가 호출되지 않는다', () => { const mockFetchNextPage = jest.fn(); mockUseInfiniteScroll.mockReturnValue({ items: [], nextCursor: null, error: null, fetchNextPage: mockFetchNextPage, hasNextPage: false, isFetchingNextPage: false, isFetching: false, isLoading: false, completedMessage: '', refetch: jest.fn(), }); render(<My />); expect(onIntersectCallback).not.toBeNull(); onIntersectCallback?.(); expect(mockFetchNextPage).not.toHaveBeenCalled(); }); test('isFetchingNextPage가 true일 때는 fetchNextPage가 호출되지 않는다', () => { const mockFetchNextPage = jest.fn(); mockUseInfiniteScroll.mockReturnValue({ items: [], nextCursor: 100, error: null, fetchNextPage: mockFetchNextPage, hasNextPage: true, isFetchingNextPage: true, isFetching: false, isLoading: false, completedMessage: '', refetch: jest.fn(), }); render(<My />); expect(onIntersectCallback).not.toBeNull(); onIntersectCallback?.(); expect(mockFetchNextPage).not.toHaveBeenCalled(); }); }); |