|
| 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 | +}); |
0 commit comments