|
| 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