Skip to content

Commit 99ca2c9

Browse files
cursoragentpulpdrew
andcommitted
test(app): assert reference-area shading by rendering, not props
Replace the prop-only getAlertReferenceLines tests with ones that render the areas in a fixed-size chart and check which side of the threshold line gets shaded. The prop-only checks restated the implementation and would survive a future Recharts default-edge flip; they also tripped the app lint warning cap via unsafe type assertions. Co-authored-by: Drew Davis <pulpdrew@gmail.com>
1 parent 2e2ade8 commit 99ca2c9

3 files changed

Lines changed: 102 additions & 76 deletions

File tree

‎packages/app/src/components/Alerts.tsx‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,6 @@ export const getAlertReferenceLines = ({
191191
thresholdType === AlertThresholdType.NOT_BETWEEN &&
192192
thresholdMax != null
193193
) {
194-
// Recharts v3 flipped the default edge for an open-ended ReferenceArea:
195-
// on the Y axis a missing y1 now snaps to the top (highest value) and a
196-
// missing y2 to the bottom (lowest value) - the reverse of v2. So the
197-
// open end must be the prop we omit: omit y2 to fill down from `threshold`
198-
// (values below the lower bound) and omit y1 to fill up from `thresholdMax`
199-
// (values above the upper bound).
200194
return [
201195
<ReferenceArea
202196
key="not-between-lower"
@@ -235,10 +229,6 @@ export const getAlertReferenceLines = ({
235229
thresholdType === AlertThresholdType.ABOVE ||
236230
thresholdType === AlertThresholdType.ABOVE_EXCLUSIVE
237231
) {
238-
// Omit y1 so the area fills up from `threshold` to the top edge. In
239-
// Recharts v3 a missing y1 snaps to the top of the Y axis (highest value);
240-
// setting y1={threshold} instead would fill downward and wrongly highlight
241-
// the region below the threshold.
242232
return (
243233
<ReferenceArea
244234
y2={threshold}

‎packages/app/src/components/__tests__/Alerts.test.ts‎

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import { isValidElement, ReactElement } from 'react';
21
import { AlertThresholdType } from '@hyperdx/common-utils/dist/types';
32

4-
import {
5-
getAlertReferenceLines,
6-
getAlertReferenceLineValues,
7-
} from '@/components/Alerts';
3+
import { getAlertReferenceLineValues } from '@/components/Alerts';
84

95
describe('getAlertReferenceLineValues', () => {
106
it('returns only threshold for a simple (non-range) alert type', () => {
@@ -82,64 +78,3 @@ describe('getAlertReferenceLineValues', () => {
8278
).toEqual([0, 550]);
8379
});
8480
});
85-
86-
describe('getAlertReferenceLines', () => {
87-
// Recharts v3 fills an open-ended ReferenceArea from the set bound to the
88-
// opposite edge: a set y2 (with y1 omitted) fills up to the top, and a set
89-
// y1 (with y2 omitted) fills down to the bottom. These tests pin which bound
90-
// each threshold type sets so the red firing region stays on the correct
91-
// side of the threshold.
92-
it.each([AlertThresholdType.ABOVE, AlertThresholdType.ABOVE_EXCLUSIVE])(
93-
'fills up from the threshold for %s',
94-
thresholdType => {
95-
const area = getAlertReferenceLines({
96-
threshold: 50,
97-
thresholdMax: undefined,
98-
thresholdType,
99-
}) as ReactElement<{ y1?: number; y2?: number }>;
100-
expect(isValidElement(area)).toBe(true);
101-
expect(area.props.y2).toBe(50);
102-
expect(area.props.y1).toBeUndefined();
103-
},
104-
);
105-
106-
it.each([AlertThresholdType.BELOW, AlertThresholdType.BELOW_OR_EQUAL])(
107-
'fills from 0 up to the threshold for %s',
108-
thresholdType => {
109-
const area = getAlertReferenceLines({
110-
threshold: 50,
111-
thresholdMax: undefined,
112-
thresholdType,
113-
}) as ReactElement<{ y1?: number; y2?: number }>;
114-
expect(area.props.y1).toBe(0);
115-
expect(area.props.y2).toBe(50);
116-
},
117-
);
118-
119-
it('fills the band between the bounds for BETWEEN', () => {
120-
const area = getAlertReferenceLines({
121-
threshold: 10,
122-
thresholdMax: 20,
123-
thresholdType: AlertThresholdType.BETWEEN,
124-
}) as ReactElement<{ y1?: number; y2?: number }>;
125-
expect(area.props.y1).toBe(10);
126-
expect(area.props.y2).toBe(20);
127-
});
128-
129-
it('fills below the lower bound and above the upper bound for NOT_BETWEEN', () => {
130-
const areas = getAlertReferenceLines({
131-
threshold: 10,
132-
thresholdMax: 20,
133-
thresholdType: AlertThresholdType.NOT_BETWEEN,
134-
}) as ReactElement<{ y1?: number; y2?: number }>[];
135-
expect(Array.isArray(areas)).toBe(true);
136-
137-
const [lower, upper] = areas;
138-
// Lower region: fills down from the lower bound (y2 omitted -> bottom).
139-
expect(lower.props.y1).toBe(10);
140-
expect(lower.props.y2).toBeUndefined();
141-
// Upper region: fills up from the upper bound (y1 omitted -> top).
142-
expect(upper.props.y2).toBe(20);
143-
expect(upper.props.y1).toBeUndefined();
144-
});
145-
});
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React from 'react';
2+
import { Line, LineChart, ReferenceLine, XAxis, YAxis } from 'recharts';
3+
import { AlertThresholdType } from '@hyperdx/common-utils/dist/types';
4+
import { render } from '@testing-library/react';
5+
6+
import { getAlertReferenceLines } from '@/components/Alerts';
7+
8+
type Rect = { top: number; bottom: number };
9+
10+
function renderAreas(
11+
thresholdType: AlertThresholdType,
12+
threshold: number,
13+
thresholdMax?: number,
14+
): { rects: Rect[]; thresholdY: number; thresholdMaxY?: number } {
15+
const { container } = render(
16+
<LineChart
17+
width={400}
18+
height={300}
19+
data={[
20+
{ t: 0, v: 10 },
21+
{ t: 1, v: 80 },
22+
]}
23+
>
24+
<XAxis dataKey="t" />
25+
<YAxis domain={[0, 100]} />
26+
<Line dataKey="v" isAnimationActive={false} />
27+
{getAlertReferenceLines({ threshold, thresholdMax, thresholdType })}
28+
<ReferenceLine y={threshold} />
29+
{thresholdMax != null ? <ReferenceLine y={thresholdMax} /> : null}
30+
</LineChart>,
31+
);
32+
33+
const rects = Array.from(
34+
container.querySelectorAll('.recharts-reference-area-rect'),
35+
).map(r => {
36+
const top = Number(r.getAttribute('y'));
37+
return { top, bottom: top + Number(r.getAttribute('height')) };
38+
});
39+
const lineYs = Array.from(
40+
container.querySelectorAll('.recharts-reference-line-line'),
41+
).map(l => Number(l.getAttribute('y1')));
42+
43+
return {
44+
rects,
45+
thresholdY: lineYs[0],
46+
thresholdMaxY: lineYs[1],
47+
};
48+
}
49+
50+
const EPS = 1;
51+
52+
describe('getAlertReferenceLines shading', () => {
53+
it.each([AlertThresholdType.ABOVE, AlertThresholdType.ABOVE_EXCLUSIVE])(
54+
'shades above the threshold line for %s',
55+
thresholdType => {
56+
const { rects, thresholdY } = renderAreas(thresholdType, 50);
57+
expect(rects).toHaveLength(1);
58+
expect(rects[0].top).toBeLessThan(thresholdY);
59+
expect(rects[0].bottom).toBeLessThanOrEqual(thresholdY + EPS);
60+
},
61+
);
62+
63+
it.each([AlertThresholdType.BELOW, AlertThresholdType.BELOW_OR_EQUAL])(
64+
'shades below the threshold line for %s',
65+
thresholdType => {
66+
const { rects, thresholdY } = renderAreas(thresholdType, 50);
67+
expect(rects).toHaveLength(1);
68+
expect(rects[0].top).toBeGreaterThanOrEqual(thresholdY - EPS);
69+
expect(rects[0].bottom).toBeGreaterThan(thresholdY);
70+
},
71+
);
72+
73+
it('shades the band between the two thresholds for BETWEEN', () => {
74+
const { rects, thresholdY, thresholdMaxY } = renderAreas(
75+
AlertThresholdType.BETWEEN,
76+
30,
77+
70,
78+
);
79+
expect(rects).toHaveLength(1);
80+
expect(thresholdMaxY).toBeDefined();
81+
expect(rects[0].top).toBeCloseTo(thresholdMaxY!, 0);
82+
expect(rects[0].bottom).toBeCloseTo(thresholdY, 0);
83+
});
84+
85+
it('shades below the lower bound and above the upper bound for NOT_BETWEEN', () => {
86+
const { rects, thresholdY, thresholdMaxY } = renderAreas(
87+
AlertThresholdType.NOT_BETWEEN,
88+
30,
89+
70,
90+
);
91+
expect(rects).toHaveLength(2);
92+
expect(thresholdMaxY).toBeDefined();
93+
94+
const lower = rects.find(r => r.bottom > thresholdY)!;
95+
const upper = rects.find(r => r.top < thresholdMaxY!)!;
96+
// Lower firing region: from the lower threshold line downward.
97+
expect(lower.top).toBeCloseTo(thresholdY, 0);
98+
// Upper firing region: from the upper threshold line upward.
99+
expect(upper.bottom).toBeCloseTo(thresholdMaxY!, 0);
100+
});
101+
});

0 commit comments

Comments
 (0)