|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | + |
| 4 | +import { useRestoreScrollPosition } from './useRestoreScrollPosition'; |
| 5 | +import { RoomManager } from '../../../../lib/RoomManager'; |
| 6 | + |
| 7 | +jest.mock('../../../../lib/RoomManager', () => ({ |
| 8 | + RoomManager: { |
| 9 | + getStore: jest.fn(), |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +describe('useRestoreScrollPosition', () => { |
| 14 | + it('should restore room scroll position based on store', () => { |
| 15 | + (RoomManager.getStore as jest.Mock).mockReturnValue({ scroll: 100, atBottom: false }); |
| 16 | + |
| 17 | + const mockElement = { |
| 18 | + addEventListener: jest.fn(), |
| 19 | + removeEventListener: jest.fn(), |
| 20 | + }; |
| 21 | + |
| 22 | + const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: mockElement }); |
| 23 | + |
| 24 | + const { unmount } = renderHook(() => useRestoreScrollPosition('room-id'), { legacyRoot: true }); |
| 25 | + |
| 26 | + expect(useRefSpy).toHaveBeenCalledWith(null); |
| 27 | + expect(mockElement).toHaveProperty('scrollTop', 100); |
| 28 | + expect(mockElement).toHaveProperty('scrollLeft', 30); |
| 29 | + |
| 30 | + unmount(); |
| 31 | + expect(mockElement.removeEventListener).toHaveBeenCalledWith('scroll', expect.any(Function)); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should not restore scroll position if already at bottom', () => { |
| 35 | + (RoomManager.getStore as jest.Mock).mockReturnValue({ scroll: 100, atBottom: true }); |
| 36 | + |
| 37 | + const mockElement = { |
| 38 | + addEventListener: jest.fn(), |
| 39 | + removeEventListener: jest.fn(), |
| 40 | + scrollHeight: 800, |
| 41 | + }; |
| 42 | + |
| 43 | + const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: mockElement }); |
| 44 | + |
| 45 | + const { unmount } = renderHook(() => useRestoreScrollPosition('room-id'), { legacyRoot: true }); |
| 46 | + |
| 47 | + expect(useRefSpy).toHaveBeenCalledWith(null); |
| 48 | + expect(mockElement).toHaveProperty('scrollTop', 800); |
| 49 | + expect(mockElement).not.toHaveProperty('scrollLeft'); |
| 50 | + |
| 51 | + unmount(); |
| 52 | + expect(mockElement.removeEventListener).toHaveBeenCalledWith('scroll', expect.any(Function)); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should update store based on scroll position', () => { |
| 56 | + const update = jest.fn(); |
| 57 | + (RoomManager.getStore as jest.Mock).mockReturnValue({ update }); |
| 58 | + |
| 59 | + const mockElement = { |
| 60 | + addEventListener: jest.fn((event, handler) => { |
| 61 | + if (event === 'scroll') { |
| 62 | + handler({ |
| 63 | + target: { |
| 64 | + scrollTop: 500, |
| 65 | + }, |
| 66 | + }); |
| 67 | + } |
| 68 | + }), |
| 69 | + removeEventListener: jest.fn(), |
| 70 | + }; |
| 71 | + |
| 72 | + const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: mockElement }); |
| 73 | + |
| 74 | + const { unmount } = renderHook(() => useRestoreScrollPosition('room-id'), { legacyRoot: true }); |
| 75 | + |
| 76 | + expect(useRefSpy).toHaveBeenCalledWith(null); |
| 77 | + expect(update).toHaveBeenCalledWith({ scroll: 500, atBottom: false }); |
| 78 | + |
| 79 | + unmount(); |
| 80 | + expect(mockElement.removeEventListener).toHaveBeenCalledWith('scroll', expect.any(Function)); |
| 81 | + }); |
| 82 | +}); |
0 commit comments