/
BulkActions.test.jsx
290 lines (278 loc) 路 10.2 KB
/
BulkActions.test.jsx
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import React from 'react';
import { mount } from 'enzyme';
import BulkActions from '../BulkActions';
import { DROPDOWN_BUTTON_TEXT, SMALL_SCREEN_DROPDOWN_BUTTON_TEXT } from '../CollapsibleButtonGroup';
import {
useWindowSize, Dropdown, Button, Icon,
} from '../..';
import DataTableContext from '../DataTableContext';
jest.mock('../../hooks/useWindowSize');
useWindowSize.mockReturnValue({ width: 800 });
const firstAction = {
buttonText: 'Primary action',
handleClick: () => {},
className: 'class1',
};
const secondAction = {
buttonText: 'Secondary action',
handleClick: () => {},
className: 'class2',
};
const selectedFlatRows = [{ id: 1 }, { id: 2 }];
const twoActions = [
firstAction,
secondAction,
];
const buttonFunction = (a) => <Button>{a.length}</Button>;
const instance = {
selectedFlatRows,
controlledTableSelections: [
{
selectedRows: [],
isEntireTableSelected: false,
},
jest.fn(),
],
bulkActions: [
...twoActions,
{
buttonText: 'Extra1',
handleClick: () => {},
className: 'extra3',
},
{
buttonText: 'Extra2',
handleClick: () => {},
className: 'extra2',
},
{
buttonText: 'Extra3',
handleClick: () => {},
className: 'disabledTest',
disabled: true,
},
],
};
// eslint-disable-next-line react/prop-types
const BulkActionsWrapper = ({ value = instance }) => (
<DataTableContext.Provider value={value}><BulkActions /></DataTableContext.Provider>);
describe('<BulkActions />', () => {
describe('with one action', () => {
it('displays the primary button as an outline button', () => {
const wrapper = mount(<BulkActionsWrapper value={{ ...instance, bulkActions: [firstAction] }} />);
const button = wrapper.find(Button);
expect(button.length).toEqual(1);
expect(button.props().variant).toEqual('outline-primary');
});
it('displays the primary action with the user\'s variant', () => {
const variant = 'my-variant';
const wrapper = mount(
<BulkActionsWrapper value={{ ...instance, bulkActions: [{ ...firstAction, variant }] }} />,
);
const button = wrapper.find(Button);
expect(button.props().variant).toEqual(variant);
});
it('passes button classnames', () => {
const wrapper = mount(<BulkActionsWrapper value={{ ...instance, bulkActions: [firstAction] }} />);
const button = wrapper.find(Button);
expect(button.props().className).toContain(firstAction.className);
});
it('disables the button', () => {
const wrapper = mount(
<BulkActionsWrapper value={{ ...instance, bulkActions: [{ ...firstAction, disabled: true }] }} />,
);
const button = wrapper.find(Button);
expect(button.props().disabled).toEqual(true);
});
it('performs the button action on click', () => {
const onClickSpy = jest.fn();
const wrapper = mount(
<BulkActionsWrapper
value={{ ...instance, bulkActions: [{ ...firstAction, handleClick: onClickSpy }] }}
/>,
);
const button = wrapper.find('button');
expect(button.length).toEqual(1);
button.simulate('click');
expect(onClickSpy).toHaveBeenCalledTimes(1);
expect(onClickSpy).toHaveBeenCalledWith({
selectedRows: selectedFlatRows,
});
});
});
describe('with two actions', () => {
const wrapper = mount(<BulkActionsWrapper value={{ ...instance, bulkActions: twoActions }} />);
it('displays the user\'s first button as an brand button', () => {
const buttons = wrapper.find(Button);
expect(buttons.length).toEqual(2);
expect(buttons.get(1).props.variant).toEqual('brand');
});
it('displays the user\'s second button as an outline button', () => {
const buttons = wrapper.find(Button);
expect(buttons.get(0).props.variant).toEqual('outline-primary');
});
it('reverses the button order so that the primary button is on the right', () => {
const buttons = wrapper.find(Button);
expect(buttons.at(0).text()).toEqual(twoActions[1].buttonText);
expect(buttons.at(1).text()).toEqual(twoActions[0].buttonText);
});
it('passes button classnames', () => {
const buttons = wrapper.find(Button);
expect(buttons.at(0).props().className).toContain(twoActions[1].className);
expect(buttons.at(1).props().className).toContain(twoActions[0].className);
});
});
describe('controlled table selections', () => {
it('handles action on click with full table selection (all rows across all pages)', () => {
const onClickSpy = jest.fn();
const wrapper = mount(
<BulkActionsWrapper
value={{
...instance,
bulkActions: [{ ...firstAction, handleClick: onClickSpy }, secondAction],
controlledTableSelections: [
{
selectedRows: [],
isEntireTableSelected: true,
},
jest.fn(),
],
}}
/>,
);
const button = wrapper.find(Button).at(1);
button.simulate('click');
expect(onClickSpy).toHaveBeenCalledTimes(1);
expect(onClickSpy).toHaveBeenCalledWith({
isEntireTableSelected: true,
selectedRows: selectedFlatRows,
});
});
});
describe('two actions on click', () => {
it('performs the primary button action on click', () => {
const onClickSpy = jest.fn();
const wrapper = mount(
<BulkActionsWrapper
value={{ ...instance, bulkActions: [{ ...firstAction, handleClick: onClickSpy }, secondAction] }}
/>,
);
const button = wrapper.find(Button).at(1);
button.simulate('click');
expect(onClickSpy).toHaveBeenCalledTimes(1);
expect(onClickSpy).toHaveBeenCalledWith({
selectedRows: selectedFlatRows,
});
});
it('performs the second button action on click', () => {
const onClickSpy = jest.fn();
const wrapper = mount(
<BulkActionsWrapper
value={{ ...instance, bulkActions: [firstAction, { ...secondAction, handleClick: onClickSpy }] }}
/>,
);
const button = wrapper.find(Button).at(0);
button.simulate('click');
expect(onClickSpy).toHaveBeenCalledTimes(1);
expect(onClickSpy).toHaveBeenCalledWith({
selectedRows: selectedFlatRows,
});
});
});
describe('with more than two actions', () => {
it('displays the user\'s first button as an brand button', () => {
const wrapper = mount(<BulkActionsWrapper />);
const buttons = wrapper.find(Button);
expect(buttons.length).toEqual(3);
expect(buttons.get(2).props.variant).toEqual('brand');
});
it('displays the user\'s second button as an outline button', () => {
const wrapper = mount(<BulkActionsWrapper />);
const buttons = wrapper.find(Button);
expect(buttons.get(1).props.variant).toEqual('outline-primary');
});
describe('dropdown', () => {
const onClickSpy = jest.fn();
const itemClassName = 'itemClickTest';
const itemText = 'Yet another action';
let wrapper;
let dropdownButton;
beforeEach(() => {
wrapper = mount(
<BulkActionsWrapper
value={{
...instance,
// eslint-disable-next-line max-len
bulkActions: [...instance.bulkActions, { buttonText: itemText, handleClick: onClickSpy, className: itemClassName }],
}}
/>,
);
// the dropdown menu is the first button
dropdownButton = wrapper.find('button').at(0);
dropdownButton.simulate('click');
});
afterEach(() => {
onClickSpy.mockClear();
});
it('displays additional actions in a dropdown', () => {
const icon = wrapper.find(Icon);
expect(icon.props().screenReaderText).toEqual(DROPDOWN_BUTTON_TEXT);
const actionItems = wrapper.find(Dropdown.Item);
// we subtract two for the two main buttons that aren't in the dropdown
expect(actionItems.length).toEqual(4);
});
it('performs actions when dropdown items are clicked', () => {
wrapper.find(`a.${itemClassName}`).simulate('click');
expect(onClickSpy).toHaveBeenCalledTimes(1);
expect(onClickSpy).toHaveBeenCalledWith({
selectedRows: selectedFlatRows,
});
});
it('displays the action text', () => {
const item = wrapper.find(`a.${itemClassName}`);
expect(item.text()).toEqual(itemText);
});
it('passes the class names to the dropdown item', () => {
const item = wrapper.find(`a.${itemClassName}`);
expect(item.length).toEqual(1);
});
it('disables actions', () => {
// This should be the Dropdown.Item
const item = wrapper.find('.disabledTest').first();
expect(item.props().disabled).toEqual(true);
});
});
});
describe('small screen', () => {
const actions = [[[firstAction]], [[firstAction, secondAction]], [instance.bulkActions]];
test.each(actions)('puts all actions in a dropdown %#', (testActions) => {
useWindowSize.mockReturnValue({ width: 500 });
const wrapper = mount(<BulkActionsWrapper value={{ ...instance, bulkActions: testActions }} />);
const button = wrapper.find(Icon);
expect(button.length).toEqual(1);
expect(wrapper.text()).not.toContain(firstAction.buttonText);
const buttons = wrapper.find('button');
expect(buttons.length).toEqual(1);
const dropdownButton = buttons.at(0);
dropdownButton.simulate('click');
wrapper.update();
testActions.forEach((action) => {
expect(wrapper.text()).toContain(action.buttonText);
});
});
it('renders the correct alt text for the dropdown', () => {
useWindowSize.mockReturnValue({ width: 500 });
const wrapper = mount(<BulkActionsWrapper />);
const icon = wrapper.find(Icon);
expect(icon.props().screenReaderText).toEqual(SMALL_SCREEN_DROPDOWN_BUTTON_TEXT);
});
});
describe('with function', () => {
it('passed correct number of selected rows', () => {
const wrapper = mount(<BulkActionsWrapper value={{ ...instance, bulkActions: buttonFunction }} />);
const button = wrapper.find(Button);
expect(button.length).toEqual(1);
expect(button.text()).toEqual('2');
});
});
});