Skip to content

Commit 9e4ee41

Browse files
committed
test: change the proxy offsetLeft to proxy getBoundingClientRect
1 parent 05eeced commit 9e4ee41

File tree

2 files changed

+81
-18
lines changed

2 files changed

+81
-18
lines changed

tests/new-range.spec.tsx

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ describe('NewPicker.Range', () => {
5757
beforeAll(() => {
5858
jest.spyOn(document.documentElement, 'scrollWidth', 'get').mockReturnValue(1000);
5959

60-
// Viewport size
61-
spyElementPrototypes(HTMLElement, {
62-
clientWidth: {
63-
get: () => 400,
64-
},
65-
clientHeight: {
66-
get: () => 400,
67-
},
68-
});
69-
7060
// Popup size
7161
spyElementPrototypes(HTMLDivElement, {
7262
getBoundingClientRect() {
@@ -110,6 +100,13 @@ describe('NewPicker.Range', () => {
110100
},
111101
});
112102
spyElementPrototypes(HTMLElement, {
103+
// Viewport size
104+
clientWidth: {
105+
get: () => 400,
106+
},
107+
clientHeight: {
108+
get: () => 400,
109+
},
113110
offsetParent: {
114111
get: () => document.body,
115112
},
@@ -120,6 +117,17 @@ describe('NewPicker.Range', () => {
120117
}
121118
},
122119
},
120+
// offsetParent
121+
getBoundingClientRect() {
122+
if (this.tagName === 'BODY') {
123+
return {
124+
x: 0,
125+
y: 0,
126+
width: 200,
127+
height: 200,
128+
};
129+
}
130+
},
123131
});
124132
});
125133

@@ -1423,14 +1431,28 @@ describe('NewPicker.Range', () => {
14231431
it('the arrow should be set to `inset-inline-start` when the popup is aligned to `bottomLeft`.', async () => {
14241432
render(<DayRangePicker open />);
14251433

1434+
const oriGetComputedStyle = window.getComputedStyle;
1435+
window.getComputedStyle = (ele: HTMLElement) => {
1436+
const retObj = oriGetComputedStyle(ele);
1437+
1438+
retObj.borderRightWidth = '4px';
1439+
retObj.borderLeftWidth = '2px';
1440+
return retObj;
1441+
};
1442+
14261443
await act(async () => {
14271444
jest.runAllTimers();
14281445

14291446
await Promise.resolve();
14301447
});
1448+
14311449
expect(document.querySelector('.rc-picker-range-arrow')).toHaveStyle({
14321450
'inset-inline-start': '0',
14331451
});
1452+
expect(document.querySelector('.rc-picker-active-bar')).toHaveStyle({
1453+
'inset-inline-start': '-2px',
1454+
});
1455+
window.getComputedStyle = oriGetComputedStyle;
14341456
});
14351457

14361458
it('the arrow should be set to `inset-inline-end` when the popup is aligned to `bottomRight`.', async () => {
@@ -1450,9 +1472,24 @@ describe('NewPicker.Range', () => {
14501472
x: 300,
14511473
};
14521474
}
1475+
if (this.className.includes('rc-picker-input')) {
1476+
return {
1477+
...rangeRect,
1478+
width: 100,
1479+
};
1480+
}
14531481
},
14541482
});
14551483

1484+
const oriGetComputedStyle = window.getComputedStyle;
1485+
window.getComputedStyle = (ele: HTMLElement) => {
1486+
const retObj = oriGetComputedStyle(ele);
1487+
1488+
retObj.borderRightWidth = '4px';
1489+
retObj.borderLeftWidth = '2px';
1490+
return retObj;
1491+
};
1492+
14561493
render(<DayRangePicker open />);
14571494

14581495
await act(async () => {
@@ -1465,10 +1502,11 @@ describe('NewPicker.Range', () => {
14651502
});
14661503

14671504
expect(document.querySelector('.rc-picker-active-bar')).toHaveStyle({
1468-
'inset-inline-end': '100px',
1505+
'inset-inline-end': '96px',
14691506
});
14701507

14711508
mock.mockRestore();
1509+
window.getComputedStyle = oriGetComputedStyle;
14721510
});
14731511
});
14741512
});

tests/range.spec.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ describe('Picker.Range', () => {
541541
it('pass tabIndex', () => {
542542
const { container } = render(
543543
<div>
544-
<DayRangePicker tabIndex={-1}/>
544+
<DayRangePicker tabIndex={-1} />
545545
</div>,
546546
);
547547

@@ -705,12 +705,7 @@ describe('Picker.Range', () => {
705705
});
706706

707707
it('prefix', () => {
708-
render(
709-
<DayRangePicker
710-
prefix={<span className="prefix" />}
711-
allowClear
712-
/>,
713-
);
708+
render(<DayRangePicker prefix={<span className="prefix" />} allowClear />);
714709
expect(document.querySelector('.prefix')).toBeInTheDocument();
715710
});
716711

@@ -1771,6 +1766,36 @@ describe('Picker.Range', () => {
17711766
}
17721767
},
17731768
},
1769+
getBoundingClientRect() {
1770+
if (this.className.includes('rc-picker-dropdown')) {
1771+
return {
1772+
x: 0,
1773+
y: 0,
1774+
width: 300,
1775+
};
1776+
}
1777+
if (this.className.includes('rc-picker-range')) {
1778+
return {
1779+
x: 0,
1780+
y: 0,
1781+
width: 200,
1782+
};
1783+
}
1784+
if (this.className.includes('rc-picker-input')) {
1785+
return {
1786+
x: 100,
1787+
y: 0,
1788+
width: 100,
1789+
};
1790+
}
1791+
if (this.className.includes('rc-picker')) {
1792+
return {
1793+
x: 0,
1794+
y: 0,
1795+
width: 200,
1796+
};
1797+
}
1798+
},
17741799
});
17751800
const { container } = render(
17761801
<DayRangePicker

0 commit comments

Comments
 (0)