Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Table to TypeScript #852

Merged
merged 27 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2637e06
feat(components): migrate core Table component to TypeScript
Mar 19, 2021
2936092
fix(tests): fix unused variable error and update snapshots
Mar 23, 2021
6f515d4
feat(components): migrate SortArrow to TypeScript
Mar 23, 2021
bef9446
feat(components): migrate TableCell to TypeScript
Mar 25, 2021
2710c09
refactor: amend tests to use getByRole instead of testids
Mar 26, 2021
e5db60a
fix: forward scope prop to table header cells
Mar 26, 2021
bdba32f
feat(components): migrate TableRow to TypeScript
Mar 30, 2021
76740b9
fix(tests): update snapshots
Mar 30, 2021
0d4cc57
fix(components): mark data-testid prop in TableCell as optional
Mar 30, 2021
269f817
feat(components): migrate TableHeader to TypeScript
Mar 31, 2021
4d3ead7
Merge branch 'canary' of github.com:sumup-oss/circuit-ui into feat/mi…
Mar 31, 2021
46da2c8
feat(components): migrate TableHead to TypeScript
Apr 6, 2021
bd6b3ed
feat(components): migrate TableBody to TypeScript
Apr 6, 2021
1f1dfbe
fix(components) handle falsy sortByValue
Apr 6, 2021
c8941a4
fix(components): pass a falsy sortedRow
Apr 6, 2021
d99a713
refactor: reuse types and switch defaults from null to undefined
Apr 7, 2021
49cb79a
refactor: reuse Direction type across Table components
Apr 7, 2021
f2ebfb6
fix(components): fix CellObject type to accept a ReactNode as children
Apr 7, 2021
dabefff
fix(components): reuse Cell type in Table component
Apr 7, 2021
efc3508
refactor: simplify types
Apr 7, 2021
afc124d
cleanup: remove leftover comments in utils.ts
Apr 7, 2021
e0ddf95
fix: address code review feedback
Apr 9, 2021
c7c61e9
fix: revert renaming of height attr and update types
Apr 9, 2021
30a8578
fix: reuse types from component props in static TableHeader methods
Apr 12, 2021
8c5eae5
fix: make TableProps extend HTML table element and spread props to wr…
Apr 12, 2021
aae153e
Add changesets
Apr 12, 2021
b3c6637
Specify that changeset applies to the Table component
Apr 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@

import React from 'react';

import {
create,
render,
renderToHtml,
axe,
act,
userEvent,
} from '../../util/test-utils';
import Badge from '../Badge';

import Table from './Table';
import { ASCENDING } from './constants';

