UNPKG

react-table

Version:

Hooks for building lightweight, fast and extendable datagrids for React

127 lines (119 loc) 2.74 kB
import React from 'react' import { render } from '@testing-library/react' import { useTable } from '../useTable' const data = [ { firstName: 'tanner', lastName: 'linsley', age: 29, visits: 100, status: 'In Relationship', progress: 50, }, { firstName: 'derek', lastName: 'perkins', age: 40, visits: 40, status: 'Single', progress: 80, }, { firstName: 'joe', lastName: 'bergevin', age: 45, visits: 20, status: 'Complicated', progress: 10, }, ] function Table({ columns, data }) { // Use the state and functions returned from useTable to build your UI const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow, } = useTable({ columns, data, }) // Render the UI for your table return ( <table {...getTableProps()}> <thead> {headerGroups.map(headerGroup => ( <tr {...headerGroup.getHeaderGroupProps()}> {headerGroup.headers.map(column => ( <th {...column.getHeaderProps()}>{column.render('Header')}</th> ))} </tr> ))} </thead> <tbody {...getTableBodyProps()}> {rows.map( (row, i) => prepareRow(row) || ( <tr {...row.getRowProps()}> {row.cells.map(cell => { return <td {...cell.getCellProps()}>{cell.render('Cell')}</td> })} </tr> ) )} </tbody> </table> ) } function App() { const columns = React.useMemo( () => [ { Header: 'Name', columns: [ { Header: 'First Name', accessor: 'firstName', }, { Header: 'Last Name', accessor: 'lastName', }, ], }, { Header: 'Info', columns: [ { Header: 'Age', accessor: 'age', }, { Header: 'Visits', accessor: 'visits', }, { Header: 'Status', accessor: 'status', }, { Header: 'Profile Progress', accessor: 'progress', }, ], }, ], [] ) return <Table columns={columns} data={data} /> } test('renders a basic table', () => { const rtl = render(<App />) expect(rtl.getByText('tanner')).toBeInTheDocument() expect(rtl.getByText('linsley')).toBeInTheDocument() expect(rtl.getByText('29')).toBeInTheDocument() expect(rtl.getByText('100')).toBeInTheDocument() expect(rtl.getByText('In Relationship')).toBeInTheDocument() expect(rtl.getByText('50')).toBeInTheDocument() })