Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
104 changes: 104 additions & 0 deletions __tests__/demo/demo-components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,20 @@ export function DefaultOrderIssue(props) {
birthYear: 2017,
birthCity: 34,
id: 1
},
{
name: 'Mehmet',
surname: 'Terot',
birthYear: 1997,
birthCity: 63,
id: 3
},
{
name: 'Mehmet',
surname: 'Terot',
birthYear: 2000,
birthCity: 34,
id: 4
}
]}
options={{
Expand Down Expand Up @@ -1253,3 +1267,93 @@ export function FixedColumnWithEdit() {
/>
);
}

export function TableMultiSorting(props) {
const global_cols = [
{
title: 'Number',
field: 'number',
minWidth: 140,
maxWidth: 400
},
{
title: 'Title',
field: 'title',
minWidth: 140,
maxWidth: 400,
sorting: true,
defaultSort: 'desc'
},
{
title: 'Name',
field: 'name',
minWidth: 140,
maxWidth: 400,
sorting: true,
defaultSort: 'desc'
},
{
title: 'Last Name',
field: 'lastName',
minWidth: 140,
maxWidth: 400,
sorting: true,
defaultSort: 'asc'
}
];

const global_data1 = [
{
number: 1,
title: 'Developer',
name: 'Mehmet',
lastName: 'Baran',
id: '1231'
},
{
number: 22,
title: 'Developer',
name: 'Pratik',
lastName: 'N',
id: '1234'
},
{
number: 25,
title: 'Human Resources',
name: 'Juan',
lastName: 'Lopez',
id: '1235'
},
{
number: 3,
title: 'Consultant',
name: 'Raul',
lastName: 'Barak',
id: '1236'
}
];

const orderCollection = [
{ orderBy: 1, orderDirection: 'asc', sortOrder: 1 },
{ orderBy: 2, orderDirection: 'desc', sortOrder: 2 }
];

const onOrderCollectionChange = (orderByCollection) => {
console.log('onOrderCollectionChange ===>', orderByCollection);
};

return (
<MaterialTable
data={global_data1}
columns={global_cols}
title="Multi Column Sort"
options={{
selection: false,
maxColumnSort: 2,
defaultOrderByCollection: orderCollection,
showColumnSortOrder: true
}}
onOrderCollectionChange={onOrderCollectionChange}
/>
);
}
6 changes: 5 additions & 1 deletion __tests__/demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ import {
TreeData,
TableWithSummary,
TableWithNumberOfPagesAround,
FixedColumnWithEdit
FixedColumnWithEdit,
TableMultiSorting
} from './demo-components';
import { I1353, I1941, I122 } from './demo-components/RemoteData';
import { Table, TableCell, TableRow, Paper } from '@material-ui/core';
Expand All @@ -60,6 +61,9 @@ render(
<h1>DetailPanelRemounting</h1>
<DetailPanelRemounting />

<h1>Multi Sorting</h1>
<TableMultiSorting />

<h1>Switcher</h1>
<DataSwitcher />

Expand Down
193 changes: 193 additions & 0 deletions __tests__/multiColumnSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import '@testing-library/jest-dom';
import { fireEvent, render } from '@testing-library/react';
import * as React from 'react';
import MaterialTable from '../src';

const columns = [
{
title: 'Number',
field: 'number',
minWidth: 140,
maxWidth: 400
},
{
title: 'Title',
field: 'title',
minWidth: 140,
maxWidth: 400,
sorting: true
},
{
title: 'Name',
field: 'name',
minWidth: 140,
maxWidth: 400,
sorting: true
},
{
title: 'Last Name',
field: 'lastName',
minWidth: 140,
maxWidth: 400,
sorting: true
}
];

const data = [
{
number: 1,
title: 'Developer',
name: 'Mehmet',
lastName: 'Baran',
id: '1231'
},
{
number: 22,
title: 'Developer',
name: 'Pratik',
lastName: 'N',
id: '1234'
},
{
number: 25,
title: 'Human Resources',
name: 'Juan',
lastName: 'Lopez',
id: '1235'
},
{
number: 3,
title: 'Consultant',
name: 'Raul',
lastName: 'Barak',
id: '1236'
}
];

describe('Multi Column Sort', () => {
let initialOrderCollection = [];
let onOrderCollectionChangeSpy;

beforeEach(() => {
jest.clearAllMocks();
onOrderCollectionChangeSpy = jest.fn();
initialOrderCollection = [
{
orderBy: 1,
orderDirection: 'asc',
sortOrder: 1
},
{
orderBy: 2,
orderDirection: 'desc',
sortOrder: 2
}
];
});

test('should update table by multi column', () => {
const { queryAllByTestId } = render(
<MaterialTable
data={data}
columns={columns}
title="Multi Column Sort"
options={{
maxColumnSort: 3
}}
onOrderCollectionChange={onOrderCollectionChangeSpy}
/>
);

const numberColumn = queryAllByTestId('mtableheader-sortlabel')[0];
fireEvent.click(numberColumn);

expect(onOrderCollectionChangeSpy).toHaveBeenCalledWith([
{ sortOrder: 1, orderBy: 0, orderDirection: 'asc' }
]);

const titleColumn = queryAllByTestId('mtableheader-sortlabel')[1];
fireEvent.click(titleColumn);

expect(onOrderCollectionChangeSpy).toHaveBeenCalledWith([
{ sortOrder: 1, orderBy: 0, orderDirection: 'asc' },
{ sortOrder: 2, orderBy: 1, orderDirection: 'asc' }
]);
});

test('should update table by multi column and replace first if reach the maximum order columns', () => {
const { queryAllByTestId } = render(
<MaterialTable
data={data}
columns={columns}
title="Multi Column Sort"
options={{
maxColumnSort: 3
}}
onOrderCollectionChange={onOrderCollectionChangeSpy}
/>
);

const numberColumn = queryAllByTestId('mtableheader-sortlabel')[0];
fireEvent.click(numberColumn);

expect(onOrderCollectionChangeSpy).toHaveBeenCalledWith([
{ sortOrder: 1, orderBy: 0, orderDirection: 'asc' }
]);

fireEvent.click(queryAllByTestId('mtableheader-sortlabel')[1]);
fireEvent.click(queryAllByTestId('mtableheader-sortlabel')[2]);
fireEvent.click(queryAllByTestId('mtableheader-sortlabel')[3]);

expect(onOrderCollectionChangeSpy).toHaveBeenCalledWith([
{ sortOrder: 1, orderBy: 1, orderDirection: 'asc' },
{ sortOrder: 2, orderBy: 2, orderDirection: 'asc' },
{ sortOrder: 3, orderBy: 3, orderDirection: 'asc' }
]);
});

test('should order desc when secon click', () => {
const { queryAllByTestId } = render(
<MaterialTable
data={data}
columns={columns}
title="Multi Column Sort"
options={{
maxColumnSort: 3
}}
onOrderCollectionChange={onOrderCollectionChangeSpy}
/>
);

const numberColumn = queryAllByTestId('mtableheader-sortlabel')[0];
fireEvent.click(numberColumn);
fireEvent.click(numberColumn);

expect(onOrderCollectionChangeSpy).toHaveBeenCalledWith([
{ sortOrder: 1, orderBy: 0, orderDirection: 'desc' }
]);
});

test('should have being initialized by defaultOrderByCollection', () => {
const { queryAllByTestId } = render(
<MaterialTable
data={data}
columns={columns}
title="Multi Column Sort"
options={{
maxColumnSort: 3,
defaultOrderByCollection: initialOrderCollection
}}
onOrderCollectionChange={onOrderCollectionChangeSpy}
/>
);

const numberColumn = queryAllByTestId('mtableheader-sortlabel')[0];
fireEvent.click(numberColumn);

expect(onOrderCollectionChangeSpy).toHaveBeenCalledWith([
{ sortOrder: 1, orderBy: 1, orderDirection: 'asc' },
{ sortOrder: 2, orderBy: 2, orderDirection: 'desc' },
{ sortOrder: 3, orderBy: 0, orderDirection: 'asc' }
]);
});
});
3 changes: 3 additions & 0 deletions __tests__/pre.build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('Render Table : Pre Build', () => {
expect(screen.getAllByRole('table')).toHaveLength(2);
});
});

// Render table with data
describe('when attempting to render a table with data', () => {
it('renders without crashing', () => {
Expand Down Expand Up @@ -78,6 +79,7 @@ describe('Render Table : Pre Build', () => {
name: /5 rows First Page Previous Page 1-5 of 99 Next Page Last Page/i
});
});

it('navigates between the pages', () => {
const data = makeData();
render(<MaterialTable data={data} columns={columns} />);
Expand Down Expand Up @@ -125,6 +127,7 @@ describe('Render Table : Pre Build', () => {
});
expect(screen.getAllByRole('row')).toHaveLength(8);
});

it('filters data by search input', async () => {
const data = makeData();
render(<MaterialTable data={data} columns={columns} />);
Expand Down
Loading