-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathalign.test.tsx
More file actions
406 lines (344 loc) · 9.63 KB
/
align.test.tsx
File metadata and controls
406 lines (344 loc) · 9.63 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { act, cleanup, fireEvent, render } from '@testing-library/react';
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
import React from 'react';
import type { TriggerProps, TriggerRef } from '../src';
import Trigger from '../src';
import { awaitFakeTimer } from './util';
import { _rs } from '@rc-component/resize-observer';
export const triggerResize = (target: Element) => {
act(() => {
_rs([{ target } as ResizeObserverEntry]);
});
};
describe('Trigger.Align', () => {
let targetVisible = true;
let rectX = 100;
let rectY = 100;
let rectWidth = 100;
let rectHeight = 100;
beforeAll(() => {
spyElementPrototypes(HTMLDivElement, {
getBoundingClientRect: () => ({
x: rectX,
y: rectY,
left: rectX,
top: rectY,
width: rectWidth,
height: rectHeight,
right: 200,
bottom: 200,
}),
});
spyElementPrototypes(HTMLElement, {
offsetParent: {
get: () => (targetVisible ? document.body : null),
},
});
spyElementPrototypes(SVGElement, {
offsetParent: {
get: () => (targetVisible ? document.body : null),
},
});
});
beforeEach(() => {
targetVisible = true;
rectX = 100;
rectY = 100;
rectWidth = 100;
rectHeight = 100;
jest.useFakeTimers();
});
afterEach(() => {
cleanup();
jest.useRealTimers();
});
it('not show', async () => {
const onAlign = jest.fn();
const Demo = (props: Partial<TriggerProps>) => {
const scrollRef = React.useRef<HTMLDivElement>(null);
return (
<>
<div
className="scroll"
ref={scrollRef}
// Jest can not get calculated style in jsdom. So we need to set it manually
style={{ overflowX: 'hidden', overflowY: 'hidden' }}
/>
<Trigger
onPopupAlign={onAlign}
popupAlign={{
points: ['bl', 'tl'],
}}
popup={<strong>trigger</strong>}
getPopupContainer={() => scrollRef.current!}
{...props}
>
<div />
</Trigger>
</>
);
};
const { rerender, container } = render(<Demo />);
const scrollDiv = container.querySelector('.scroll')!;
const mockAddEvent = jest.spyOn(scrollDiv, 'addEventListener');
expect(mockAddEvent).not.toHaveBeenCalled();
// Visible
rerender(<Demo popupVisible />);
expect(mockAddEvent).toHaveBeenCalled();
// Scroll
onAlign.mockReset();
fireEvent.scroll(scrollDiv);
await awaitFakeTimer();
expect(onAlign).toHaveBeenCalled();
});
it('resize align', async () => {
const onAlign = jest.fn();
const { container } = render(
<Trigger
onPopupAlign={onAlign}
popupVisible
popupAlign={{
points: ['bl', 'tl'],
}}
popup={<strong>trigger</strong>}
>
<span className="target" />
</Trigger>,
);
await Promise.resolve();
onAlign.mockReset();
// Resize
const target = container.querySelector('.target')!;
triggerResize(target);
await awaitFakeTimer();
expect(onAlign).toHaveBeenCalled();
});
it('placement is higher than popupAlign', async () => {
render(
<Trigger
popupVisible
popup={<span className="bamboo" />}
builtinPlacements={{
top: {},
}}
popupPlacement="top"
popupAlign={{}}
>
<span />
</Trigger>,
);
await awaitFakeTimer();
expect(
document.querySelector('.rc-trigger-popup-placement-top'),
).toBeTruthy();
});
it('invisible should not align', async () => {
const onPopupAlign = jest.fn();
const triggerRef = React.createRef<TriggerRef>();
render(
<Trigger
popupVisible
popup={<span className="bamboo" />}
popupAlign={{}}
onPopupAlign={onPopupAlign}
ref={triggerRef}
>
<span />
</Trigger>,
);
await awaitFakeTimer();
expect(onPopupAlign).toHaveBeenCalled();
onPopupAlign.mockReset();
for (let i = 0; i < 10; i += 1) {
triggerRef.current!.forceAlign();
await awaitFakeTimer();
expect(onPopupAlign).toHaveBeenCalled();
onPopupAlign.mockReset();
}
// Make invisible
targetVisible = false;
triggerRef.current!.forceAlign();
await awaitFakeTimer();
expect(onPopupAlign).not.toHaveBeenCalled();
});
it('align should merge into placement', async () => {
render(
<Trigger
popupVisible
popup={<span className="bamboo" />}
builtinPlacements={{
top: {
targetOffset: [0, 0],
},
}}
popupPlacement="top"
popupAlign={{
targetOffset: [-903, -1128],
}}
>
<svg />
</Trigger>,
);
await awaitFakeTimer();
expect(
document.querySelector('.rc-trigger-popup-placement-top'),
).toHaveStyle({
left: `753px`,
top: `978px`,
});
});
it('targetOffset support ptg', async () => {
render(
<Trigger
popupVisible
popup={<span className="bamboo" />}
popupAlign={{
targetOffset: ['50%', '-50%'],
}}
>
<div />
</Trigger>,
);
await awaitFakeTimer();
// Correct this if I miss understand the value calculation
// from https://github.com/yiminghe/dom-align/blob/master/src/getElFuturePos.js
expect(document.querySelector('.rc-trigger-popup')).toHaveStyle({
left: `-50px`,
top: `50px`,
});
});
it('support dynamicInset', async () => {
render(
<Trigger
popupVisible
popup={<span className="bamboo" />}
popupAlign={{
points: ['bc', 'tc'],
_experimental: {
dynamicInset: true,
},
}}
>
<div />
</Trigger>,
);
await awaitFakeTimer();
expect(document.querySelector('.rc-trigger-popup')).toHaveStyle({
bottom: `100px`,
});
});
it('floor when decimal precision', async () => {
rectX = 22.6;
rectY = 33.4;
rectWidth = 33.7;
rectHeight = 55.9;
render(
<Trigger
popupVisible
popup={<span className="bamboo" />}
popupAlign={{
points: ['tl', 'bl'],
}}
>
<div />
</Trigger>,
);
await awaitFakeTimer();
expect(document.querySelector('.rc-trigger-popup')).toHaveStyle({
top: `55px`,
});
});
it('both adjustX and adjustY should get correct points', async () => {
// Set target position to top left corner to force flip to bottom right
rectX = 0;
rectY = 0;
rectWidth = 100;
rectHeight = 100;
const onPopupAlign = jest.fn();
render(
<Trigger
popupVisible
popup={<span className="bamboo" />}
popupAlign={{
points: ['tl', 'bl'],
overflow: {
adjustX: true,
adjustY: true,
},
}}
onPopupAlign={onPopupAlign}
>
<div />
</Trigger>,
);
await awaitFakeTimer();
// Check that the points have been flipped correctly
expect(onPopupAlign).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
points: ['br', 'tr'],
}),
);
});
// https://github.com/react-component/trigger/issues/XXX
it('should not modify popup styles during alignment measurement', async () => {
// On some platforms (notably Linux), the alignment calculation runs
// mid-CSS-animation. The fix uses a temporary measurement element
// instead of modifying the popup's styles, so CSS animations
// (transform, transition) are never disrupted.
render(
<Trigger
popupVisible
popup={<span className="popup-content" />}
popupAlign={{
points: ['tl', 'bl'],
}}
>
<div className="trigger-target" />
</Trigger>,
);
await awaitFakeTimer();
const popupElement = document.querySelector(
'.rc-trigger-popup',
) as HTMLElement;
expect(popupElement).toBeTruthy();
// Spy on popup style mutations during alignment using property setter
// spies (catches both direct assignment and setProperty)
const styleChanges: string[] = [];
const propsToWatch = ['left', 'top', 'transform', 'transition', 'overflow'];
const restoreSpies: (() => void)[] = [];
propsToWatch.forEach((prop) => {
const descriptor = Object.getOwnPropertyDescriptor(
CSSStyleDeclaration.prototype,
prop,
);
if (descriptor?.set) {
const origSet = descriptor.set;
Object.defineProperty(popupElement.style, prop, {
set(value: string) {
styleChanges.push(prop);
origSet.call(this, value);
},
get: descriptor.get,
configurable: true,
});
restoreSpies.push(() => {
Object.defineProperty(popupElement.style, prop, descriptor);
});
}
});
// Trigger re-alignment
triggerResize(popupElement);
await awaitFakeTimer();
// Restore original property descriptors
restoreSpies.forEach((restore) => restore());
// The popup's styles should not have been modified directly during
// measurement (only the final positioning values should be applied
// via the React state update, not during the measurement phase)
expect(styleChanges).not.toContain('left');
expect(styleChanges).not.toContain('top');
expect(styleChanges).not.toContain('transform');
expect(styleChanges).not.toContain('transition');
expect(styleChanges).not.toContain('overflow');
});
});