-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
/
Popover.test.js
1085 lines (955 loc) · 32.7 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as React from 'react';
import { expect } from 'chai';
import { spy, stub, match } from 'sinon';
import { act, createRenderer, reactMajor, screen } from '@mui/internal-test-utils';
import PropTypes from 'prop-types';
import Modal from '@mui/material/Modal';
import Paper, { paperClasses } from '@mui/material/Paper';
import Popover, { popoverClasses as classes, PopoverPaper } from '@mui/material/Popover';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import { getOffsetLeft, getOffsetTop } from './Popover';
import useForkRef from '../utils/useForkRef';
import styled from '../styles/styled';
import describeConformance from '../../test/describeConformance';
const FakePaper = React.forwardRef(function FakeWidthPaper(props, ref) {
const handleMocks = React.useCallback((paperInstance) => {
if (paperInstance) {
// For jsdom
Object.defineProperty(paperInstance, 'offsetWidth', { value: 0 });
Object.defineProperty(paperInstance, 'offsetHeight', { value: 0 });
}
}, []);
const handleRef = useForkRef(ref, handleMocks);
return (
<div
tabIndex={-1}
ref={handleRef}
style={{
width: 0,
height: 0,
}}
{...props}
/>
);
});
const ReplacementPaper = styled(Paper, {
name: 'ReplacementPaper',
slot: 'Paper',
overridesResolver: (props, styles) => styles.paper,
})({
backgroundColor: 'red',
});
describe('<Popover />', () => {
const { clock, render } = createRenderer({ clock: 'fake' });
describeConformance(<Popover anchorEl={() => document.createElement('div')} open />, () => ({
classes,
inheritComponent: Modal,
render,
muiName: 'MuiPopover',
refInstanceof: window.HTMLDivElement,
testDeepOverrides: { slotName: 'paper', slotClassName: classes.paper },
slots: {
root: {
expectedClassName: classes.root,
},
paper: {
expectedClassName: classes.paper,
testWithComponent: React.forwardRef((props, ref) => (
<ReplacementPaper ref={ref} {...props} data-testid="custom" />
)),
},
},
skip: [
'rootClass', // portal, can't determine the root
'componentProp',
'componentsProp',
'themeDefaultProps', // portal, can't determine the root
'themeStyleOverrides', // portal, can't determine the root
'themeVariants',
],
}));
describe('root node', () => {
it('should render a Modal with an invisible backdrop as the root node', () => {
function TestBackdrop(props) {
const { open, invisible } = props;
if (!open) {
return null;
}
return <div data-testid="backdrop" data-invisible={invisible} />;
}
render(
<Popover
open
anchorEl={document.createElement('div')}
slotProps={{
root: {
slots: {
backdrop: TestBackdrop,
},
},
}}
>
<div />
</Popover>,
);
expect(screen.getByTestId('backdrop')).to.have.attribute('data-invisible', 'true');
});
it('should only render its children when open', () => {
const { setProps } = render(
<Popover open={false} anchorEl={document.createElement('div')}>
<div data-testid="children" />
</Popover>,
);
expect(screen.queryByTestId('children')).to.equal(null);
setProps({ open: true });
expect(screen.getByTestId('children')).not.to.equal(null);
});
it('hide its children immediately when closing but transition them out', () => {
const { setProps } = render(
<Popover open anchorEl={document.createElement('div')} transitionDuration={1974}>
<div data-testid="children" />
</Popover>,
);
setProps({ open: false });
expect(screen.getByTestId('children')).toBeInaccessible();
clock.tick(1974);
expect(screen.queryByTestId('children')).to.equal(null);
});
describe('getOffsetTop', () => {
it('should return vertical when vertical is a number', () => {
const vertical = 1;
const offsetTop = getOffsetTop('', vertical);
expect(offsetTop).to.equal(vertical);
});
it("should return half of rect.height if vertical is 'center'", () => {
const vertical = 'center';
const rect = { height: 1 };
const offsetTop = getOffsetTop(rect, vertical);
expect(offsetTop).to.equal(rect.height / 2);
});
it("should return rect.height if vertical is 'bottom'", () => {
const vertical = 'bottom';
const rect = { height: 1 };
const offsetTop = getOffsetTop(rect, vertical);
expect(offsetTop).to.equal(rect.height);
});
it('should return zero if vertical is something else', () => {
const vertical = undefined;
const rect = { height: 1 };
const offsetTop = getOffsetTop(rect, vertical);
expect(offsetTop).to.equal(0);
});
});
describe('getOffsetLeft', () => {
it('should return horizontal when horizontal is a number', () => {
const horizontal = 1;
const offsetLeft = getOffsetLeft('', horizontal);
expect(offsetLeft).to.equal(horizontal);
});
it("should return half of rect.width if horizontal is 'center'", () => {
const horizontal = 'center';
const rect = { width: 1 };
const offsetLeft = getOffsetLeft(rect, horizontal);
expect(offsetLeft).to.equal(rect.width / 2);
});
it("should return rect.width if horizontal is 'right'", () => {
const horizontal = 'right';
const rect = { width: 1 };
const offsetLeft = getOffsetLeft(rect, horizontal);
expect(offsetLeft).to.equal(rect.width);
});
it('should return zero if horizontal is something else', () => {
const horizontal = undefined;
const rect = { width: 1 };
const offsetLeft = getOffsetLeft(rect, horizontal);
expect(offsetLeft).to.equal(0);
});
});
});
describe('transition', () => {
it('appears on mount', () => {
const handleEnter = spy();
render(
<Popover
anchorEl={document.createElement('div')}
open
TransitionProps={{ onEnter: handleEnter }}
>
<div />
</Popover>,
);
expect(handleEnter.callCount).to.equal(
// onEnter is called on mount which is run twice with Strict Effects
reactMajor >= 18 ? 2 : 1,
);
});
it('should fire Popover transition event callbacks', () => {
const handleEnter = spy();
const handleEntering = spy();
const handleEntered = spy();
const handleExit = spy();
const handleExiting = spy();
const handleExited = spy();
const { setProps } = render(
<Popover
anchorEl={document.createElement('div')}
open
transitionDuration={0}
TransitionProps={{
onEnter: handleEnter,
onEntering: handleEntering,
onEntered: handleEntered,
onExit: handleExit,
onExiting: handleExiting,
onExited: handleExited,
}}
>
<div />
</Popover>,
);
expect({
onEnter: handleEnter.callCount,
onEntered: handleEntered.callCount,
onEntering: handleEntering.callCount,
onExit: handleExit.callCount,
onExited: handleExited.callCount,
onExiting: handleExiting.callCount,
}).to.deep.equal({
// onEnter is called on mount which is run twice with Strict Effects
onEnter: reactMajor >= 18 ? 2 : 1,
onEntering: 1,
onEntered: 0,
onExit: 0,
onExiting: 0,
onExited: 0,
});
clock.tick(0);
expect({
onEnter: handleEnter.callCount,
onEntered: handleEntered.callCount,
onEntering: handleEntering.callCount,
onExit: handleExit.callCount,
onExited: handleExited.callCount,
onExiting: handleExiting.callCount,
}).to.deep.equal({
// onEnter is called on mount which is run twice with Strict Effects
onEnter: reactMajor >= 18 ? 2 : 1,
onEntering: 1,
onEntered: 1,
onExit: 0,
onExiting: 0,
onExited: 0,
});
setProps({ open: false });
expect({
onEnter: handleEnter.callCount,
onEntered: handleEntered.callCount,
onEntering: handleEntering.callCount,
onExit: handleExit.callCount,
onExited: handleExited.callCount,
onExiting: handleExiting.callCount,
}).to.deep.equal({
// onEnter is called on mount which is run twice with Strict Effects
onEnter: reactMajor >= 18 ? 2 : 1,
onEntering: 1,
onEntered: 1,
onExit: 1,
onExiting: 1,
onExited: 0,
});
clock.tick(0);
expect({
onEnter: handleEnter.callCount,
onEntered: handleEntered.callCount,
onEntering: handleEntering.callCount,
onExit: handleExit.callCount,
onExited: handleExited.callCount,
onExiting: handleExiting.callCount,
}).to.deep.equal({
// onEnter is called on mount which is run twice with Strict Effects
onEnter: reactMajor >= 18 ? 2 : 1,
onEntering: 1,
onEntered: 1,
onExit: 1,
onExiting: 1,
onExited: 1,
});
});
});
describe('paper', () => {
it('should have the paper class', () => {
render(
<Popover
anchorEl={document.createElement('div')}
open
PaperProps={{ 'data-testid': 'paper' }}
>
<div />
</Popover>,
);
expect(screen.getByTestId('paper')).to.have.class(classes.paper);
});
it('should have a elevation prop passed down', () => {
const { setProps } = render(
<Popover
anchorEl={document.createElement('div')}
open
PaperProps={{ 'data-testid': 'paper' }}
>
<div />
</Popover>,
);
expect(screen.getByTestId('paper')).to.have.class(paperClasses.elevation8);
setProps({ slotProps: { paper: { 'data-testid': 'paper', elevation: 16 } } });
expect(screen.getByTestId('paper')).to.have.class(paperClasses.elevation16);
});
});
describe('prop: PaperProps', () => {
describe('ref', () => {
it('should position popover correctly', () => {
const handleEntering = spy();
render(
<Popover
anchorEl={document.createElement('div')}
open
PaperProps={{ 'data-testid': 'Popover', ref: () => null }}
TransitionProps={{ onEntering: handleEntering }}
>
<div />
</Popover>,
);
expect(handleEntering.args[0][0]).toHaveInlineStyle({ top: '16px', left: '16px' });
});
});
describe('className', () => {
it('should add the className to the paper', () => {
const className = 'MyPaperClassName';
render(
<Popover
anchorEl={document.createElement('div')}
open
PaperProps={{ 'data-testid': 'paper', className }}
>
<div />
</Popover>,
);
expect(screen.getByTestId('paper')).to.have.class(className);
});
});
});
describe('transition lifecycle', () => {
describe('handleEntering(element)', () => {
it('should set the inline styles for the enter phase', () => {
const handleEntering = spy();
const { setProps } = render(
<Popover
anchorEl={document.createElement('div')}
open={false}
TransitionProps={{ onEntering: handleEntering }}
>
<div />
</Popover>,
);
setProps({
open: true,
});
const element = handleEntering.args[0][0];
expect(element.style.top === '16px' && element.style.left === '16px').to.equal(true);
expect(element.style.transformOrigin).to.match(/-16px -16px( 0px)?/);
});
});
describe('paper styles', () => {
it('should have opacity 1 only after onEntering has been called', () => {
const onEnteringSpy = spy();
const paperRenderSpy = spy(PopoverPaper, 'render');
const { setProps } = render(
<Popover
anchorEl={document.createElement('div')}
TransitionProps={{
onEntering: onEnteringSpy,
}}
open={false}
>
<div />
</Popover>,
);
setProps({ open: true });
expect(
paperRenderSpy
.withArgs(match({ style: { opacity: 1 } }))
.firstCall.calledAfter(onEnteringSpy.lastCall),
).to.equal(true);
});
});
});
describe('prop: anchorEl', () => {
it('should accept a function', () => {
const anchorElSpy = spy();
render(
<Popover open={false} anchorEl={anchorElSpy}>
<div />
</Popover>,
);
expect(anchorElSpy.callCount).to.be.greaterThanOrEqual(1);
});
it('should accept a virtual element', () => {
const top = 100;
const left = 300;
const virtualElement = {
nodeType: 1,
getBoundingClientRect: () => ({
x: 0,
y: 0,
top,
left,
bottom: 0,
right: 0,
height: 0,
width: 0,
}),
};
render(
<Popover
open
anchorEl={virtualElement}
transitionDuration={0}
slotProps={{ paper: { 'data-testid': 'paper' } }}
>
<div />
</Popover>,
);
expect(screen.getByTestId('paper')).toHaveInlineStyle({
top: `${top}px`,
left: `${left}px`,
});
});
});
describe('positioning on an anchor', () => {
let anchorEl;
function openPopover(anchorOrigin) {
render(
<Popover
anchorEl={anchorEl}
anchorOrigin={anchorOrigin}
open
transitionDuration={0}
PaperProps={{ 'data-testid': 'paper' }}
>
<div />
</Popover>,
);
clock.tick(0);
}
beforeEach(() => {
anchorEl = document.createElement('div');
anchorEl.style.width = '50px';
anchorEl.style.height = '50px';
anchorEl.style.position = 'absolute';
anchorEl.style.top = '100px';
anchorEl.style.left = '100px';
document.body.appendChild(anchorEl);
});
afterEach(() => {
document.body.removeChild(anchorEl);
});
it('should be positioned over the top left of the anchor', () => {
openPopover({ vertical: 'top', horizontal: 'left' });
const anchorRect = anchorEl.getBoundingClientRect();
const top = anchorRect.top <= 16 ? 16 : anchorRect.top;
const left = anchorRect.left <= 16 ? 16 : anchorRect.left;
expect(screen.getByTestId('paper')).toHaveInlineStyle({ top: `${top}px`, left: `${left}px` });
});
it('should be positioned over the center left of the anchor', () => {
openPopover({ vertical: 'center', horizontal: 'left' });
const anchorRect = anchorEl.getBoundingClientRect();
const anchorTop = anchorRect.top + anchorRect.height / 2;
const top = anchorTop <= 16 ? 16 : anchorTop;
const left = anchorRect.left <= 16 ? 16 : anchorRect.left;
expect(screen.getByTestId('paper')).toHaveInlineStyle({ top: `${top}px`, left: `${left}px` });
});
it('should be positioned over the bottom left of the anchor', () => {
openPopover({ vertical: 'bottom', horizontal: 'left' });
const anchorRect = anchorEl.getBoundingClientRect();
const top = anchorRect.bottom <= 16 ? 16 : anchorRect.bottom;
const left = anchorRect.left <= 16 ? 16 : anchorRect.left;
expect(screen.getByTestId('paper')).toHaveInlineStyle({ top: `${top}px`, left: `${left}px` });
});
it('should be positioned over the center center of the anchor', () => {
openPopover({ vertical: 'center', horizontal: 'center' });
const anchorRect = anchorEl.getBoundingClientRect();
const anchorTop = anchorRect.top + anchorRect.height / 2;
const anchorLeft = anchorRect.left + anchorRect.height / 2;
const top = anchorTop <= 16 ? 16 : anchorTop;
const left = anchorLeft <= 16 ? 16 : anchorLeft;
expect(screen.getByTestId('paper')).toHaveInlineStyle({ top: `${top}px`, left: `${left}px` });
});
it('should be positioned over the top right of the anchor', () => {
openPopover({ vertical: 'top', horizontal: 'right' });
const anchorRect = anchorEl.getBoundingClientRect();
const top = anchorRect.top <= 16 ? 16 : anchorRect.top;
const left = anchorRect.right <= 16 ? 16 : anchorRect.right;
expect(screen.getByTestId('paper')).toHaveInlineStyle({ top: `${top}px`, left: `${left}px` });
});
it('should be positioned over the bottom right of the anchor', () => {
openPopover({ vertical: 'bottom', horizontal: 'right' });
const anchorRect = anchorEl.getBoundingClientRect();
const top = anchorRect.bottom <= 16 ? 16 : anchorRect.bottom;
const left = anchorRect.right <= 16 ? 16 : anchorRect.right;
expect(screen.getByTestId('paper')).toHaveInlineStyle({ top: `${top}px`, left: `${left}px` });
});
});
it('should pass through container prop if container and anchorEl props are provided', () => {
const container = document.createElement('div');
const anchorEl = document.createElement('div');
render(<Popover anchorEl={anchorEl} data-testid="popover" container={container} open />);
expect(container.querySelector('[data-testid="popover"]').parentElement).to.equal(container);
});
it("should use anchorEl's parent body as container if container not provided", () => {
const anchorEl = document.createElement('div');
render(<Popover anchorEl={anchorEl} data-testid="popover" open />);
expect(screen.getByTestId('popover').parentElement).to.equal(anchorEl.ownerDocument.body);
});
describe('warnings', () => {
beforeEach(() => {
PropTypes.resetWarningCache();
});
it('should warn if anchorEl is not valid', () => {
expect(() => {
PropTypes.checkPropTypes(
Popover.propTypes,
{ classes: {}, open: true },
'prop',
'MockedPopover',
);
}).toErrorDev('It should be an Element or PopoverVirtualElement instance');
});
it('warns if a component for the Paper is used that cant hold a ref', () => {
expect(() => {
PropTypes.checkPropTypes(
Popover.propTypes,
{
anchorEl: document.createElement('div'),
classes: {},
open: false,
PaperProps: { component: () => <div />, elevation: 4 },
},
'prop',
'MockedPopover',
);
}).toErrorDev(
'Warning: Failed prop type: Invalid prop `PaperProps.component` supplied to `MockedPopover`. Expected an element type that can hold a ref.',
);
});
});
describe('prop anchorReference="anchorPosition"', () => {
const anchorPosition = { top: 300, left: 500 };
function openPopover(anchorOrigin) {
render(
<Popover
anchorEl={document.createElement('div')}
anchorReference="anchorPosition"
anchorPosition={anchorPosition}
anchorOrigin={anchorOrigin}
open
transitionDuration={0}
PaperProps={{ 'data-testid': 'paper' }}
>
<div />
</Popover>,
);
clock.tick(0);
}
it('should be positioned according to the passed coordinates', () => {
openPopover();
expect(screen.getByTestId('paper')).toHaveInlineStyle({
top: `${anchorPosition.top}px`,
left: `${anchorPosition.left}px`,
});
});
it('should ignore the anchorOrigin prop when being positioned', () => {
openPopover({ vertical: 'top', horizontal: 'right' });
expect(screen.getByTestId('paper')).toHaveInlineStyle({
top: `${anchorPosition.top}px`,
left: `${anchorPosition.left}px`,
});
});
});
describe('prop anchorReference="none"', () => {
it('should not try to change the position', () => {
const anchorEl = document.createElement('div');
render(
<Popover
anchorEl={anchorEl}
anchorReference="none"
open
transitionDuration={0}
PaperProps={{
'data-testid': 'paper',
style: {
top: 11,
left: 12,
},
}}
>
<div />
</Popover>,
);
expect(screen.getByTestId('paper')).toHaveInlineStyle({
top: `11px`,
left: `12px`,
});
});
});
describe('update position', () => {
let windowInnerHeight;
beforeEach(() => {
windowInnerHeight = window.innerHeight;
window.innerHeight = 8;
});
afterEach(() => {
window.innerHeight = windowInnerHeight;
});
it('should recalculate position if the popover is open', () => {
let element;
const anchorEl = document.createElement('div');
stub(anchorEl, 'getBoundingClientRect').callsFake(() => ({
left: 0,
top: 9,
}));
render(
<Popover
anchorEl={anchorEl}
open
TransitionProps={{
onEntering: (node) => {
element = node;
},
}}
transitionDuration={0}
marginThreshold={8}
>
<div />
</Popover>,
);
const beforeStyle = {
top: element.style.top,
left: element.style.left,
transformOrigin: element.style.transformOrigin,
};
window.innerHeight = windowInnerHeight * 2;
window.dispatchEvent(new window.Event('resize'));
clock.tick(166);
const afterStyle = {
top: element.style.top,
left: element.style.left,
transformOrigin: element.style.transformOrigin,
};
expect(beforeStyle).not.to.deep.equal(afterStyle);
});
it('should not recalculate position if the popover is closed', () => {
let element;
const mockedAnchor = document.createElement('div');
stub(mockedAnchor, 'getBoundingClientRect').callsFake(() => ({
left: 0,
top: 9,
}));
const { setProps } = render(
<Popover
anchorEl={mockedAnchor}
open
TransitionProps={{
onEntering: (node) => {
element = node;
},
}}
transitionDuration={0}
marginThreshold={8}
>
<div />
</Popover>,
);
const beforeStyle = {
top: element.style.top,
left: element.style.left,
transformOrigin: element.style.transformOrigin,
};
window.innerHeight = windowInnerHeight * 2;
window.dispatchEvent(new window.Event('resize'));
setProps({ open: false });
clock.tick(166);
const afterStyle = {
top: element.style.top,
left: element.style.left,
transformOrigin: element.style.transformOrigin,
};
expect(beforeStyle).to.deep.equal(afterStyle);
});
it('should be able to manually recalculate position', () => {
let element;
const mockedAnchor = document.createElement('div');
stub(mockedAnchor, 'getBoundingClientRect').callsFake(() => ({
left: 0,
top: 9,
}));
const { setProps } = render(
<Popover
anchorEl={mockedAnchor}
open
TransitionProps={{
onEntering: (node) => {
element = node;
},
}}
transitionDuration={0}
marginThreshold={8}
>
<div />
</Popover>,
);
let popoverActions;
setProps({
action: (actions) => {
popoverActions = actions;
},
});
const beforeStyle = {
top: element.style.top,
left: element.style.left,
transformOrigin: element.style.transformOrigin,
};
window.innerHeight = windowInnerHeight * 2;
expect(typeof popoverActions.updatePosition === 'function').to.equal(true);
act(() => {
popoverActions.updatePosition();
});
clock.tick(166);
const afterStyle = {
top: element.style.top,
left: element.style.left,
transformOrigin: element.style.transformOrigin,
};
expect(beforeStyle).not.to.deep.equal(afterStyle);
});
});
describe('prop: marginThreshold', () => {
[0, 18, 16].forEach((marginThreshold) => {
function getElementStyleOfOpenPopover(anchorEl = document.createElement('svg')) {
let style;
render(
<Popover
anchorEl={anchorEl}
open
TransitionProps={{
onEntering: (node) => {
style = node.style;
},
}}
marginThreshold={marginThreshold}
slotProps={{ paper: { component: FakePaper } }}
>
<div />
</Popover>,
);
return style;
}
describe(`positioning when \`marginThreshold=${marginThreshold}\``, () => {
specify('when no movement is needed', () => {
const negative = marginThreshold === 0 ? '' : '-';
const positioningStyle = getElementStyleOfOpenPopover();
expect(positioningStyle.top).to.equal(`${marginThreshold}px`);
expect(positioningStyle.left).to.equal(`${marginThreshold}px`);
expect(positioningStyle.transformOrigin).to.match(
new RegExp(`${negative}${marginThreshold}px ${negative}${marginThreshold}px( 0px)?`),
);
});
specify('top < marginThreshold', () => {
const mockedAnchor = document.createElement('div');
stub(mockedAnchor, 'getBoundingClientRect').callsFake(() => ({
left: marginThreshold,
top: marginThreshold - 1,
}));
const positioningStyle = getElementStyleOfOpenPopover(mockedAnchor);
expect(positioningStyle.top).to.equal(`${marginThreshold}px`);
expect(positioningStyle.left).to.equal(`${marginThreshold}px`);
expect(positioningStyle.transformOrigin).to.match(/0px -1px( 0px)?/);
});
describe('bottom > heightThreshold', () => {
let windowInnerHeight;
before(() => {
windowInnerHeight = window.innerHeight;
window.innerHeight = marginThreshold * 2;
});
after(() => {
window.innerHeight = windowInnerHeight;
});
specify('test', () => {
const mockedAnchor = document.createElement('div');
stub(mockedAnchor, 'getBoundingClientRect').callsFake(() => ({
left: marginThreshold,
top: marginThreshold + 1,
}));
const positioningStyle = getElementStyleOfOpenPopover(mockedAnchor);
expect(positioningStyle.top).to.equal(`${marginThreshold}px`);
expect(positioningStyle.left).to.equal(`${marginThreshold}px`);
expect(positioningStyle.transformOrigin).to.match(/0px 1px( 0px)?/);
});
});
specify('left < marginThreshold', () => {
const mockedAnchor = document.createElement('div');
stub(mockedAnchor, 'getBoundingClientRect').callsFake(() => ({
left: marginThreshold - 1,
top: marginThreshold,
}));
const positioningStyle = getElementStyleOfOpenPopover(mockedAnchor);
expect(positioningStyle.top).to.equal(`${marginThreshold}px`);
expect(positioningStyle.left).to.equal(`${marginThreshold}px`);
expect(positioningStyle.transformOrigin).to.match(/-1px 0px( 0px)?/);
});
describe('right > widthThreshold', () => {
let innerWidthContainer;
before(() => {
innerWidthContainer = window.innerWidth;
window.innerWidth = marginThreshold * 2;
});
after(() => {
window.innerWidth = innerWidthContainer;
});
specify('test', () => {
const mockedAnchor = document.createElement('div');
stub(mockedAnchor, 'getBoundingClientRect').callsFake(() => ({
left: marginThreshold + 1,
top: marginThreshold,
}));
const positioningStyle = getElementStyleOfOpenPopover(mockedAnchor);
expect(positioningStyle.top).to.equal(`${marginThreshold}px`);
expect(positioningStyle.left).to.equal(`${marginThreshold}px`);
expect(positioningStyle.transformOrigin).to.match(/1px 0px( 0px)?/);
});
});
});
});
describe('positioning when `marginThreshold=null`', () => {
it('should not apply the marginThreshold when marginThreshold is null', () => {
const mockedAnchor = document.createElement('div');
const valueOutsideWindow = -100;
stub(mockedAnchor, 'getBoundingClientRect').callsFake(() => ({
top: valueOutsideWindow,
left: valueOutsideWindow,
}));
let style;
render(
<Popover
anchorEl={mockedAnchor}
open
TransitionProps={{
onEntering: (node) => {
style = node.style;
},
}}
marginThreshold={null}
slotProps={{ paper: { component: FakePaper } }}
>
<div />
</Popover>,
);
expect(style.top).to.equal(`${valueOutsideWindow}px`);
expect(style.left).to.equal(`${valueOutsideWindow}px`);
expect(style.transformOrigin).to.match(/0px 0px( 0px)?/);
});
});
});
describe('prop: transitionDuration', () => {
it('should apply the auto prop if supported', () => {
const TransitionComponent = React.forwardRef((props, ref) => (
<div data-testid="transition" data-timeout={props.timeout} ref={ref} tabIndex={-1} />
));
TransitionComponent.muiSupportAuto = true;
render(
<Popover
anchorEl={document.createElement('div')}
open
TransitionComponent={TransitionComponent}
>
<div />
</Popover>,
);
expect(screen.getByTestId('transition')).to.have.attribute('data-timeout').equals('auto');