const headers = [
{ children: 'Letters', sortable: true },
Expand Down Expand Up @@ -69,7 +78,24 @@ describe('Table', () => {
});

it('should not render a scrollable table if the rowHeaders prop is true', () => {
const actual = create(<Table headers={headers} rows={rows} />);
const actual = create(<Table headers={headers} rows={rows} rowHeaders />);
expect(actual).toMatchSnapshot();
});

it('should render with component cells', () => {
robinmetral marked this conversation as resolved.
Show resolved Hide resolved
const actual = create(
<Table
headers={['Name', 'Type']}
rows={[
['Apple', 'Fruit'],
['Broccoli', 'Vegetable'],
[
'Chickpeas',
{ children: <Badge color={'warning'}>Unknown</Badge> },
],
]}
/>,
);
expect(actual).toMatchSnapshot();
});
});
Expand All @@ -83,7 +109,7 @@ describe('Table', () => {
);

act(() => {
fireEvent.click(getAllByTestId('table-row')[0]);
userEvent.click(getAllByTestId('table-row')[0]);
});

expect(onRowClickMock).toHaveBeenCalledTimes(1);
Expand All @@ -92,60 +118,58 @@ describe('Table', () => {

describe('sorting', () => {
it('should sort a column in ascending order', () => {
const { getAllByTestId } = render(
<Table rows={rows} headers={headers} />,
const { getAllByRole } = render(
<Table rows={rows} headers={headers} rowHeaders={false} />,
);

const letterHeaderEl = getAllByTestId('table-header')[0];
const cellEls = getAllByTestId('table-cell');
const letterHeaderEl = getAllByRole('columnheader')[0];
const cellEls = getAllByRole('cell');

act(() => {
fireEvent.click(letterHeaderEl);
userEvent.click(letterHeaderEl);
});

const sortedRow = ['a', 'b', 'c'];

rows.forEach((row, index) => {
// There's a hidden header cell that we need to skip with +1.
const cellIndex = rowLength * index + 1;
rows.forEach((_row, index) => {
const cellIndex = rowLength * index;
expect(cellEls[cellIndex]).toHaveTextContent(sortedRow[index]);
});
});

it('should sort a column in descending order', () => {
const { getAllByTestId } = render(
<Table rows={rows} headers={headers} />,
const { getAllByRole } = render(
<Table rows={rows} headers={headers} rowHeaders={false} />,
);

const letterHeaderEl = getAllByTestId('table-header')[0];
const cellEls = getAllByTestId('table-cell');
const letterHeaderEl = getAllByRole('columnheader')[0];
const cellEls = getAllByRole('cell');

act(() => {
fireEvent.click(letterHeaderEl);
userEvent.click(letterHeaderEl);
});
act(() => {
fireEvent.click(letterHeaderEl);
userEvent.click(letterHeaderEl);
});

const sortedRow = ['c', 'b', 'a'];

rows.forEach((row, index) => {
// There's a hidden header cell that we need to skip with +1.
const cellIndex = rowLength * index + 1;
rows.forEach((_row, index) => {
const cellIndex = rowLength * index;
expect(cellEls[cellIndex]).toHaveTextContent(sortedRow[index]);
});
});

it('should call a custom sort callback', () => {
const onSortByMock = jest.fn();
const index = 0;
const nextDirection = ASCENDING;
const nextDirection = 'ascending';
const { getAllByTestId } = render(
<Table onSortBy={onSortByMock} headers={headers} rows={rows} />,
);

act(() => {
fireEvent.click(getAllByTestId('table-header')[0]);
userEvent.click(getAllByTestId('table-header')[0]);
});

expect(onSortByMock).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { action } from '@storybook/addon-actions';
import Badge from '../Badge';

import docs from './Table.docs.mdx';
import { TableProps } from './Table';

import Table, { TableHeader, TableRow, TableCell } from '.';

Expand All @@ -31,7 +32,7 @@ export default {
},
};

export const Base = ({ onSortBy, ...args }) => <Table {...args} />;
export const Base = ({ onSortBy, ...args }: TableProps) => <Table {...args} />;

Base.args = {
headers: [
Expand All @@ -43,7 +44,7 @@ Base.args = {
rows: [
{
'cells': [
'Lorem ipsum dolor',
'Lorem ipsum',
{
'children': '12/01/2017',
'sortByValue': 0,
Expand All @@ -55,13 +56,13 @@ Base.args = {
'data-selector': 'item-1',
},
[
'Ipsum dolor sit amet',
'Consectetur adipiscing',
{ children: '13/01/2017', sortByValue: 1 },
'Virtual Terminal',
{ children: 'Enabled', align: 'right' },
],
[
'Dolor sit amet, consectetur adipiscing',
'Dolor sit amet',
{ children: '14/01/2017', sortByValue: 2 },
'-',
{ children: 'Disabled', align: 'right' },
Expand All @@ -71,7 +72,9 @@ Base.args = {
onRowClick: action('onRowClick'),
};

export const WithComponentRows = ({ onSortBy, ...args }) => <Table {...args} />;
export const WithComponentRows = ({ onSortBy, ...args }: TableProps) => (
<Table {...args} />
);

WithComponentRows.args = {
headers: ['Name', 'Type'],
Expand All @@ -82,7 +85,9 @@ WithComponentRows.args = {
],
};

export const Sortable = ({ onSortBy, ...args }) => <Table {...args} />;
export const Sortable = ({ onSortBy, ...args }: TableProps) => (
<Table {...args} />
);

Sortable.args = {
headers: [
Expand Down Expand Up @@ -114,13 +119,17 @@ Sortable.args = {
],
};

export const CustomSort = (args) => (
export const CustomSort = (args: TableProps) => (
<Table
{...args}
onSortBy={(i, direction, rows) =>
onSortBy={(_i, direction, rows) =>
direction === 'ascending'
? rows.sort((a, b) => a[0].localeCompare(b[0]))
: rows.sort((a, b) => b[0].localeCompare(a[0]))
? rows.sort(
(a, b) => typeof a[0] === 'string' && a[0].localeCompare(b[0]),
)
: rows.sort(
(a, b) => typeof b[0] === 'string' && b[0].localeCompare(a[0]),
)
}
/>
);
Expand Down
Loading