Skip to content

Commit 2d34937

Browse files
add more tests
1 parent 815c301 commit 2d34937

File tree

5 files changed

+70
-37
lines changed

5 files changed

+70
-37
lines changed

react/src/components/table/Header.jsx

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,30 @@ import React from 'react';
2020
import PropTypes from 'prop-types';
2121
import { TableCell, TableHead, TableRow, TableSortLabel } from '@mui/material';
2222

23-
const SortableCell = ({ prefix, cellInfo, sortHandler, ascending, orderBy }) => {
24-
const direction = ascending ? 'asc' : 'desc';
25-
const sortDirection = (orderBy, id) => (orderBy === id ? direction : false);
26-
const defaultDirection = (orderBy, id) => (orderBy === id ? direction : 'asc');
23+
const ASCENDING = 'asc';
24+
const DESCENDING = 'desc';
2725

28-
return (
29-
<TableCell
30-
data-testid={prefix + '-' + cellInfo.id}
31-
id={prefix + '-' + cellInfo.id}
32-
align={cellInfo.align}
33-
sortDirection={sortDirection(orderBy, cellInfo.id)}
26+
const getDirection = (ascending) => (ascending ? ASCENDING : DESCENDING);
27+
const sortDirection = (orderBy, id, ascending) => (orderBy === id ? getDirection(ascending) : false);
28+
const sortDefaultDirection = (orderBy, id, ascending) => getDirection(ascending || orderBy !== id);
29+
const isActive = (orderBy, id) => orderBy === id;
30+
31+
const SortableCell = ({ prefix, cellInfo, sortHandler, ascending, orderBy }) => (
32+
<TableCell
33+
data-testid={prefix + '-' + cellInfo.id}
34+
id={prefix + '-' + cellInfo.id}
35+
align={cellInfo.align}
36+
sortDirection={sortDirection(orderBy, cellInfo.id, ascending)}
37+
>
38+
<TableSortLabel
39+
active={isActive(orderBy, cellInfo.id)}
40+
direction={sortDefaultDirection(orderBy, cellInfo.id, ascending)}
41+
onClick={sortHandler(cellInfo.id)}
3442
>
35-
<TableSortLabel
36-
active={orderBy === cellInfo.id}
37-
direction={defaultDirection(orderBy, cellInfo.id)}
38-
onClick={sortHandler(cellInfo.id)}
39-
>
40-
{cellInfo.label}
41-
</TableSortLabel>
42-
</TableCell>
43-
);
44-
};
43+
{cellInfo.label}
44+
</TableSortLabel>
45+
</TableCell>
46+
);
4547

4648
// Stryker disable all
4749
SortableCell.propTypes = {
@@ -115,6 +117,6 @@ Header.propTypes = {
115117
};
116118
// Stryker restore all
117119

118-
export { Header, Cell, SortableCell };
120+
export { Header, Cell, SortableCell, getDirection, sortDirection, sortDefaultDirection, isActive };
119121

120122
export default Header;

react/src/components/table/Header.test.jsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import React from 'react';
2020
import { render, screen, fireEvent } from '@testing-library/react';
2121
import '@testing-library/jest-dom';
22-
import { Header, Cell, SortableCell } from './Header';
22+
import { Header, Cell, SortableCell, getDirection, sortDirection, sortDefaultDirection, isActive } from './Header';
2323

2424
describe('Header Suite', () => {
2525
const onRequestSort = jest.fn();
@@ -155,4 +155,27 @@ describe('Header Suite', () => {
155155
expect(document.querySelector('#test-name')).toBeInTheDocument();
156156
});
157157
});
158+
159+
it('getDirection', () => {
160+
expect(getDirection(true)).toBe('asc');
161+
expect(getDirection(false)).toBe('desc');
162+
});
163+
164+
it('isActive', () => {
165+
expect(isActive('id1', 'id1')).toBeTruthy();
166+
expect(isActive('id1', 'id2')).toBeFalsy();
167+
});
168+
169+
it('sortDirection', () => {
170+
expect(sortDirection('id1', 'id1', true)).toBe('asc');
171+
expect(sortDirection('id1', 'id1', false)).toBe('desc');
172+
expect(sortDirection('id1', 'id2', true)).toBeFalsy();
173+
});
174+
175+
it('sortDefaultDirection', () => {
176+
expect(sortDefaultDirection('id1', 'id1', true)).toBe('asc');
177+
expect(sortDefaultDirection('id1', 'id1', false)).toBe('desc');
178+
expect(sortDefaultDirection('id1', 'id2', true)).toBe('asc');
179+
expect(sortDefaultDirection('id1', 'id2', false)).toBe('asc');
180+
});
158181
});

react/src/utils/SortUtils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Bandwidth Monitor
1717
*/
1818

19-
export const comparator = (isAscending, orderBy) => {
19+
const comparator = (isAscending, orderBy) => {
2020
const ascending = (a, b, orderBy) => {
2121
if (a[orderBy] < b[orderBy]) return -1;
2222
if (a[orderBy] > b[orderBy]) return 1;
@@ -30,4 +30,6 @@ export const comparator = (isAscending, orderBy) => {
3030
return isAscending ? (a, b) => ascending(a, b, orderBy) : (a, b) => descending(a, b, orderBy);
3131
};
3232

33-
export const sort = (values, comparator) => [...values].sort(comparator);
33+
const sort = (values, comparator) => [...values].sort(comparator);
34+
35+
export { sort, comparator };

react/src/utils/SortUtils.test.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,45 @@ import { comparator, sort } from './SortUtils';
2020

2121
describe('SortUtils', () => {
2222
describe('comparator', () => {
23-
it('ascending', () => {
23+
it('ascending 1', () => {
24+
const a = { value: 20 };
25+
const b = { value: 10 };
26+
const fn = comparator(true, 'value');
27+
expect(fn(a, b)).toEqual(1);
28+
});
29+
30+
it('ascending -1', () => {
2431
const a = { value: 10 };
2532
const b = { value: 20 };
26-
2733
const fn = comparator(true, 'value');
28-
2934
expect(fn(a, b)).toEqual(-1);
3035
});
3136

32-
it('ascending equals', () => {
37+
it('ascending 0', () => {
3338
const a = { value: 10 };
3439
const b = { value: 10 };
35-
3640
const fn = comparator(true, 'value');
37-
3841
expect(fn(a, b)).toEqual(0);
3942
});
4043

41-
it('descending', () => {
44+
it('descending 1', () => {
4245
const a = { value: 10 };
4346
const b = { value: 20 };
44-
4547
const fn = comparator(false, 'value');
46-
4748
expect(fn(a, b)).toEqual(1);
4849
});
4950

50-
it('descending equals', () => {
51-
const a = { value: 10 };
51+
it('descending -1', () => {
52+
const a = { value: 20 };
5253
const b = { value: 10 };
53-
5454
const fn = comparator(false, 'value');
55+
expect(fn(a, b)).toEqual(-1);
56+
});
5557

58+
it('descending 0', () => {
59+
const a = { value: 10 };
60+
const b = { value: 10 };
61+
const fn = comparator(false, 'value');
5662
expect(fn(a, b)).toEqual(0);
5763
});
5864
});

react/vite.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineConfig({
1313
port: 3000,
1414
open: true,
1515
watch: {
16-
ignored: ['**/reports/**'],
16+
ignored: ['**/reports/**', '**/stryker*/**'],
1717
},
1818
},
1919
plugins: [

0 commit comments

Comments
 (0)