Skip to content

Commit a6a038c

Browse files
test: added tests
1 parent 8cdc464 commit a6a038c

File tree

2 files changed

+184
-2
lines changed

2 files changed

+184
-2
lines changed

src/components/NavigationBar/tabs/useIndexOfLastVisibleChild.js

Lines changed: 1 addition & 1 deletion
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-
} /* istanbul ignore else */ else {
52+
} else {
5353
break;
5454
}
5555
}

src/components/NavigationBar/tabs/useIndexOfLastVisibleChild.test.jsx

Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import { render, renderHook } from '@testing-library/react';
3+
import { act, render, renderHook } from '@testing-library/react';
44

55
import useIndexOfLastVisibleChild from './useIndexOfLastVisibleChild';
66

@@ -77,4 +77,186 @@ describe('useIndexOfLastVisibleChild', () => {
7777
const { unmount } = render(<TestComponent />);
7878
unmount();
7979
});
80+
81+
it('calls measureVisibleChildren on mount and when ResizeObserver triggers', () => {
82+
const TestComponent = () => {
83+
const [, containerRef] = useIndexOfLastVisibleChild();
84+
React.useEffect(() => {
85+
const container = document.createElement('div');
86+
container.getBoundingClientRect = () => ({ width: 200 });
87+
containerRef.current = container;
88+
}, []);
89+
return <div ref={containerRef} />;
90+
};
91+
92+
const { unmount } = render(<TestComponent />);
93+
94+
expect(observeMock).toHaveBeenCalled();
95+
96+
act(() => {
97+
const resizeObserverCallback = observeMock.mock.calls[0][0];
98+
if (resizeObserverCallback && typeof resizeObserverCallback === 'function') {
99+
resizeObserverCallback();
100+
}
101+
});
102+
103+
unmount();
104+
});
105+
106+
it('calculates correct last visible index when children fit within container', () => {
107+
let resizeObserverCallback;
108+
109+
global.ResizeObserver = function (callback) {
110+
resizeObserverCallback = callback;
111+
this.observe = jest.fn();
112+
this.disconnect = jest.fn();
113+
};
114+
115+
const TestComponent = () => {
116+
const [lastVisibleIndex, containerRef, , overflowRef] = useIndexOfLastVisibleChild();
117+
118+
React.useEffect(() => {
119+
const container = document.createElement('div');
120+
container.getBoundingClientRect = () => ({ width: 200 });
121+
122+
const child1 = document.createElement('div');
123+
child1.getBoundingClientRect = () => ({ width: 50 });
124+
const child2 = document.createElement('div');
125+
child2.getBoundingClientRect = () => ({ width: 60 });
126+
const child3 = document.createElement('div');
127+
child3.getBoundingClientRect = () => ({ width: 70 });
128+
129+
container.appendChild(child1);
130+
container.appendChild(child2);
131+
container.appendChild(child3);
132+
133+
containerRef.current = container;
134+
overflowRef.current = null;
135+
}, []);
136+
137+
return (
138+
<div>
139+
<div ref={containerRef} />
140+
<div data-testid="last-visible-index">{lastVisibleIndex}</div>
141+
</div>
142+
);
143+
};
144+
145+
const { getByTestId, unmount } = render(<TestComponent />);
146+
147+
act(() => {
148+
if (resizeObserverCallback) {
149+
resizeObserverCallback();
150+
}
151+
});
152+
153+
// The last visible index should be 2 (all three children fit: 50 + 60 + 70 = 180 <= 200)
154+
expect(getByTestId('last-visible-index').textContent).toBe('2');
155+
156+
unmount();
157+
});
158+
159+
it('handles overflow element in width calculation', () => {
160+
let resizeObserverCallback;
161+
162+
global.ResizeObserver = function (callback) {
163+
resizeObserverCallback = callback;
164+
this.observe = jest.fn();
165+
this.disconnect = jest.fn();
166+
};
167+
168+
const TestComponent = () => {
169+
const [lastVisibleIndex, containerRef, , overflowRef] = useIndexOfLastVisibleChild();
170+
171+
React.useEffect(() => {
172+
const container = document.createElement('div');
173+
container.getBoundingClientRect = () => ({ width: 150 });
174+
175+
const overflow = document.createElement('div');
176+
overflow.getBoundingClientRect = () => ({ width: 30 });
177+
178+
const child1 = document.createElement('div');
179+
child1.getBoundingClientRect = () => ({ width: 50 });
180+
const child2 = document.createElement('div');
181+
child2.getBoundingClientRect = () => ({ width: 60 });
182+
183+
container.appendChild(child1);
184+
container.appendChild(child2);
185+
container.appendChild(overflow);
186+
187+
containerRef.current = container;
188+
overflowRef.current = overflow;
189+
}, []);
190+
191+
return (
192+
<div>
193+
<div ref={containerRef} />
194+
<div data-testid="last-visible-index">{lastVisibleIndex}</div>
195+
</div>
196+
);
197+
};
198+
199+
const { getByTestId, unmount } = render(<TestComponent />);
200+
201+
act(() => {
202+
if (resizeObserverCallback) {
203+
resizeObserverCallback();
204+
}
205+
});
206+
207+
// With overflow width (30) + child1 (50) + child2 (60) = 140 <= 150
208+
// So last visible index should be 1 (child2)
209+
expect(getByTestId('last-visible-index').textContent).toBe('1');
210+
211+
unmount();
212+
});
213+
214+
it('returns -1 when no children fit within container width', () => {
215+
let resizeObserverCallback;
216+
217+
global.ResizeObserver = function (callback) {
218+
resizeObserverCallback = callback;
219+
this.observe = jest.fn();
220+
this.disconnect = jest.fn();
221+
};
222+
223+
const TestComponent = () => {
224+
const [lastVisibleIndex, containerRef] = useIndexOfLastVisibleChild();
225+
226+
React.useEffect(() => {
227+
const container = document.createElement('div');
228+
container.getBoundingClientRect = () => ({ width: 50 });
229+
230+
const child1 = document.createElement('div');
231+
child1.getBoundingClientRect = () => ({ width: 100 });
232+
const child2 = document.createElement('div');
233+
child2.getBoundingClientRect = () => ({ width: 80 });
234+
235+
container.appendChild(child1);
236+
container.appendChild(child2);
237+
238+
containerRef.current = container;
239+
}, []);
240+
241+
return (
242+
<div>
243+
<div ref={containerRef} />
244+
<div data-testid="last-visible-index">{lastVisibleIndex}</div>
245+
</div>
246+
);
247+
};
248+
249+
const { getByTestId, unmount } = render(<TestComponent />);
250+
251+
act(() => {
252+
if (resizeObserverCallback) {
253+
resizeObserverCallback();
254+
}
255+
});
256+
257+
// No children fit within 50px width, so should return -1
258+
expect(getByTestId('last-visible-index').textContent).toBe('-1');
259+
260+
unmount();
261+
});
80262
});

0 commit comments

Comments
 (0)