File size: 4,455 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
import React from 'react';
import { spy, stub } from 'sinon';
import { mount, shallow } from 'enzyme';
import { assert } from 'chai';
import getTextLabels from '../src/textLabels';
import TableHeadCell from '../src/components/TableHeadCell';
import TableCell from '@mui/material/TableCell';
import TableSortLabel from '@mui/material/TableSortLabel';
import HelpIcon from '@mui/icons-material/Help';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
describe('<TableHeadCell />', function() {
let classes;
before(() => {
classes = {
root: {},
};
});
it('should add custom props to header cell if "setCellHeaderProps" provided', () => {
const options = { sort: true, textLabels: getTextLabels() };
const toggleSort = () => {};
const setCellHeaderProps = { myProp: 'test', className: 'testClass' };
const selectRowUpdate = stub();
const toggleExpandRow = () => {};
const mountWrapper = mount(
<DndProvider backend={HTML5Backend}>
<TableHeadCell
cellHeaderProps={setCellHeaderProps}
options={options}
sortDirection={'asc'}
sort={true}
toggleSort={toggleSort}
classes={classes}>
some content
</TableHeadCell>
</DndProvider>,
);
const props = mountWrapper.find(TableCell).props();
const classNames = props.className.split(' ');
const finalClass = classNames[classNames.length - 1];
assert.strictEqual(props.myProp, 'test');
assert.strictEqual(finalClass, 'testClass');
});
it('should render a table head cell with sort label when options.sort = true provided', () => {
const options = { sort: true, textLabels: getTextLabels() };
const toggleSort = () => {};
const wrapper = mount(
<DndProvider backend={HTML5Backend}>
<TableHeadCell options={options} sortDirection={'asc'} sort={true} toggleSort={toggleSort} classes={classes}>
some content
</TableHeadCell>
</DndProvider>,
);
const actualResult = wrapper.find(TableSortLabel);
assert.strictEqual(actualResult.length, 1);
});
it('should render a table head cell without sort label when options.sort = false provided', () => {
const options = { sort: false, textLabels: getTextLabels() };
const toggleSort = () => {};
const shallowWrapper = shallow(
<DndProvider backend={HTML5Backend}>
<TableHeadCell options={options} sortDirection={'asc'} sort={true} toggleSort={toggleSort} classes={classes}>
some content
</TableHeadCell>
</DndProvider>,
);
const actualResult = shallowWrapper.find(TableSortLabel);
assert.strictEqual(actualResult.length, 0);
});
it('should render a table help icon when hint provided', () => {
const options = { sort: true, textLabels: getTextLabels() };
const wrapper = mount(
<DndProvider backend={HTML5Backend}>
<TableHeadCell options={options} hint={'hint text'} classes={classes}>
some content
</TableHeadCell>
</DndProvider>,
);
const actualResult = wrapper.find(HelpIcon);
assert.strictEqual(actualResult.length, 1);
});
it('should render a table head cell without custom tooltip when hint provided', () => {
const options = { sort: true, textLabels: getTextLabels() };
const shallowWrapper = shallow(
<DndProvider backend={HTML5Backend}>
<TableHeadCell options={options} classes={classes}>
some content
</TableHeadCell>
</DndProvider>,
).dive();
const actualResult = shallowWrapper.find(HelpIcon);
assert.strictEqual(actualResult.length, 0);
});
it('should trigger toggleSort prop callback when calling method handleSortClick', () => {
const options = { sort: true, textLabels: getTextLabels() };
const toggleSort = spy();
const wrapper = mount(
<DndProvider backend={HTML5Backend}>
<TableHeadCell
options={options}
sort={true}
index={0}
sortDirection={'asc'}
toggleSort={toggleSort}
classes={classes}>
some content
</TableHeadCell>
</DndProvider>,
);
const instance = wrapper
.find('td')
.at(0)
.childAt(0);
const event = { target: { value: 'All' } };
instance.simulate('click');
assert.strictEqual(toggleSort.callCount, 1);
});
});
|