|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | + |
| 4 | +import { useGetMore } from './useGetMore'; |
| 5 | +import { RoomHistoryManager } from '../../../../../app/ui-utils/client'; |
| 6 | + |
| 7 | +jest.mock('../../../../../app/ui-utils/client', () => ({ |
| 8 | + RoomHistoryManager: { |
| 9 | + isLoading: jest.fn(), |
| 10 | + hasMore: jest.fn(), |
| 11 | + hasMoreNext: jest.fn(), |
| 12 | + getMore: jest.fn(), |
| 13 | + getMoreNext: jest.fn(), |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +const mockGetMore = jest.fn(); |
| 18 | + |
| 19 | +describe('useGetMore', () => { |
| 20 | + it('should call getMore when scrolling near top and hasMore is true', () => { |
| 21 | + (RoomHistoryManager.isLoading as jest.Mock).mockReturnValue(false); |
| 22 | + (RoomHistoryManager.hasMore as jest.Mock).mockReturnValue(true); |
| 23 | + (RoomHistoryManager.hasMoreNext as jest.Mock).mockReturnValue(false); |
| 24 | + (RoomHistoryManager.getMore as jest.Mock).mockImplementation(mockGetMore); |
| 25 | + const atBottomRef = { current: false }; |
| 26 | + |
| 27 | + const mockElement = { |
| 28 | + addEventListener: jest.fn((event, handler) => { |
| 29 | + if (event === 'scroll') { |
| 30 | + handler({ |
| 31 | + target: { |
| 32 | + scrollTop: 10, |
| 33 | + clientHeight: 300, |
| 34 | + }, |
| 35 | + }); |
| 36 | + } |
| 37 | + }), |
| 38 | + removeEventListener: jest.fn(), |
| 39 | + }; |
| 40 | + |
| 41 | + const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: mockElement }); |
| 42 | + |
| 43 | + const { unmount } = renderHook(() => useGetMore('room-id', atBottomRef), { legacyRoot: true }); |
| 44 | + |
| 45 | + expect(useRefSpy).toHaveBeenCalledWith(null); |
| 46 | + expect(RoomHistoryManager.getMore).toHaveBeenCalledWith('room-id'); |
| 47 | + |
| 48 | + unmount(); |
| 49 | + expect(mockElement.removeEventListener).toHaveBeenCalledWith('scroll', expect.any(Function)); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should call getMoreNext when scrolling near bottom and hasMoreNext is true', () => { |
| 53 | + (RoomHistoryManager.isLoading as jest.Mock).mockReturnValue(false); |
| 54 | + (RoomHistoryManager.hasMore as jest.Mock).mockReturnValue(false); |
| 55 | + (RoomHistoryManager.hasMoreNext as jest.Mock).mockReturnValue(true); |
| 56 | + (RoomHistoryManager.getMoreNext as jest.Mock).mockImplementation(mockGetMore); |
| 57 | + |
| 58 | + const atBottomRef = { current: false }; |
| 59 | + const mockElement = { |
| 60 | + addEventListener: jest.fn((event, handler) => { |
| 61 | + if (event === 'scroll') { |
| 62 | + handler({ |
| 63 | + target: { |
| 64 | + scrollTop: 600, |
| 65 | + clientHeight: 300, |
| 66 | + scrollHeight: 800, |
| 67 | + }, |
| 68 | + }); |
| 69 | + } |
| 70 | + }), |
| 71 | + removeEventListener: jest.fn(), |
| 72 | + }; |
| 73 | + const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: mockElement }); |
| 74 | + |
| 75 | + renderHook(() => useGetMore('room-id', atBottomRef), { legacyRoot: true }); |
| 76 | + |
| 77 | + expect(useRefSpy).toHaveBeenCalledWith(null); |
| 78 | + expect(RoomHistoryManager.getMoreNext).toHaveBeenCalledWith('room-id', atBottomRef); |
| 79 | + }); |
| 80 | +}); |
0 commit comments