Skip to content

Commit ea02fa9

Browse files
committed
Fixed topOffset and added tests (#2065)
1 parent 709d88b commit ea02fa9

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react';
2+
import { renderHook } from '@testing-library/react-hooks';
3+
import { Item, Section } from '../shared';
4+
import { ItemElement } from './itemUtils';
5+
import { useStaticItemInitialScrollPosition } from './useStaticItemInitialScrollPosition';
6+
7+
beforeEach(() => {
8+
jest.clearAllMocks();
9+
});
10+
11+
afterEach(() => {
12+
expect.hasAssertions();
13+
});
14+
15+
const mockSelectedKey = 'selected.key';
16+
17+
describe('useStaticItemInitialScrollPosition: selectedKey: %s', () => {
18+
const itemHeight = 32;
19+
const topOffset = 20;
20+
21+
const items = {
22+
empty: [],
23+
mixed: [<Item key="">Item</Item>, <Section key="">Test</Section>],
24+
only: [
25+
<Item key="a">Item</Item>,
26+
<Item key="b">Item</Item>,
27+
<Item key="c">Item</Item>,
28+
<Item key={mockSelectedKey}>Item</Item>,
29+
<Item key="e">Item</Item>,
30+
],
31+
} satisfies Record<string, ItemElement<unknown>[]>;
32+
33+
it.each([
34+
[items.empty, undefined, topOffset],
35+
[items.mixed, undefined, topOffset],
36+
[items.only, undefined, topOffset],
37+
[items.empty, mockSelectedKey, topOffset],
38+
[items.mixed, mockSelectedKey, topOffset],
39+
[items.only, mockSelectedKey, topOffset + 3 * itemHeight],
40+
])(
41+
'should return a function that returns the initial scroll position for item only collections: %s, %s',
42+
async (givenItems, selectedKey, expected) => {
43+
const { result } = renderHook(() =>
44+
useStaticItemInitialScrollPosition({
45+
itemHeight,
46+
selectedKey,
47+
topOffset,
48+
items: givenItems,
49+
})
50+
);
51+
52+
const actual = await result.current();
53+
54+
expect(actual).toEqual(expected);
55+
}
56+
);
57+
});

packages/components/src/spectrum/utils/useStaticItemInitialScrollPosition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function useStaticItemInitialScrollPosition({
3535
const getInitialScrollPosition = useCallback(
3636
async () =>
3737
disableScrollOnOpen
38-
? null
38+
? topOffset
3939
: getPositionOfSelectedItemElement({
4040
items,
4141
itemHeight,

0 commit comments

Comments
 (0)