Skip to content

Commit

Permalink
Fixed topOffset and added tests (deephaven#2065)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Jun 12, 2024
1 parent 709d88b commit ea02fa9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { Item, Section } from '../shared';
import { ItemElement } from './itemUtils';
import { useStaticItemInitialScrollPosition } from './useStaticItemInitialScrollPosition';

beforeEach(() => {
jest.clearAllMocks();
});

afterEach(() => {
expect.hasAssertions();
});

const mockSelectedKey = 'selected.key';

describe('useStaticItemInitialScrollPosition: selectedKey: %s', () => {
const itemHeight = 32;
const topOffset = 20;

const items = {
empty: [],
mixed: [<Item key="">Item</Item>, <Section key="">Test</Section>],
only: [
<Item key="a">Item</Item>,
<Item key="b">Item</Item>,
<Item key="c">Item</Item>,
<Item key={mockSelectedKey}>Item</Item>,
<Item key="e">Item</Item>,
],
} satisfies Record<string, ItemElement<unknown>[]>;

it.each([
[items.empty, undefined, topOffset],
[items.mixed, undefined, topOffset],
[items.only, undefined, topOffset],
[items.empty, mockSelectedKey, topOffset],
[items.mixed, mockSelectedKey, topOffset],
[items.only, mockSelectedKey, topOffset + 3 * itemHeight],
])(
'should return a function that returns the initial scroll position for item only collections: %s, %s',
async (givenItems, selectedKey, expected) => {
const { result } = renderHook(() =>
useStaticItemInitialScrollPosition({
itemHeight,
selectedKey,
topOffset,
items: givenItems,
})
);

const actual = await result.current();

expect(actual).toEqual(expected);
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function useStaticItemInitialScrollPosition({
const getInitialScrollPosition = useCallback(
async () =>
disableScrollOnOpen
? null
? topOffset
: getPositionOfSelectedItemElement({
items,
itemHeight,
Expand Down

0 comments on commit ea02fa9

Please sign in to comment.