import React from 'react';
import { spy, stub } from 'sinon';
import { mount, shallow } from 'enzyme';
import { assert, expect, should } from 'chai';
import TableHead from '../src/components/TableHead';
import TableHeadCell from '../src/components/TableHeadCell';
import Checkbox from '@mui/material/Checkbox';
import Tooltip from '@mui/material/Tooltip';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
describe('', function() {
let columns;
let handleHeadUpdateRef;
before(() => {
columns = [
{ name: 'First Name', label: 'First Name', display: 'true', sort: true },
{ name: 'Company', label: 'Company', display: 'true', sort: null },
{ name: 'City', label: 'City Label', display: 'true', sort: null },
{
name: 'State',
label: 'State',
display: 'true',
options: { fixedHeaderOptions: { xAxis: true, yAxis: true }, selectableRows: 'multiple' },
customHeadRender: columnMeta => {columnMeta.name + 's'},
sort: null,
},
];
handleHeadUpdateRef = () => {};
});
it('should render a table head', () => {
const options = {};
const toggleSort = () => {};
const mountWrapper = mount(
{}}
handleHeadUpdateRef={handleHeadUpdateRef}
toggleSort={toggleSort}
/>
,
);
const actualResult = mountWrapper.find(TableHeadCell);
assert.strictEqual(actualResult.length, 4);
});
it('should render the label in the table head cell', () => {
const options = {};
const toggleSort = () => {};
const mountWrapper = mount(
{}}
handleHeadUpdateRef={handleHeadUpdateRef}
toggleSort={toggleSort}
/>
,
);
const labels = mountWrapper.find(TableHeadCell).map(n => n.text());
assert.deepEqual(labels, ['First Name', 'Company', 'City Label', 'States']);
});
it('should render a table head with no cells', () => {
const options = {};
const toggleSort = () => {};
const newColumns = columns.map(column => ({ ...column, display: false }));
const mountWrapper = mount(
{}}
handleHeadUpdateRef={handleHeadUpdateRef}
toggleSort={toggleSort}
/>
,
);
const actualResult = mountWrapper.find(TableHeadCell);
assert.strictEqual(actualResult.length, 0);
});
it('should trigger toggleSort prop callback when calling method handleToggleColumn', () => {
const options = { sort: true };
const toggleSort = spy();
const wrapper = mount(
{}}
handleHeadUpdateRef={handleHeadUpdateRef}
toggleSort={toggleSort}
/>
,
);
const instance = wrapper.find('th span').at(0);
instance.simulate('click');
assert.strictEqual(toggleSort.callCount, 1);
});
it('should trigger selectRowUpdate prop callback and selectChecked state update when calling method handleRowSelect', () => {
const options = { sort: true, selectableRows: 'multiple' };
const rowSelectUpdate = spy();
const wrapper = mount(
{}}
handleHeadUpdateRef={handleHeadUpdateRef}
selectRowUpdate={rowSelectUpdate}
/>
,
);
const instance = wrapper.find('input[type="checkbox"]');
instance.simulate('change', { target: { value: 'checked' } });
assert.strictEqual(rowSelectUpdate.callCount, 1);
});
it('should render a table head with checkbox if selectableRows = multiple', () => {
const options = { selectableRows: 'multiple' };
const mountWrapper = mount(
{}}
handleHeadUpdateRef={handleHeadUpdateRef}
/>
,
);
const actualResult = mountWrapper.find(Checkbox);
assert.strictEqual(actualResult.length, 1);
});
it('should render a table head with no checkbox if selectableRows = single', () => {
const options = { selectableRows: 'single' };
const mountWrapper = mount(
{}}
handleHeadUpdateRef={handleHeadUpdateRef}
/>
,
);
const actualResult = mountWrapper.find(Checkbox);
assert.strictEqual(actualResult.length, 0);
});
it('should render a table head with no checkbox if selectableRows = none', () => {
const options = { selectableRows: 'none' };
const mountWrapper = mount(
{}}
handleHeadUpdateRef={handleHeadUpdateRef}
/>
,
);
const actualResult = mountWrapper.find(Checkbox);
assert.strictEqual(actualResult.length, 0);
});
});