File size: 5,803 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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('<TableHead />', 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 => <TableHeadCell {...columnMeta}>{columnMeta.name + 's'}</TableHeadCell>,
        sort: null,
      },
    ];

    handleHeadUpdateRef = () => {};
  });

  it('should render a table head', () => {
    const options = {};
    const toggleSort = () => {};
    const mountWrapper = mount(
      <DndProvider backend={HTML5Backend}>
        <TableHead
          columns={columns}
          options={options}
          setCellRef={() => {}}
          handleHeadUpdateRef={handleHeadUpdateRef}
          toggleSort={toggleSort}
        />
      </DndProvider>,
    );
    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(
      <DndProvider backend={HTML5Backend}>
        <TableHead
          columns={columns}
          options={options}
          setCellRef={() => {}}
          handleHeadUpdateRef={handleHeadUpdateRef}
          toggleSort={toggleSort}
        />
      </DndProvider>,
    );
    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(
      <DndProvider backend={HTML5Backend}>
        <TableHead
          columns={newColumns}
          options={options}
          setCellRef={() => {}}
          handleHeadUpdateRef={handleHeadUpdateRef}
          toggleSort={toggleSort}
        />
      </DndProvider>,
    );
    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(
      <DndProvider backend={HTML5Backend}>
        <TableHead
          columns={columns}
          options={options}
          setCellRef={() => {}}
          handleHeadUpdateRef={handleHeadUpdateRef}
          toggleSort={toggleSort}
        />
      </DndProvider>,
    );

    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(
      <DndProvider backend={HTML5Backend}>
        <TableHead
          columns={columns}
          options={options}
          setCellRef={() => {}}
          handleHeadUpdateRef={handleHeadUpdateRef}
          selectRowUpdate={rowSelectUpdate}
        />
      </DndProvider>,
    );

    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(
      <DndProvider backend={HTML5Backend}>
        <TableHead
          columns={columns}
          options={options}
          setCellRef={() => {}}
          handleHeadUpdateRef={handleHeadUpdateRef}
        />
      </DndProvider>,
    );

    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(
      <DndProvider backend={HTML5Backend}>
        <TableHead
          columns={columns}
          options={options}
          setCellRef={() => {}}
          handleHeadUpdateRef={handleHeadUpdateRef}
        />
      </DndProvider>,
    );

    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(
      <DndProvider backend={HTML5Backend}>
        <TableHead
          columns={columns}
          options={options}
          setCellRef={() => {}}
          handleHeadUpdateRef={handleHeadUpdateRef}
        />
      </DndProvider>,
    );

    const actualResult = mountWrapper.find(Checkbox);
    assert.strictEqual(actualResult.length, 0);
  });
});