|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { render, renderHook } from '@testing-library/react'; |
| 4 | + |
| 5 | +import useIndexOfLastVisibleChild from './useIndexOfLastVisibleChild'; |
| 6 | + |
| 7 | +describe('useIndexOfLastVisibleChild', () => { |
| 8 | + let observeMock; |
| 9 | + let disconnectMock; |
| 10 | + |
| 11 | + beforeAll(() => { |
| 12 | + observeMock = jest.fn(); |
| 13 | + disconnectMock = jest.fn(); |
| 14 | + global.ResizeObserver = class { |
| 15 | + observe = observeMock; |
| 16 | + |
| 17 | + disconnect = disconnectMock; |
| 18 | + }; |
| 19 | + }); |
| 20 | + |
| 21 | + afterAll(() => { |
| 22 | + delete global.ResizeObserver; |
| 23 | + }); |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + observeMock.mockReset(); |
| 27 | + disconnectMock.mockReset(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('calls disconnect on cleanup when container exists', () => { |
| 31 | + const TestComponent = () => { |
| 32 | + const [, containerRef] = useIndexOfLastVisibleChild(); |
| 33 | + return <div ref={containerRef} />; |
| 34 | + }; |
| 35 | + const { unmount } = render(<TestComponent />); |
| 36 | + unmount(); |
| 37 | + expect(disconnectMock).toHaveBeenCalled(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('handles missing container gracefully (no observer or disconnect)', () => { |
| 41 | + const { unmount } = renderHook(() => useIndexOfLastVisibleChild()); |
| 42 | + unmount(); |
| 43 | + expect(disconnectMock).not.toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns -1 if there are no children', () => { |
| 47 | + const TestComponent = () => { |
| 48 | + const [, containerRef] = useIndexOfLastVisibleChild(); |
| 49 | + React.useEffect(() => { |
| 50 | + const container = document.createElement('div'); |
| 51 | + container.getBoundingClientRect = () => ({ width: 100 }); |
| 52 | + containerRef.current = container; |
| 53 | + }, []); |
| 54 | + return <div ref={containerRef} />; |
| 55 | + }; |
| 56 | + const { unmount } = render(<TestComponent />); |
| 57 | + unmount(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('triggers break when child tabs exceed container width', () => { |
| 61 | + const TestComponent = () => { |
| 62 | + const [, containerRef] = useIndexOfLastVisibleChild(); |
| 63 | + React.useEffect(() => { |
| 64 | + const container = document.createElement('div'); |
| 65 | + container.getBoundingClientRect = () => ({ width: 100 }); |
| 66 | + const child1 = document.createElement('div'); |
| 67 | + child1.getBoundingClientRect = () => ({ width: 80 }); |
| 68 | + const child2 = document.createElement('div'); |
| 69 | + child2.getBoundingClientRect = () => ({ width: 80 }); |
| 70 | + container.appendChild(child1); |
| 71 | + container.appendChild(child2); |
| 72 | + |
| 73 | + containerRef.current = container; |
| 74 | + }, []); |
| 75 | + return <div ref={containerRef} />; |
| 76 | + }; |
| 77 | + const { unmount } = render(<TestComponent />); |
| 78 | + unmount(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments