import React, { Fragment } from 'react'; import { flexRender } from '@tanstack/react-table'; import { fireEvent, render } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { Cell, HeaderCell } from '../Cell'; import ExpandedContent from '../ExpandedContent'; import { HeaderRow, Row } from '../Row'; import Table from '../Table'; import TableBody from '../TableBody'; import TableHead from '../TableHead'; import { LeafyGreenTableCell, LeafyGreenTableRow } from '../useLeafyGreenTable'; import { Person } from '../utils/makeData.testutils'; import { TestTableWithHookProps, useTestHookCall, } from '../utils/testHookCalls.testutils'; import { getTestUtils } from './getTestUtils'; function TableWithHook(props: TestTableWithHookProps) { // @ts-ignore const { ['data-lgid']: dataLgId, isDisabled = false, ...rest } = props; const { table, rowSelection } = useTestHookCall({ rowProps: { renderExpandedContent: (_: LeafyGreenTableRow) => { return <>Expandable content test; }, }, ...rest, }); const { rows } = table.getRowModel(); return ( <>
{JSON.stringify(rowSelection)}
{table.getHeaderGroups().map(headerGroup => ( {headerGroup.headers.map(header => { return ( {flexRender( header.column.columnDef.header, header.getContext(), )} ); })} ))} {rows.map((row: LeafyGreenTableRow) => { const isExpandedContent = row.isExpandedContent ?? false; return ( {!isExpandedContent && ( {row .getVisibleCells() .map((cell: LeafyGreenTableCell) => { return ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ); })} )} {isExpandedContent && } ); })}
); } describe('packages/table', () => { describe('getTestUtils', () => { test('throws error if LG Table is not found', () => { render(); try { const _utils = getTestUtils(); } catch (error) { expect(error).toBeInstanceOf(Error); expect(error).toHaveProperty( 'message', expect.stringMatching( /Unable to find an element by: \[data-lgid="lg-table"\]/, ), ); } }); describe('single table', () => { describe('getTable', () => { test('is in the document', () => { render(); const { getTable } = getTestUtils(); expect(getTable()).toBeInTheDocument(); }); }); describe('getAllHeaders', () => { test('returns all the headers', () => { render(); const { getAllHeaders } = getTestUtils(); expect(getAllHeaders().length).toEqual(6); }); }); describe('getHeaderByIndex', () => { test('returns the correct header', () => { render(); const { getHeaderByIndex } = getTestUtils(); expect(getHeaderByIndex(0)?.getElement()).toHaveTextContent('ID'); }); describe('sort icon', () => { test('returns the sort icon', () => { render(); const { getHeaderByIndex } = getTestUtils(); expect(getHeaderByIndex(0)?.getSortIcon()).toBeInTheDocument(); }); test('returns null', () => { render(); const { getHeaderByIndex } = getTestUtils(); expect(getHeaderByIndex(0)?.getSortIcon()).not.toBeInTheDocument(); }); }); }); describe('getSelectAllCheckbox', () => { test('returns the select all checkbox', () => { render(); const { getSelectAllCheckbox } = getTestUtils(); expect(getSelectAllCheckbox()).toBeInTheDocument(); }); test('returns null', () => { render(); const { getSelectAllCheckbox } = getTestUtils(); expect(getSelectAllCheckbox()).not.toBeInTheDocument(); }); }); describe('getAllVisibleRows', () => { test('returns all the visible rows', () => { render(); const { getAllVisibleRows } = getTestUtils(); expect(getAllVisibleRows().length).toEqual(3); }); test('throws an error if there are no visible rows', async () => { render(); const { getAllVisibleRows } = getTestUtils(); expect(() => getAllVisibleRows()).toThrow(); }); }); describe('getRowByIndex', () => { describe('getElement', () => { test('returns the element', () => { render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(0)?.getElement()).toBeInTheDocument(); }); }); describe('getAllCells', () => { test('returns all the cells in the row', () => { render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(0)?.getAllCells().length).toEqual(6); }); }); describe('getCheckbox', () => { test('returns the checkbox toggle', () => { render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(0)?.getCheckbox()).toBeInTheDocument(); }); test('returns null', () => { render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(0)?.getCheckbox()).not.toBeInTheDocument(); }); }); describe('getExpandButton', () => { test('returns the expand toggle', () => { render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(0)?.getExpandButton()).toBeInTheDocument(); }); test('returns null', () => { render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(1)?.getExpandButton()).not.toBeInTheDocument(); }); }); describe('isExpanded', () => { test('returns false', () => { render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(0)?.isExpanded()).toBeFalsy(); }); test('returns true', () => { render(); const { getRowByIndex } = getTestUtils(); const expandButton = getRowByIndex(0)?.getExpandButton(); userEvent.click(expandButton!); expect(getRowByIndex(0)?.isExpanded()).toBeTruthy(); }); }); describe('isSelected', () => { test('returns false', () => { render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(0)?.isSelected()).toBeFalsy(); }); test('returns true', () => { render(); const { getRowByIndex } = getTestUtils(); const checkbox = getRowByIndex(0)?.getCheckbox(); fireEvent.click(checkbox!); expect(getRowByIndex(0)?.isSelected()).toBeTruthy(); }); }); describe('isDisabled', () => { test('returns false', () => { render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(0)?.isDisabled()).toBeFalsy(); }); test('returns true', () => { // @ts-ignore - isDisabled prop render(); const { getRowByIndex } = getTestUtils(); expect(getRowByIndex(0)?.isDisabled()).toBeTruthy(); }); }); }); describe('getAllVisibleSelectedRows', () => { test('returns the correct number of selected visible cells', () => { render(); const { getSelectAllCheckbox, getAllVisibleSelectedRows } = getTestUtils(); const checkbox = getSelectAllCheckbox(); fireEvent.click(checkbox!); expect(getAllVisibleSelectedRows().length).toEqual(3); }); test('returns an empty array', () => { render(); const { getAllVisibleSelectedRows } = getTestUtils(); expect(getAllVisibleSelectedRows().length).toEqual(0); }); }); }); describe('multiple tables', () => { describe('getAllVisibleRows', () => { test('returns the correct rows', () => { render( <> , ); const { getAllVisibleRows } = getTestUtils(); expect(getAllVisibleRows().length).toEqual(3); const { getAllVisibleRows: getAllVisibleRowsB } = getTestUtils('lg-table-2'); expect(getAllVisibleRowsB().length).toEqual(4); }); }); }); }); });