Skip to content

Commit 8cdc464

Browse files
committed
test: add tests
1 parent f9c5186 commit 8cdc464

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

src/components/NavigationBar/tabs/useIndexOfLastVisibleChild.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function useIndexOfLastVisibleChild() {
4949

5050
if (sumWidth <= containingRect.width) {
5151
lastVisibleIndex = i;
52-
} else {
52+
} /* istanbul ignore else */ else {
5353
break;
5454
}
5555
}
@@ -59,7 +59,9 @@ export default function useIndexOfLastVisibleChild() {
5959

6060
useLayoutEffect(() => {
6161
const container = containerElementRef.current;
62-
if (!container) { return undefined; }
62+
if (!container) {
63+
return () => {};
64+
}
6365

6466
// ResizeObserver tracks size changes of the container or its children
6567
const resizeObserver = new ResizeObserver(() => {
@@ -70,7 +72,10 @@ export default function useIndexOfLastVisibleChild() {
7072
// Run once on mount to ensure accurate measurement from the start
7173
measureVisibleChildren();
7274

73-
return () => resizeObserver.disconnect();
75+
/* istanbul ignore next */
76+
return () => {
77+
resizeObserver.disconnect();
78+
};
7479
}, []);
7580

7681
return [indexOfLastVisibleChild, containerElementRef, invisibleStyle, overflowElementRef];
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)