File size: 2,829 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
import IconButton from '@mui/material/IconButton';
import DownloadIcon from '@mui/icons-material/CloudDownload';
import FilterIcon from '@mui/icons-material/FilterList';
import PrintIcon from '@mui/icons-material/Print';
import SearchIcon from '@mui/icons-material/Search';
import ViewColumnIcon from '@mui/icons-material/ViewColumn';
import Chip from '@mui/material/Chip';
import { assert } from 'chai';
import { mount } from 'enzyme';
import React from 'react';
import TableToolbar from '../src/components/TableToolbar';
import getTextLabels from '../src/textLabels';

const CustomChip = props => {
  return <Chip variant="outlined" color="secondary" label={props.label} />;
};

const icons = {
  SearchIcon,
  DownloadIcon,
  PrintIcon,
  ViewColumnIcon,
  FilterIcon,
};
let setTableAction = () => {};
const options = {
  print: true,
  download: true,
  search: true,
  filter: true,
  viewColumns: true,
  textLabels: getTextLabels(),
  downloadOptions: {
    separator: ',',
    filename: 'tableDownload.csv',
    filterOptions: {
      useDisplayedRowsOnly: true,
      useDisplayedColumnsOnly: true,
    },
  },
};
const columns = ['First Name', 'Company', 'City', 'State'];
const data = [
  {
    data: ['Joe James', 'Test Corp', 'Yonkers', 'NY'],
    dataIndex: 0,
  },
  {
    data: ['John Walsh', 'Test Corp', 'Hartford', 'CT'],
    dataIndex: 1,
  },
  {
    data: ['Bob Herm', 'Test Corp', 'Tampa', 'FL'],
    dataIndex: 2,
  },
  {
    data: ['James Houston', 'Test Corp', 'Dallas', 'TX'],
    dataIndex: 3,
  },
];

const testCustomIcon = iconName => {
  const components = { icons: { [iconName]: CustomChip } };
  const wrapper = mount(<TableToolbar {...{ columns, data, options, setTableAction, components }} />);
  assert.strictEqual(wrapper.find(IconButton).length, 5); // All icons show
  assert.strictEqual(wrapper.find(CustomChip).length, 1); // Custom chip shows once
  Object.keys(icons).forEach(icon => {
    // The original default for the custom icon should be gone, the rest should remain
    assert.strictEqual(wrapper.find(icons[icon]).length, iconName === icon ? 0 : 1);
  });
};

describe('<TableToolbar /> with custom icons', function() {
  it('should render a toolbar with a custom chip in place of the search icon', () => {
    testCustomIcon('SearchIcon');
  });

  it('should render a toolbar with a custom chip in place of the download icon', () => {
    testCustomIcon('DownloadIcon');
  });

  it('should render a toolbar with a custom chip in place of the print icon', () => {
    testCustomIcon('PrintIcon');
  });

  it('should render a toolbar with a custom chip in place of the view columns icon', () => {
    testCustomIcon('ViewColumnIcon');
  });

  it('should render a toolbar with a custom chip in place of the filter icon', () => {
    testCustomIcon('FilterIcon');
  });
});