Skip to content

Commit 8a00626

Browse files
test: cover always-on distribution mode and color legend on Event Deltas (#2303)
## Summary The existing `deltaChart*.test.ts` files cover utility functions (`applyTopNAggregation`, `getPropertyStatistics`, filter-key mapping, scoring, sampling), but nothing renders `DBDeltaChart` or `DBSearchHeatmapChart`. That left two behaviours of the always-on distribution feature (#1899) without regression coverage: 1. Distribution mode renders without a heatmap selection. The parent `DBSearchHeatmapChart` must render `<DBDeltaChart>` unconditionally; a four-null conditional silently turns the feature off. 2. The heatmap color legend is forwarded to `DBDeltaChart` via `legendPrefix` and rendered at the start of the legend Flex. Without rendering tests, either of these can regress silently while CI stays green. This PR adds two test files so the rendering contract is covered going forward: - `packages/app/src/components/__tests__/DBDeltaChart.test.tsx` (9 tests): distribution mode labels, selection mode labels, query gating (`enabled` flags for outlier / inlier / allSpans), the no-filters-override regression guard, and `legendPrefix` rendering. - `packages/app/src/components/Search/__tests__/DBSearchHeatmapChart.test.tsx` (3 tests): asserts `<DBDeltaChart>` renders unconditionally (without URL selection coords), that URL coords are forwarded when set, and that the parent always passes a `legendPrefix`. All 12 pass on origin/main without any source change. ## Test plan - [x] `yarn jest src/components/__tests__/DBDeltaChart.test.tsx src/components/Search/__tests__/DBSearchHeatmapChart.test.tsx`: 12 / 12 pass on origin/main - [x] Existing 83 `deltaChart*.test.ts` utility tests still pass - [x] prose-lint clean - [x] Prettier clean - [x] No source files changed; tests only ## Out of scope - The DBDeltaChart / DBSearchHeatmapChart source. No production code changes here.
1 parent fbe5a9a commit 8a00626

2 files changed

Lines changed: 406 additions & 0 deletions

File tree

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import React from 'react';
2+
import {
3+
BuilderChartConfigWithDateRange,
4+
SourceKind,
5+
TTraceSource,
6+
} from '@hyperdx/common-utils/dist/types';
7+
import { screen } from '@testing-library/react';
8+
9+
import type DBDeltaChartType from '../../DBDeltaChart';
10+
import { DBSearchHeatmapChart } from '../DBSearchHeatmapChart';
11+
12+
type FieldsState = {
13+
value: string;
14+
count: string;
15+
scaleType: string;
16+
xMin: number | null;
17+
xMax: number | null;
18+
yMin: number | null;
19+
yMax: number | null;
20+
};
21+
22+
function freshFieldsState(): FieldsState {
23+
return {
24+
value: 'Duration',
25+
count: 'count()',
26+
scaleType: 'log',
27+
xMin: null,
28+
xMax: null,
29+
yMin: null,
30+
yMax: null,
31+
};
32+
}
33+
34+
// Controls the values returned by useQueryStates each render.
35+
let mockFieldsState: FieldsState = freshFieldsState();
36+
37+
jest.mock('nuqs', () => ({
38+
parseAsFloat: { withDefault: jest.fn(() => 'parseAsFloat') },
39+
parseAsString: { withDefault: jest.fn(() => 'parseAsString') },
40+
useQueryStates: jest.fn(() => [mockFieldsState, jest.fn()]),
41+
}));
42+
43+
jest.mock('@hyperdx/common-utils/dist/core/metadata', () => ({
44+
tcFromSource: jest.fn(() => ({})),
45+
}));
46+
47+
jest.mock('@/source', () => ({
48+
getDurationMsExpression: jest.fn(() => 'Duration'),
49+
}));
50+
51+
jest.mock('@/components/HeatmapSettingsDrawer', () =>
52+
jest.fn(() => <div data-testid="heatmap-settings-drawer" />),
53+
);
54+
55+
jest.mock('../../DBHeatmapChart', () => ({
56+
__esModule: true,
57+
default: jest.fn((props: { enabled?: boolean }) => (
58+
<div
59+
data-testid="db-heatmap-chart"
60+
data-enabled={String(!!props.enabled)}
61+
/>
62+
)),
63+
ColorLegend: jest.fn(() => <div data-testid="color-legend">color scale</div>),
64+
darkPalette: ['#000'],
65+
lightPalette: ['#fff'],
66+
toHeatmapChartConfig: jest.fn(() => ({ heatmapConfig: {} })),
67+
}));
68+
69+
// Capture props passed to DBDeltaChart so we can assert URL coords forward.
70+
type DBDeltaChartProps = React.ComponentProps<typeof DBDeltaChartType>;
71+
const deltaChartPropsLog: DBDeltaChartProps[] = [];
72+
jest.mock('../../DBDeltaChart', () => ({
73+
__esModule: true,
74+
default: jest.fn((props: DBDeltaChartProps) => {
75+
deltaChartPropsLog.push(props);
76+
return <div data-testid="db-delta-chart" />;
77+
}),
78+
}));
79+
80+
// Re-import the mocked ColorLegend so we can assert that it's specifically
81+
// the heatmap color scale being passed as legendPrefix (not any truthy node).
82+
const { ColorLegend: MockedColorLegend } = jest.requireMock(
83+
'../../DBHeatmapChart',
84+
);
85+
86+
// Direct type annotations (no `as unknown as` casts) so adding a
87+
// required field to either schema fails this fixture at compile
88+
// time rather than silently passing.
89+
const baseSource: TTraceSource = {
90+
id: 'trace-source',
91+
kind: SourceKind.Trace,
92+
name: 'Trace Source',
93+
connection: 'conn',
94+
from: { databaseName: 'otel', tableName: 'otel_traces' },
95+
timestampValueExpression: 'Timestamp',
96+
defaultTableSelectExpression: 'Timestamp',
97+
durationExpression: 'Duration',
98+
durationPrecision: 3,
99+
traceIdExpression: 'TraceId',
100+
spanIdExpression: 'SpanId',
101+
parentSpanIdExpression: 'ParentSpanId',
102+
spanNameExpression: 'SpanName',
103+
spanKindExpression: 'SpanKind',
104+
};
105+
106+
const baseChartConfig: BuilderChartConfigWithDateRange = {
107+
dateRange: [new Date(0), new Date(1000)],
108+
from: { databaseName: 'otel', tableName: 'otel_traces' },
109+
timestampValueExpression: 'Timestamp',
110+
connection: 'conn',
111+
select: '',
112+
where: '',
113+
whereLanguage: 'sql',
114+
};
115+
116+
describe('DBSearchHeatmapChart', () => {
117+
beforeEach(() => {
118+
jest.clearAllMocks();
119+
deltaChartPropsLog.length = 0;
120+
mockFieldsState = freshFieldsState();
121+
});
122+
123+
it('renders DBDeltaChart unconditionally when no heatmap selection exists', () => {
124+
// Regression test for #1899: the attribute distribution chart must
125+
// render before any heatmap selection. A conditional wrapper here
126+
// gates the always-on distribution mode and ships the feature off.
127+
renderWithMantine(
128+
<DBSearchHeatmapChart
129+
chartConfig={baseChartConfig}
130+
source={baseSource}
131+
isReady={true}
132+
/>,
133+
);
134+
135+
expect(screen.getByTestId('db-delta-chart')).toBeInTheDocument();
136+
expect(deltaChartPropsLog.length).toBeGreaterThan(0);
137+
expect(deltaChartPropsLog[deltaChartPropsLog.length - 1]).toMatchObject({
138+
xMin: null,
139+
xMax: null,
140+
yMin: null,
141+
yMax: null,
142+
valueExpr: 'Duration',
143+
});
144+
});
145+
146+
it('forwards URL selection coordinates to DBDeltaChart', () => {
147+
mockFieldsState = {
148+
...mockFieldsState,
149+
xMin: 10,
150+
xMax: 20,
151+
yMin: 30,
152+
yMax: 40,
153+
};
154+
155+
renderWithMantine(
156+
<DBSearchHeatmapChart
157+
chartConfig={baseChartConfig}
158+
source={baseSource}
159+
isReady={true}
160+
/>,
161+
);
162+
163+
expect(screen.getByTestId('db-delta-chart')).toBeInTheDocument();
164+
expect(deltaChartPropsLog[deltaChartPropsLog.length - 1]).toMatchObject({
165+
xMin: 10,
166+
xMax: 20,
167+
yMin: 30,
168+
yMax: 40,
169+
});
170+
});
171+
172+
it('passes a ColorLegend as legendPrefix to DBDeltaChart', () => {
173+
// Regression test: the parent must keep passing a ColorLegend as
174+
// legendPrefix so the heatmap color scale appears above the
175+
// distribution charts. DBDeltaChart renders this prop in its
176+
// legend Flex. Truthiness alone would not catch a regression that
177+
// swaps ColorLegend for any other element; pin the element type.
178+
renderWithMantine(
179+
<DBSearchHeatmapChart
180+
chartConfig={baseChartConfig}
181+
source={baseSource}
182+
isReady={true}
183+
/>,
184+
);
185+
186+
const lastProps = deltaChartPropsLog[deltaChartPropsLog.length - 1];
187+
expect(lastProps.legendPrefix).toBeTruthy();
188+
expect(React.isValidElement(lastProps.legendPrefix)).toBe(true);
189+
expect((lastProps.legendPrefix as React.ReactElement).type).toBe(
190+
MockedColorLegend,
191+
);
192+
});
193+
});

0 commit comments

Comments
 (0)