mirror of
https://github.com/gosticks/react-table.git
synced 2026-08-21 01:10:28 +00:00
126 lines
2.7 KiB
JavaScript
126 lines
2.7 KiB
JavaScript
import '@testing-library/react/cleanup-after-each'
|
|
import '@testing-library/jest-dom/extend-expect'
|
|
|
|
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, 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>
|
|
{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 { getByText, asFragment } = render(<App />)
|
|
|
|
expect(getByText('tanner')).toBeInTheDocument()
|
|
expect(getByText('linsley')).toBeInTheDocument()
|
|
expect(getByText('29')).toBeInTheDocument()
|
|
expect(getByText('100')).toBeInTheDocument()
|
|
expect(getByText('In Relationship')).toBeInTheDocument()
|
|
expect(getByText('50')).toBeInTheDocument()
|
|
|
|
expect(asFragment()).toMatchSnapshot()
|
|
})
|