This repository has been archived by the owner on Nov 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.spec.tsx
311 lines (275 loc) · 12.1 KB
/
popup.spec.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
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
import React = require('react');
import ReactDOM = require('react-dom');
import {ClientRenderer, DriverBase, expect, selectDom, waitFor} from 'test-drive-react';
import {PopupDemo} from '../../demo/components/popup-demo';
import {Popup, PopupPositionPoint} from '../../src/components/';
import {PopupTestDriver} from '../../test-kit/components/popup-driver';
import {sleep} from '../utils';
export class PopupDemoTestDriver extends DriverBase {
public static ComponentClass = PopupDemo;
public popup: PopupTestDriver;
public container: HTMLButtonElement = this.select('POPUP_DEMO_BTN');
constructor(getPopupDemo: () => HTMLElement) {
super(getPopupDemo);
this.popup = new PopupTestDriver(getPopupDemo);
}
}
describe('<Popup />', () => {
const clientRenderer = new ClientRenderer();
let anchor: HTMLElement;
before(() => {
anchor = document.body.appendChild(document.createElement('div'));
anchor.style.position = 'absolute';
anchor.style.top = '150px';
anchor.style.left = '150px';
anchor.style.width = '150px';
anchor.style.height = '150px';
anchor.style.border = '1px solid blue';
});
afterEach(() => {clientRenderer.cleanup(); });
after(() => {document.body.removeChild(anchor); });
describe('The popup user', () => {
it('clicks on the parent and the popup opens and closes after another click', async () => {
const {driver: popupDemo, waitForDom} =
clientRenderer.render(<PopupDemo />).withDriver(PopupDemoTestDriver);
await waitForDom(() => {
expect(popupDemo.container).to.be.present();
expect(popupDemo.popup.root).to.be.absent();
});
popupDemo.container.click();
await waitForDom(() => expect(popupDemo.popup.root).to.be.present());
popupDemo.container.click();
return waitForDom(() => expect(popupDemo.popup.root).to.be.absent());
});
});
it('displays the popup and renders its children if the open prop is given', () => {
const {driver: popup} = clientRenderer.render(
<Popup anchor={anchor} open>
<span>Popup Body</span>
</Popup>
).withDriver(PopupTestDriver);
return waitFor(() => {
expect(popup.root).to.be.present();
expect(popup.content[0]).to.be.present();
});
});
it('does not render the popup if there is no anchor', async () => {
const {driver: popup} = clientRenderer.render(
<Popup anchor={null} open>
<span>Popup Body</span>
</Popup>
).withDriver(PopupTestDriver);
await sleep(100);
await waitFor(() => expect(popup.root).to.be.absent());
});
it('does not render the popup if the open prop is false', async () => {
const {driver: popup} = clientRenderer.render(
<Popup anchor={anchor}>
<span data-automation-id="SPAN">Popup Body</span>
</Popup>
).withDriver(PopupTestDriver);
await sleep(100);
await waitFor(() => expect(popup.root).to.be.absent());
});
it('removes the component when unmounting', async () => {
const {driver: popup} = clientRenderer.render(
<Popup
anchor={anchor}
open
>
<span data-automation-id="SPAN">Popup Body</span>
</Popup>).withDriver(PopupTestDriver);
await waitFor(() => {expect(popup.root).to.be.present(); });
ReactDOM.unmountComponentAtNode(popup.root!.parentElement!);
return waitFor(() => {expect(popup.root).to.be.absent(); });
});
it('syncs the popup width', () => {
const {driver: popup} = clientRenderer.render(
<Popup
anchor={anchor}
syncWidth={true}
open
>
<span data-automation-id="SPAN">Popup Body</span>
</Popup>).withDriver(PopupTestDriver);
return waitFor(() => {
expect(popup.root.getBoundingClientRect().width)
.to.equal(anchor.getBoundingClientRect().width);
});
});
it('syncs the popup position when rendered before the anchor and the anchor size changes', async () => {
class Dummy extends React.PureComponent {
public state = {anchor: null};
public render() {
return (
<div>
<Popup anchor={this.state.anchor} open syncWidth>
<div data-automation-id="POPUP">Popup body</div>
</Popup>
<div data-automation-id="ANCHOR" ref={node => this.setState({anchor: node})}>
{this.props.children}
</div>
</div>
);
}
}
const select = selectDom(document.body);
const {container, waitForDom} = clientRenderer.render(<Dummy>*</Dummy>);
await waitForDom(() => expect([select('ANCHOR'), select('POPUP')]).to.be.inVerticalSequence());
clientRenderer.render(<Dummy>*<br />*</Dummy>, container);
await waitForDom(() => expect([select('ANCHOR'), select('POPUP')]).to.be.inVerticalSequence());
});
describe('Layout tests', () => {
const fixture = getFixture();
for (const popupPos of fixture) {
for (const anchorPos of fixture) {
it(`Popup position: V: ${popupPos.vertical} H: ${popupPos.horizontal};
Anchor position: V: ${anchorPos.vertical} H: ${anchorPos.horizontal}`, () => {
const {driver: popup} = clientRenderer.render(
<Popup anchor={anchor} anchorPosition={anchorPos} popupPosition={popupPos} open>
<div style={{background: 'green', color: 'white'}}>
<span data-automation-id="SPAN">
Popup Body
</span>
<div>some more stuff</div>
</div>
</Popup>).withDriver(PopupTestDriver);
return waitFor(() => runTest(popup.root, anchor, popupPos, anchorPos));
});
}
}
});
describe('autoPosition test', () => {
const fixture = getAutoPositionFixture();
for (const item of fixture) {
it(`${item.name} position`, () => {
Object.keys(item.anchorStyle).forEach(key => {
(anchor.style as any)[key] = (item as any).anchorStyle[key];
});
const {driver: popup} = clientRenderer.render(
<Popup anchor={anchor} open autoPosition>
<div style={{background: 'green', color: 'white'}}>
<span data-automation-id="SPAN">
Popup Body
</span>
<div>some more stuff</div>
</div>
</Popup>).withDriver(PopupTestDriver);
return waitFor(() => runTest(popup.root, anchor, item.popupPosition, item.anchorPosition));
});
}
});
});
function getLayoutTest(axis: 'vertical' | 'horizontal') {
let start: 'left' | 'top' = 'top';
let end: 'bottom' | 'right' = 'bottom';
let length: 'height' | 'width' = 'height';
if (axis === 'horizontal') {
start = 'left';
end = 'right';
length = 'width';
}
// Level one: popup position, level two: anchor position
return {
[start]: {
[start]: (anchor: HTMLElement, popup: HTMLElement) =>
createExpect(popup.getBoundingClientRect()[start], anchor.getBoundingClientRect()[start]),
center: (anchor: HTMLElement, popup: HTMLElement) => {
const anchorRect = anchor.getBoundingClientRect();
createExpect(popup.getBoundingClientRect()[start], anchorRect[start] + (anchorRect[length] / 2)); },
[end]: (anchor: HTMLElement, popup: HTMLElement) =>
createExpect(popup.getBoundingClientRect()[start], anchor.getBoundingClientRect()[end])
},
center: {
[start]: (anchor: HTMLElement, popup: HTMLElement) => {
const popupRect = popup.getBoundingClientRect();
createExpect(popupRect[start], anchor.getBoundingClientRect()[start] - (popupRect[length] / 2)); },
center: (anchor: HTMLElement, popup: HTMLElement) => {
const popupRect = popup.getBoundingClientRect();
const anchorRect = anchor.getBoundingClientRect();
createExpect(popupRect[start], anchorRect[start] + (anchorRect[length] / 2) - (popupRect[length] / 2));
},
[end]: (anchor: HTMLElement, popup: HTMLElement) => {
const popupRect = popup.getBoundingClientRect();
createExpect(popupRect[end], anchor.getBoundingClientRect()[end] + (popupRect[length] / 2)); }
},
[end]: {
[start]: (anchor: HTMLElement, popup: HTMLElement) =>
createExpect(popup.getBoundingClientRect()[end], anchor.getBoundingClientRect()[start]),
center: (anchor: HTMLElement, popup: HTMLElement) => {
const anchorRect = anchor.getBoundingClientRect();
createExpect(popup.getBoundingClientRect()[end], anchorRect[start] + (anchorRect[length] / 2)); },
[end]: (anchor: HTMLElement, popup: HTMLElement) =>
createExpect(popup.getBoundingClientRect()[end], anchor.getBoundingClientRect()[end])
}
};
}
function runTest(popup: HTMLElement, anchor: HTMLElement, popupPos: PopupPositionPoint, anchorPos: PopupPositionPoint) {
const topTests = getLayoutTest('vertical');
const leftTests = getLayoutTest('horizontal');
topTests[popupPos.vertical][anchorPos.vertical](anchor, popup);
leftTests[popupPos.horizontal][anchorPos.horizontal](anchor, popup);
}
function getFixture(): PopupPositionPoint[] {
return [{vertical: 'top', horizontal: 'left'},
{vertical: 'top', horizontal: 'center'},
{vertical: 'top', horizontal: 'right'},
{vertical: 'center', horizontal: 'left'},
{vertical: 'center', horizontal: 'center'},
{vertical: 'center', horizontal: 'right'},
{vertical: 'bottom', horizontal: 'left'},
{vertical: 'bottom', horizontal: 'center'},
{vertical: 'bottom', horizontal: 'right'}];
}
function getAutoPositionFixture(): Array<{
anchorPosition: PopupPositionPoint,
popupPosition: PopupPositionPoint,
anchorStyle: Partial<CSSStyleDeclaration>,
name: string
}> {
return [
{
anchorStyle: {
left: '0',
top: '0'
},
anchorPosition: {vertical: 'bottom', horizontal: 'left'},
popupPosition: {vertical: 'top', horizontal: 'left'},
name: 'bottomLeft'
},
{
anchorStyle: {
left: 'auto',
top: '0',
right: '0'
},
anchorPosition: {vertical: 'bottom', horizontal: 'right'},
popupPosition: {vertical: 'top', horizontal: 'right'},
name: 'bottomRight'
},
{
anchorStyle: {
left: '0',
top: 'auto',
bottom: '0'
},
anchorPosition: {vertical: 'top', horizontal: 'left'},
popupPosition: {vertical: 'bottom', horizontal: 'left'},
name: 'topLeft'
},
{
anchorStyle: {
left: 'auto',
top: 'auto',
right: '0',
bottom: '0'
},
anchorPosition: {vertical: 'top', horizontal: 'right'},
popupPosition: {vertical: 'bottom', horizontal: 'right'},
name: 'topRight'
}
];
}
function createExpect(pValue: number, aValue: number) {
expect(pValue, `popup value: ${pValue} anchor value: ${aValue}`).to.be.closeTo(aValue, 0.01);
}