|
| 1 | +import React from 'react'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { cleanup, render, screen, waitFor } from '@testing-library/react'; |
| 4 | +import { VirtualList, type VirtualListProps } from './virtual-list'; |
| 5 | + |
| 6 | +type Doc = { |
| 7 | + id: number; |
| 8 | + renderHeight: number; |
| 9 | +}; |
| 10 | + |
| 11 | +const docs: Doc[] = [ |
| 12 | + { |
| 13 | + id: 1, |
| 14 | + renderHeight: 200, |
| 15 | + }, |
| 16 | + { |
| 17 | + id: 2, |
| 18 | + renderHeight: 10, |
| 19 | + }, |
| 20 | + { |
| 21 | + id: 3, |
| 22 | + renderHeight: 500, |
| 23 | + }, |
| 24 | +]; |
| 25 | + |
| 26 | +function defaultItemRenderer(doc: Doc, ref: React.Ref<HTMLDivElement>) { |
| 27 | + return ( |
| 28 | + <div ref={ref} style={{ height: `${doc.renderHeight}px` }}> |
| 29 | + Div - {doc.id} |
| 30 | + </div> |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +const defaultProps: VirtualListProps<Doc> = { |
| 35 | + items: docs, |
| 36 | + renderItem: defaultItemRenderer, |
| 37 | + estimateItemInitialHeight(item) { |
| 38 | + return item.renderHeight; |
| 39 | + }, |
| 40 | + __TEST_LIST_WIDTH: 300, |
| 41 | + __TEST_LIST_HEIGHT: 800, |
| 42 | +}; |
| 43 | + |
| 44 | +describe('VirtualList', function () { |
| 45 | + afterEach(function () { |
| 46 | + cleanup(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('renders the docs in the visible viewport', async function () { |
| 50 | + render(<VirtualList {...defaultProps} />); |
| 51 | + await waitFor(() => { |
| 52 | + // Expecting all three divs to be visible because the viewport can house |
| 53 | + // them all |
| 54 | + expect(screen.getByText('Div - 1')).to.be.visible; |
| 55 | + expect(screen.getByText('Div - 2')).to.be.visible; |
| 56 | + expect(screen.getByText('Div - 3')).to.be.visible; |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('respects the overscan count', async function () { |
| 61 | + render( |
| 62 | + <VirtualList |
| 63 | + {...defaultProps} |
| 64 | + overScanCount={1} |
| 65 | + __TEST_LIST_HEIGHT={10} |
| 66 | + /> |
| 67 | + ); |
| 68 | + await waitFor(() => { |
| 69 | + expect(screen.getByText('Div - 1')).to.be.visible; |
| 70 | + expect(() => screen.getByText('Div - 2')).to.throw; |
| 71 | + expect(() => screen.getByText('Div - 3')).to.throw; |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('respects the rowGap', function () { |
| 76 | + render( |
| 77 | + <VirtualList {...defaultProps} rowGap={4} itemDataTestId="list-item" /> |
| 78 | + ); |
| 79 | + |
| 80 | + const listItems = screen.getAllByTestId('list-item'); |
| 81 | + // The height of each list item is <rowGap> + item size |
| 82 | + expect(listItems[0].style.height).to.equal(`${200 + 4}px`); |
| 83 | + expect(listItems[1].style.height).to.equal(`${10 + 4}px`); |
| 84 | + // last item does not get a height change |
| 85 | + expect(listItems[2].style.height).to.equal('500px'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('applies the initialScrollTop', function () { |
| 89 | + const scrollRef = React.createRef<HTMLDivElement>(); |
| 90 | + render( |
| 91 | + <VirtualList |
| 92 | + {...defaultProps} |
| 93 | + initialScrollTop={100} |
| 94 | + scrollableContainerRef={scrollRef} |
| 95 | + /> |
| 96 | + ); |
| 97 | + expect(scrollRef.current?.scrollTop).to.equal(100); |
| 98 | + }); |
| 99 | +}); |
0 commit comments