|
| 1 | +import { act, cleanup, render } from '@testing-library/react'; |
| 2 | +import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; |
| 3 | +import React from 'react'; |
| 4 | +import { DayRangePicker } from './util/commonUtil'; |
| 5 | + |
| 6 | +describe('the popup arrow should be placed in the correct position.', () => { |
| 7 | + let rangeRect = { x: 0, y: 0, width: 0, height: 0 }; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + rangeRect = { |
| 11 | + x: 0, |
| 12 | + y: 0, |
| 13 | + width: 200, |
| 14 | + height: 100, |
| 15 | + }; |
| 16 | + |
| 17 | + document.documentElement.scrollLeft = 0; |
| 18 | + }); |
| 19 | + |
| 20 | + beforeAll(() => { |
| 21 | + jest.spyOn(document.documentElement, 'scrollWidth', 'get').mockReturnValue(1000); |
| 22 | + |
| 23 | + // Viewport size |
| 24 | + spyElementPrototypes(HTMLElement, { |
| 25 | + clientWidth: { |
| 26 | + get: () => 400, |
| 27 | + }, |
| 28 | + clientHeight: { |
| 29 | + get: () => 400, |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + // Popup size |
| 34 | + spyElementPrototypes(HTMLDivElement, { |
| 35 | + getBoundingClientRect() { |
| 36 | + if (this.className.includes('rc-picker-dropdown')) { |
| 37 | + return { |
| 38 | + x: 0, |
| 39 | + y: 0, |
| 40 | + width: 300, |
| 41 | + height: 100, |
| 42 | + }; |
| 43 | + } |
| 44 | + if (this.className.includes('rc-picker-range')) { |
| 45 | + return rangeRect; |
| 46 | + } |
| 47 | + }, |
| 48 | + offsetWidth: { |
| 49 | + get() { |
| 50 | + if (this.className.includes('rc-picker-range-wrapper')) { |
| 51 | + return rangeRect.width; |
| 52 | + } |
| 53 | + if (this.className.includes('rc-picker-range-arrow')) { |
| 54 | + return 10; |
| 55 | + } |
| 56 | + if (this.className.includes('rc-picker-input')) { |
| 57 | + return 100; |
| 58 | + } |
| 59 | + if (this.className.includes('rc-picker-dropdown')) { |
| 60 | + return 300; |
| 61 | + } |
| 62 | + }, |
| 63 | + }, |
| 64 | + offsetLeft: { |
| 65 | + get() { |
| 66 | + if (this.className.includes('rc-picker-input')) { |
| 67 | + return 0; |
| 68 | + } |
| 69 | + }, |
| 70 | + }, |
| 71 | + }); |
| 72 | + spyElementPrototypes(HTMLElement, { |
| 73 | + offsetParent: { |
| 74 | + get: () => document.body, |
| 75 | + }, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + beforeEach(() => { |
| 80 | + jest.useFakeTimers(); |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(() => { |
| 84 | + cleanup(); |
| 85 | + jest.useRealTimers(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('the arrow should be set to `inset-inline-start` when the popup is aligned to `bottomLeft`.', async () => { |
| 89 | + render(<DayRangePicker open />); |
| 90 | + |
| 91 | + await act(async () => { |
| 92 | + jest.runAllTimers(); |
| 93 | + |
| 94 | + await Promise.resolve(); |
| 95 | + }); |
| 96 | + expect(document.querySelector('.rc-picker-range-arrow')).toHaveStyle({ |
| 97 | + 'inset-inline-start': '0', |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('the arrow should be set to `inset-inline-end` when the popup is aligned to `bottomRight`.', async () => { |
| 102 | + const mock = spyElementPrototypes(HTMLDivElement, { |
| 103 | + getBoundingClientRect() { |
| 104 | + if (this.className.includes('rc-picker-dropdown')) { |
| 105 | + return { |
| 106 | + x: 0, |
| 107 | + y: 0, |
| 108 | + width: 300, |
| 109 | + height: 100, |
| 110 | + }; |
| 111 | + } |
| 112 | + if (this.className.includes('rc-picker-range')) { |
| 113 | + return { |
| 114 | + ...rangeRect, |
| 115 | + x: 300, |
| 116 | + }; |
| 117 | + } |
| 118 | + }, |
| 119 | + }); |
| 120 | + |
| 121 | + render(<DayRangePicker open />); |
| 122 | + |
| 123 | + await act(async () => { |
| 124 | + jest.runAllTimers(); |
| 125 | + |
| 126 | + await Promise.resolve(); |
| 127 | + }); |
| 128 | + expect(document.querySelector('.rc-picker-range-arrow')).toHaveStyle({ |
| 129 | + 'inset-inline-end': '0', |
| 130 | + }); |
| 131 | + |
| 132 | + mock.mockRestore(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments