-
Notifications
You must be signed in to change notification settings - Fork 182
/
dropdown.test.tsx
192 lines (156 loc) · 4.95 KB
/
dropdown.test.tsx
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
import * as Sinon from 'sinon';
import React from 'react';
import {
Simulate
} from 'react-dom/test-utils';
import {shallow, mount, ReactWrapper} from 'enzyme';
import Popup, {PopupProps} from '../popup/popup';
import Anchor from './anchor';
import Dropdown, {DropdownAttrs} from './dropdown';
describe('Dropdown', () => {
let anchor: HTMLElement | null;
let popup: Popup | null;
const anchorElement = (
<span
ref={function anchorRef(el) {
anchor = el;
}}
/>
);
const popupElement = (
<Popup<PopupProps>
ref={function popupRef(el) {
popup = el;
}}
>{'foo'}</Popup>
);
const shallowDropdown = (props?: Partial<DropdownAttrs>, children = popupElement) => shallow(
<Dropdown
anchor={anchorElement}
{...props}
>
{children}
</Dropdown>
);
const mountDropdown = (props?: Partial<DropdownAttrs>, children = popupElement) => mount(
<Dropdown
anchor={anchorElement}
{...props}
>
{children}
</Dropdown>
);
it('should create component', () => {
mountDropdown().should.have.type(Dropdown);
});
it('should wrap children with div', () => {
shallowDropdown().should.have.tagName('div');
});
it('should use passed className', () => {
shallowDropdown({className: 'test-class'}).should.have.className('test-class');
});
it('should not show popup by default', () => {
mountDropdown();
should.exist(popup);
popup?.isVisible().should.be.false;
});
it('should show popup on anchor click', () => {
mountDropdown();
should.exist(anchor);
anchor && Simulate.click(anchor);
should.exist(popup);
popup?.isVisible().should.be.true;
});
it('should hide popup on second anchor click', () => {
mountDropdown();
should.exist(anchor);
anchor && Simulate.click(anchor);
anchor && Simulate.click(anchor);
should.exist(popup);
popup?.isVisible().should.be.false;
});
it('should hide popup on outside pointer down event', async () => {
mountDropdown();
should.exist(anchor);
anchor && Simulate.click(anchor);
await new Promise<void>(resolve => {
setTimeout(() => {
document.dispatchEvent(new PointerEvent('pointerdown'));
should.exist(popup);
popup?.isVisible().should.be.false;
resolve();
}, 0);
});
});
it('should show popup when initialized with initShown=true', () => {
mountDropdown({initShown: true});
should.exist(popup);
popup?.isVisible().should.be.true;
});
it('should accept function as anchor', () => {
const anchorFunc = sandbox.stub().returns(anchorElement);
mountDropdown({anchor: anchorFunc});
anchorFunc.should.have.been.calledWithMatch({active: false});
});
it('should pass active property to anchor function', () => {
const anchorFunc = sandbox.stub().returns(anchorElement);
mountDropdown({anchor: anchorFunc});
should.exist(anchor);
anchor && Simulate.click(anchor);
anchorFunc.should.have.been.calledTwice;
anchorFunc.getCall(1).calledWithMatch({active: true}).should.be.true;
});
it('should render <Anchor> with child if type of anchor property is string', () => {
const anchorText = 'Test anchor text';
shallowDropdown({anchor: anchorText}).find(Anchor).children().should.have.text(anchorText);
});
describe('hoverMode', () => {
let wrapper: ReactWrapper;
let popupEl: Popup | null;
let clock: Sinon.SinonFakeTimers;
const dropDownProps = {
hoverMode: true,
hoverShowTimeOut: 100,
hoverHideTimeOut: 300
};
beforeEach(() => {
clock = sandbox.useFakeTimers({toFake: ['setTimeout']});
const popupComponent = (
<Popup<PopupProps>
ref={function popupRef(el) {
popupEl = el;
}}
>{'foo'}</Popup>
);
wrapper = mountDropdown(dropDownProps, popupComponent);
});
it('should show popup on mouse enter', () => {
wrapper.simulate('mouseenter');
clock.tick(dropDownProps.hoverShowTimeOut);
should.exist(popupEl);
popupEl?.isVisible().should.be.true;
});
it('should hide popup on mouse leave', () => {
wrapper.simulate('mouseenter');
clock.tick(dropDownProps.hoverShowTimeOut);
wrapper.simulate('mouseleave');
clock.tick(dropDownProps.hoverHideTimeOut);
should.exist(popupEl);
popupEl?.isVisible().should.be.false;
});
it('should no hide popup on mouse leave if wrapper was clicked', () => {
wrapper.simulate('click');
wrapper.simulate('mouseleave');
clock.tick(dropDownProps.hoverHideTimeOut);
should.exist(popupEl);
popupEl?.isVisible().should.be.true;
});
it('should no hide popup on click if popup is already opened by hover popup', () => {
wrapper.simulate('mouseenter');
clock.tick(dropDownProps.hoverShowTimeOut);
wrapper.simulate('click');
should.exist(popupEl);
popupEl?.isVisible().should.be.true;
});
});
});