Skip to content

Commit a943c56

Browse files
authored
chore: Mock getBoundingClientRect in mixed chart tests (#3894)
1 parent add1bdd commit a943c56

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

pages/container-queries.page.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ function MeasureReporter(props: { id?: string; style?: React.CSSProperties; type
2121
[props.type]
2222
);
2323
if (value === null) {
24-
return <div ref={ref}>Loading...</div>;
24+
return (
25+
<div ref={ref} style={{ ...boxStyles, ...props.style }}>
26+
Loading...
27+
</div>
28+
);
2529
}
2630
return (
2731
<div id={props.id} ref={ref} style={{ ...boxStyles, ...props.style }}>
@@ -72,8 +76,15 @@ export default function ColumnLayoutPage() {
7276
type={measureType}
7377
/>
7478

79+
<h2>Reports content-box dimensions when border is present</h2>
80+
<MeasureReporter
81+
id="test-border"
82+
style={{ inlineSize: 300, blockSize: 50, border: '2px solid blue' }}
83+
type={measureType}
84+
/>
85+
7586
<h2>Adjusts as the element changes size (resize browser)</h2>
76-
<MeasureReporter id="test-updates" style={{ blockSize: 50 }} type={measureType} />
87+
<MeasureReporter id="test-updates" style={{ blockSize: 50, border: '2px solid blue' }} type={measureType} />
7788

7889
<h2>Returns correct breakpoints</h2>
7990
<BreakpointReporter id="test-breakpoints" style={{ blockSize: 50 }} />

src/mixed-line-bar-chart/__tests__/mixed-chart.test.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ jest.mock('../../../lib/components/popover/utils/positions', () => {
2424
};
2525
});
2626

27+
jest.mock('@cloudscape-design/component-toolkit', () => ({
28+
...jest.requireActual('@cloudscape-design/component-toolkit'),
29+
// Mock the chart width with enough space to fit all expected elements (labels, x ticks, etc)
30+
useContainerQuery: () => [900, null],
31+
}));
32+
2733
jest.mock('@cloudscape-design/component-toolkit/internal', () => ({
2834
...jest.requireActual('@cloudscape-design/component-toolkit/internal'),
2935
getIsRtl: jest.fn().mockReturnValue(false),
@@ -113,7 +119,7 @@ const thresholdSeries: MixedLineBarChartProps.ThresholdSeries = {
113119
// Transformation to fallback colors for browsers that don't support them are covered by the `parseCssVariable` utility.
114120
const originalCSS = window.CSS;
115121

116-
let originalGetComputedStyle: Window['getComputedStyle'];
122+
const originalGetComputedStyle = window.getComputedStyle;
117123
const fakeGetComputedStyle: Window['getComputedStyle'] = (...args) => {
118124
const result = originalGetComputedStyle(...args);
119125
result.borderWidth = '2px'; // Approximate mock value for the popover body' border width
@@ -124,9 +130,7 @@ const fakeGetComputedStyle: Window['getComputedStyle'] = (...args) => {
124130

125131
beforeEach(() => {
126132
window.CSS.supports = () => true;
127-
originalGetComputedStyle = window.getComputedStyle;
128133
window.getComputedStyle = fakeGetComputedStyle;
129-
130134
jest.resetAllMocks();
131135
});
132136
afterEach(() => {

0 commit comments

Comments
 (0)