-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathPopover.test.js
319 lines (269 loc) · 12.8 KB
/
Popover.test.js
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* eslint-disable compat/compat */
import Icon from '../Icon/Icon';
import Menu from '../Menu/Menu';
import { mount } from 'enzyme';
import Popover from './Popover';
import Popper from '../utils/_Popper';
import React from 'react';
import { Popper as ReactPopper } from 'react-popper';
describe('<Popover />', () => {
const popOver = (
<Popover
body={
<Menu>
<Menu.List>
<Menu.Item url='/'>Option 1</Menu.Item>
<Menu.Item url='/'>Option 2</Menu.Item>
<Menu.Item url='/'>Option 3</Menu.Item>
<Menu.Item url='/'>Option 4</Menu.Item>
</Menu.List>
</Menu>
}
control={<Icon glyph='cart' size='xl' />} popperProps={{ id: 'fd-default-popover' }} />
);
const popOverDisabled = (
<Popover
body={
<Menu>
<Menu.List>
<Menu.Item url='/'>Option 1</Menu.Item>
<Menu.Item url='/'>Option 2</Menu.Item>
<Menu.Item url='/'>Option 3</Menu.Item>
<Menu.Item url='/'>Option 4</Menu.Item>
</Menu.List>
</Menu>
}
className='blue'
control={<Icon glyph='cart' size='xl' />}
disabled popperProps={{ id: 'fd-disabled-popover' }} />
);
const popOverDisableEdgeDetection = (
<Popover
body={
<Menu>
<Menu.List>
<Menu.Item url='/'>Option 1</Menu.Item>
<Menu.Item url='/'>Option 2</Menu.Item>
<Menu.Item url='/'>Option 3</Menu.Item>
<Menu.Item url='/'>Option 4</Menu.Item>
</Menu.List>
</Menu>
}
control={<Icon glyph='cart' size='xl' />}
disableEdgeDetection popperProps={{ id: 'fd-edge-undetected-popover' }} />
);
test('popper is receiving disableEdgeDetection from popover', () => {
// disableEdgeDetection is defaulted to false
const popoverWithDetection = mount(popOver);
expect(popoverWithDetection.props().disableEdgeDetection).toBeFalsy();
// prop is correctly changed when set to true
const popoverWithoutDetection = mount(popOverDisableEdgeDetection);
expect(popoverWithoutDetection.props().disableEdgeDetection).toBeTruthy();
});
test('handle document click to close popover', () => {
const wrapper = mount(popOver).children();
// click on popover to show
wrapper.find('div.fd-popover__control .sap-icon--cart').simulate('click');
expect(wrapper.state('isExpanded')).toBeTruthy();
// click on popover to hide
wrapper.find('div.fd-popover__control .sap-icon--cart').simulate('click');
expect(wrapper.state('isExpanded')).toBeFalsy();
// wrapper.instance().componentWillUnmount();
});
test('handle esc key to close popover', () => {
const wrapper = mount(popOver).children();
// click on popover to show
wrapper.find('div.fd-popover__control .sap-icon--cart').simulate('click');
expect(wrapper.state('isExpanded')).toBeTruthy();
// handle esc key
let event = new KeyboardEvent('keydown', { keyCode: 27 });
document.dispatchEvent(event);
expect(wrapper.state('isExpanded')).toBeFalsy();
});
test('handle document click to close popover', () => {
const wrapper = mount(popOver).children();
// click on popover to show
wrapper.find('div.fd-popover__control .sap-icon--cart').simulate('click');
expect(wrapper.state('isExpanded')).toBeTruthy();
// handle click on document
let event = new MouseEvent('mousedown', {});
document.dispatchEvent(event);
expect(wrapper.state('isExpanded')).toBeFalsy();
});
test('handle document click to close popover', () => {
const wrapper = mount(popOverDisabled).children();
// click on popover to show
wrapper.find('div.fd-popover__control .sap-icon--cart').simulate('click');
expect(wrapper.state('isExpanded')).toBeFalsy();
});
describe('Prop spreading', () => {
test('should allow props to be spread to the Popover component', () => {
const element = mount(
<Popover
body={<div />}
control={<Icon glyph='cart' size='xl' />}
data-sample='Sample' />
);
expect(
element.find('.fd-popover').getDOMNode().attributes['data-sample'].value
).toBe('Sample');
});
});
describe('control accessibility', () => {
test('adds a tabindex of 0 to the control', () => {
const wrapper = mount(popOver);
const button = wrapper.find('Icon').at(0);
expect(button.props().tabIndex).toEqual(0);
});
test('adds appropriate aria-haspopup to the control', () => {
//check unset type
let wrapper = mount(popOver);
let button = wrapper.find('Icon').at(0);
expect(button.props()['aria-haspopup']).toEqual(true);
//check valid string type
const dialogPopover = React.cloneElement(popOver, { type: 'dialog' });
wrapper = mount(dialogPopover);
button = wrapper.find('Icon').at(0);
expect(button.props()['aria-haspopup']).toEqual('dialog');
});
test('adds appropriate aria-expanded to the control, and updates it on state change', () => {
const wrapper = mount(popOver).children();
let button = wrapper.find('Icon').at(0);
expect(button.props()['aria-expanded']).toEqual(false);
wrapper.setState({ isExpanded: true }, () => {
button = wrapper.find('Icon').at(0);
expect(button.getDOMNode().getAttribute('aria-expanded')).toEqual('true');
});
wrapper.setState({ isExpanded: false }, () => {
button = wrapper.find('Icon').at(0);
expect(button.getDOMNode().getAttribute('aria-expanded')).toEqual('false');
});
});
test('adds appropriate aria-controls to the control', () => {
let wrapper = mount(popOver).children();
let button = wrapper.find('Icon').at(0);
expect(button.props()['aria-controls']).toEqual('fd-default-popover');
//check undefined popperProps
// eslint-disable-next-line no-undefined
const propLessPopover = React.cloneElement(popOver, { popperProps: undefined } );
wrapper = mount(propLessPopover).children();
wrapper.setState({ isExpanded: true }, () => {
const popoverContentId = document.querySelector('div.fd-popover__body').id;
button = wrapper.find('Icon').at(0);
expect(button.props()['aria-controls']).toEqual(popoverContentId);
});
});
test('adds a role of button to the control', () => {
const wrapper = mount(popOver);
const button = wrapper.find('Icon').at(0);
expect(button.props().role).toEqual('button');
});
test('handle space key to open popover', () => {
const syntheticEvent = {
keyCode: 32,
preventDefault: () => {}
};
const wrapper = mount(popOver).children();
const button = wrapper.find('Icon').at(0);
button.prop('onKeyPress')(syntheticEvent, 'Icon', wrapper.triggerBody);
expect(wrapper.state('isExpanded')).toBeTruthy();
});
test('handle enter key to open popover', () => {
const syntheticEvent = {
keyCode: 13,
preventDefault: () => {}
};
const wrapper = mount(popOver).children();
const button = wrapper.find('Icon').at(0);
button.prop('onKeyPress')(syntheticEvent, 'Icon', wrapper.triggerBody);
expect(wrapper.state('isExpanded')).toBeTruthy();
});
});
describe('widthSizingType', () => {
test('matchTarget', () => {
const popoverWithParent = mount(popOver).setProps({ widthSizingType: 'matchTarget' });
const popperComponent = popoverWithParent.find(ReactPopper);
const modifier = popperComponent.props().modifiers.find(m => m.name === 'matchTargetModifier');
expect(modifier).toBeTruthy();
});
test('minTarget', () => {
const popoverWithParent = mount(popOver).setProps({ widthSizingType: 'minTarget' });
const popperComponent = popoverWithParent.find(ReactPopper);
const modifier = popperComponent.props().modifiers.find(m => m.name === 'minTargetModifier');
expect(modifier).toBeTruthy();
});
test('maxTarget', () => {
const popoverWithParent = mount(popOver).setProps({ widthSizingType: 'maxTarget' });
const popperComponent = popoverWithParent.find(ReactPopper);
const modifier = popperComponent.props().modifiers.find(m => m.name === 'maxTargetModifier');
expect(modifier).toBeTruthy();
});
});
describe('flip modifiers', () => {
test('disableEdgeDetection props', () => {
const popoverWithParent = mount(popOver).setProps({ disableEdgeDetection: true });
const popperComponent = popoverWithParent.find(ReactPopper);
const flipModifier = popperComponent.props().modifiers.find(m => m.name === 'flip');
expect(flipModifier.enabled).toBe(false);
});
test('disableEdgeDetection default', () => {
const popoverWithParent = mount(popOver).setProps({ });
const popperComponent = popoverWithParent.find(ReactPopper);
const flipModifier = popperComponent.props().modifiers.find(m => m.name === 'flip');
expect(flipModifier.enabled).not.toBeDefined();
});
test('fallback placements', () => {
const popoverWithParent = mount(popOver).setProps({ placement: ['right', 'top'] });
const popperComponent = popoverWithParent.find(ReactPopper);
const flipModifier = popperComponent.props().modifiers.find(m => m.name === 'flip');
expect(flipModifier.options.fallbackPlacements).toEqual(['top']);
});
test('fallback placements default', () => {
const popoverWithParent = mount(popOver).setProps({ });
const popperComponent = popoverWithParent.find(ReactPopper);
const flipModifier = popperComponent.props().modifiers.find(m => m.name === 'flip');
expect(flipModifier.options.fallbackPlacements).toEqual(Popper.defaultProps.popperPlacement.slice(1));
});
test('flip container props', () => {
const boundary = document.createElement('div');
const popoverWithParent = mount(popOver).setProps({ flipContainer: boundary });
const popperComponent = popoverWithParent.find(ReactPopper);
const flipModifier = popperComponent.props().modifiers.find(m => m.name === 'flip');
expect(flipModifier.options.boundary).toBe(boundary);
});
test('flip container default', () => {
const popoverWithParent = mount(popOver).setProps({ });
const popperComponent = popoverWithParent.find(ReactPopper);
const flipModifier = popperComponent.props().modifiers.find(m => m.name === 'flip');
expect(flipModifier.options.boundary).not.toBeDefined();
});
});
describe('Callback handler', () => {
test('should dispatch the onClickOutside callback with the event', () => {
let f = jest.fn();
const element = mount(
<Popover
body={<div />}
control={<button id='test' />}
onClickOutside={f} />
);
element.find('#test').simulate('click');
let event = new MouseEvent('mousedown', {});
document.dispatchEvent(event);
expect(f).toHaveBeenCalledTimes(1);
});
test('should dispatch the onEscapeKey callback with the event', () => {
let f = jest.fn();
const element = mount(
<Popover
body={<div />}
control={<button id='test' />}
onEscapeKey={f} />
);
element.find('#test').simulate('click');
let event = new KeyboardEvent('keydown', { keyCode: 27 });
document.dispatchEvent(event);
expect(f).toHaveBeenCalledTimes(1);
});
});
});