|
| 1 | +import React from 'react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import Waterfall from '../index'; |
| 4 | +import { WaterfallItem } from '../types'; |
| 5 | + |
| 6 | +const items: WaterfallItem[] = [ |
| 7 | + { key: '1', data: 100 }, |
| 8 | + { key: '2', data: 150 }, |
| 9 | + { key: '3', data: 80 }, |
| 10 | + { key: '4', data: 120 }, |
| 11 | + { key: '5', data: 90 }, |
| 12 | +]; |
| 13 | + |
| 14 | +describe('<Waterfall />', () => { |
| 15 | + it('should match the snapshot', () => { |
| 16 | + const { asFragment } = render( |
| 17 | + <Waterfall |
| 18 | + columns={3} |
| 19 | + gutter={16} |
| 20 | + items={items} |
| 21 | + itemRender={({ data, index }) => ( |
| 22 | + <div style={{ height: data }}>{index + 1}</div> |
| 23 | + )} |
| 24 | + />, |
| 25 | + ); |
| 26 | + expect(asFragment()).toMatchSnapshot(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should render correct number of items', () => { |
| 30 | + const { container } = render( |
| 31 | + <Waterfall |
| 32 | + columns={3} |
| 33 | + items={items} |
| 34 | + itemRender={({ data, index }) => ( |
| 35 | + <div style={{ height: data }}>{index + 1}</div> |
| 36 | + )} |
| 37 | + />, |
| 38 | + ); |
| 39 | + expect(container.querySelectorAll('.ty-waterfall__item')).toHaveLength(5); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should apply correct prefix class', () => { |
| 43 | + const { container } = render( |
| 44 | + <Waterfall columns={2} items={items} itemRender={() => <div />} />, |
| 45 | + ); |
| 46 | + expect(container.firstChild).toHaveClass('ty-waterfall'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should accept custom className and style', () => { |
| 50 | + const { container } = render( |
| 51 | + <Waterfall |
| 52 | + className="custom-cls" |
| 53 | + style={{ background: 'red' }} |
| 54 | + columns={2} |
| 55 | + items={items} |
| 56 | + itemRender={() => <div />} |
| 57 | + />, |
| 58 | + ); |
| 59 | + const el = container.firstChild as HTMLElement; |
| 60 | + expect(el).toHaveClass('ty-waterfall'); |
| 61 | + expect(el).toHaveClass('custom-cls'); |
| 62 | + expect(el.style.background).toBe('red'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should render items with children prop directly', () => { |
| 66 | + const itemsWithChildren: WaterfallItem[] = [ |
| 67 | + { key: '1', children: <span>Direct Content</span> }, |
| 68 | + ]; |
| 69 | + const { getByText } = render( |
| 70 | + <Waterfall columns={2} items={itemsWithChildren} />, |
| 71 | + ); |
| 72 | + expect(getByText('Direct Content')).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should render empty when no items provided', () => { |
| 76 | + const { container } = render(<Waterfall columns={3} />); |
| 77 | + expect(container.querySelectorAll('.ty-waterfall__item')).toHaveLength(0); |
| 78 | + }); |
| 79 | +}); |
0 commit comments