|
2 | 2 | import { act, fireEvent, render } from '@testing-library/react';
|
3 | 3 | import dayjs, { type Dayjs } from 'dayjs';
|
4 | 4 | import 'dayjs/locale/ar';
|
5 |
| -import { spyElementPrototype } from 'rc-util/lib/test/domHook'; |
| 5 | +import { spyElementPrototype, spyElementPrototypes } from 'rc-util/lib/test/domHook'; |
6 | 6 | import { resetWarned } from 'rc-util/lib/warning';
|
7 | 7 | import React from 'react';
|
8 | 8 | import type { RangePickerProps } from '../src';
|
@@ -43,6 +43,89 @@ describe('NewPicker.Range', () => {
|
43 | 43 | jest.useRealTimers();
|
44 | 44 | });
|
45 | 45 |
|
| 46 | + let rangeRect = { x: 0, y: 0, width: 0, height: 0 }; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + rangeRect = { |
| 50 | + x: 0, |
| 51 | + y: 0, |
| 52 | + width: 200, |
| 53 | + height: 100, |
| 54 | + }; |
| 55 | + |
| 56 | + document.documentElement.scrollLeft = 0; |
| 57 | + // jest.useFakeTimers(); |
| 58 | + }); |
| 59 | + |
| 60 | + beforeAll(() => { |
| 61 | + jest.spyOn(document.documentElement, 'scrollWidth', 'get').mockReturnValue(1000); |
| 62 | + |
| 63 | + // Viewport size |
| 64 | + spyElementPrototypes(HTMLElement, { |
| 65 | + clientWidth: { |
| 66 | + get: () => 400, |
| 67 | + }, |
| 68 | + clientHeight: { |
| 69 | + get: () => 400, |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + // Popup size |
| 74 | + spyElementPrototypes(HTMLDivElement, { |
| 75 | + getBoundingClientRect() { |
| 76 | + if (this.className.includes('rc-picker-dropdown')) { |
| 77 | + return { |
| 78 | + x: 0, |
| 79 | + y: 0, |
| 80 | + width: 300, |
| 81 | + height: 100, |
| 82 | + }; |
| 83 | + } |
| 84 | + if (this.className.includes('rc-picker-range')) { |
| 85 | + return rangeRect; |
| 86 | + } |
| 87 | + if (this.className.includes('rc-picker')) { |
| 88 | + return rangeRect; |
| 89 | + } |
| 90 | + }, |
| 91 | + offsetWidth: { |
| 92 | + get() { |
| 93 | + if (this.className.includes('rc-picker-range-wrapper')) { |
| 94 | + return rangeRect.width; |
| 95 | + } |
| 96 | + if (this.className.includes('rc-picker-range-arrow')) { |
| 97 | + return 10; |
| 98 | + } |
| 99 | + if (this.className.includes('rc-picker-input')) { |
| 100 | + return 100; |
| 101 | + } |
| 102 | + if (this.className.includes('rc-picker-dropdown')) { |
| 103 | + return 300; |
| 104 | + } |
| 105 | + }, |
| 106 | + }, |
| 107 | + offsetLeft: { |
| 108 | + get() { |
| 109 | + if (this.className.includes('rc-picker-input')) { |
| 110 | + return 0; |
| 111 | + } |
| 112 | + }, |
| 113 | + }, |
| 114 | + }); |
| 115 | + spyElementPrototypes(HTMLElement, { |
| 116 | + offsetParent: { |
| 117 | + get: () => document.body, |
| 118 | + }, |
| 119 | + offsetWidth: { |
| 120 | + get() { |
| 121 | + if (this.tagName === 'BODY') { |
| 122 | + return 200; |
| 123 | + } |
| 124 | + }, |
| 125 | + }, |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
46 | 129 | describe('PickerValue', () => {
|
47 | 130 | it('defaultPickerValue should reset every time when opened', () => {
|
48 | 131 | const { container } = render(
|
@@ -1098,7 +1181,7 @@ describe('NewPicker.Range', () => {
|
1098 | 1181 | it('pass tabIndex', () => {
|
1099 | 1182 | const { container } = render(
|
1100 | 1183 | <div>
|
1101 |
| - <DayRangePicker tabIndex={-1}/> |
| 1184 | + <DayRangePicker tabIndex={-1} /> |
1102 | 1185 | </div>,
|
1103 | 1186 | );
|
1104 | 1187 |
|
@@ -1338,4 +1421,57 @@ describe('NewPicker.Range', () => {
|
1338 | 1421 | }
|
1339 | 1422 | expect(existed).toBeTruthy();
|
1340 | 1423 | });
|
| 1424 | + |
| 1425 | + describe('pupop aligned position', () => { |
| 1426 | + it('the arrow should be set to `inset-inline-start` when the popup is aligned to `bottomLeft`.', async () => { |
| 1427 | + render(<DayRangePicker open />); |
| 1428 | + |
| 1429 | + await act(async () => { |
| 1430 | + jest.runAllTimers(); |
| 1431 | + |
| 1432 | + await Promise.resolve(); |
| 1433 | + }); |
| 1434 | + expect(document.querySelector('.rc-picker-range-arrow')).toHaveStyle({ |
| 1435 | + 'inset-inline-start': '0', |
| 1436 | + }); |
| 1437 | + }); |
| 1438 | + |
| 1439 | + it('the arrow should be set to `inset-inline-end` when the popup is aligned to `bottomRight`.', async () => { |
| 1440 | + const mock = spyElementPrototypes(HTMLDivElement, { |
| 1441 | + getBoundingClientRect() { |
| 1442 | + if (this.className.includes('rc-picker-dropdown')) { |
| 1443 | + return { |
| 1444 | + x: 0, |
| 1445 | + y: 0, |
| 1446 | + width: 300, |
| 1447 | + height: 100, |
| 1448 | + }; |
| 1449 | + } |
| 1450 | + if (this.className.includes('rc-picker-range')) { |
| 1451 | + return { |
| 1452 | + ...rangeRect, |
| 1453 | + x: 300, |
| 1454 | + }; |
| 1455 | + } |
| 1456 | + }, |
| 1457 | + }); |
| 1458 | + |
| 1459 | + render(<DayRangePicker open />); |
| 1460 | + |
| 1461 | + await act(async () => { |
| 1462 | + jest.runAllTimers(); |
| 1463 | + |
| 1464 | + await Promise.resolve(); |
| 1465 | + }); |
| 1466 | + expect(document.querySelector('.rc-picker-range-arrow')).toHaveStyle({ |
| 1467 | + 'inset-inline-end': '100px', |
| 1468 | + }); |
| 1469 | + |
| 1470 | + expect(document.querySelector('.rc-picker-active-bar')).toHaveStyle({ |
| 1471 | + 'inset-inline-end': '100px', |
| 1472 | + }); |
| 1473 | + |
| 1474 | + mock.mockRestore(); |
| 1475 | + }); |
| 1476 | + }); |
1341 | 1477 | });
|
0 commit comments