import { Fragment } from 'react'; import { screen, render, fireEvent } from '@testing-library/react'; import ExpandableList, { ExpandableMessage } from '../expandable-list'; const descriptionString = 'foo'; describe('ExpandableMessage', () => { let setExpanded: () => void; beforeEach(() => { setExpanded = jest.fn(); }); it('should render', () => { const { asFragment } = render( ); expect(asFragment()).toMatchSnapshot(); }); it('should call setExpanded when button is clicked and button text be "More descriptionString"', () => { render( ); const button = screen.getByRole('button'); fireEvent.click(button); expect(setExpanded).toHaveBeenCalled(); expect(button.textContent).toEqual(`More ${descriptionString}`); }); describe('button text', () => { it('should be "Fewer descriptionString"', () => { render( ); const button = screen.getByRole('button'); fireEvent.click(button); expect(setExpanded).toHaveBeenCalled(); expect(button.textContent).toEqual(`Fewer ${descriptionString}`); }); it('should include the number of hidden items', () => { render( ); const button = screen.getByRole('button'); fireEvent.click(button); expect(setExpanded).toHaveBeenCalled(); expect(button.textContent).toEqual(`4 more ${descriptionString}`); }); }); describe('button text, show/hide', () => { it('should be "Hide descriptionString"', () => { render( ); const button = screen.getByRole('button'); fireEvent.click(button); expect(setExpanded).toHaveBeenCalled(); expect(button.textContent).toEqual(`Hide ${descriptionString}`); }); it('should include the number of hidden items', () => { render( ); const button = screen.getByRole('button'); fireEvent.click(button); expect(setExpanded).toHaveBeenCalled(); expect(button.textContent).toEqual(`Show 4 ${descriptionString}`); }); }); }); describe('ExpandableList', () => { const numberItems = 10; const numberCollapsedItems = 5; const items = Array.from({ length: numberItems }, (_, index) => ( some content {index} )); it('should render', () => { const { asFragment } = render( {items} ); expect(asFragment()).toMatchSnapshot(); }); it('should not render when no children', () => { const { container } = render( ); expect(container).toBeEmptyDOMElement(); }); it('should not render when empty list as children', () => { const { container } = render( {[]} ); expect(container).toBeEmptyDOMElement(); }); it('should not show more button if numberItems <= numberCollapsedItems + 1', () => { render( {items.slice(0, numberCollapsedItems + 1)} ); expect(screen.queryByTestId('expandable-message')).toBeNull(); }); it('should show "show" button if numberCollapsedItems === 0', () => { render({items}); expect(screen.getByTestId('expandable-message')).toBeInTheDocument(); }); it('should not show more button if numberItems > numberCollapsedItems + 1', () => { render( {items} ); expect(screen.getByTestId('expandable-message')).toBeInTheDocument(); }); it('should initially have numberCollapsedItems and then total number of items after More button click', () => { const { container } = render( {items} ); let listItems = container.querySelectorAll('li'); expect(listItems).toHaveLength(numberCollapsedItems + 1); const button = screen.getByRole('button'); fireEvent.click(button); listItems = container.querySelectorAll('li'); expect(listItems).toHaveLength(numberItems + 1); }); });