@sybilion/uilib 1.7.2 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist/agent-glossary/content-prompt.generated.ts +2 -0
  2. package/dist/agent-glossary/content.generated.ts +2 -0
  3. package/dist/agent-glossary/content.md +2 -0
  4. package/dist/agent-glossary/content.prompt.md +2 -0
  5. package/dist/agent-glossary/workspace.generated.ts +1 -0
  6. package/dist/agent-glossary/workspace.index.md +1 -0
  7. package/dist/agent-glossary/workspace.md +1 -0
  8. package/dist/esm/src/components/ui/AnalysesSelector/AnalysesSelector.js +37 -20
  9. package/dist/esm/src/components/ui/Chart/Chart.js +4 -1
  10. package/dist/esm/src/components/ui/Chart/Chart.styl.js +2 -2
  11. package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.js +74 -57
  12. package/dist/esm/src/components/ui/Chart/components/ChartAxes.js +8 -7
  13. package/dist/esm/src/components/ui/Chart/components/ChartContainer.js +2 -2
  14. package/dist/esm/src/components/ui/Chart/components/ChartGrid.js +2 -2
  15. package/dist/esm/src/components/ui/Chart/components/ChartLegendContent.js +2 -2
  16. package/dist/esm/src/components/ui/Chart/components/ChartLegendItem.js +2 -2
  17. package/dist/esm/src/components/ui/Chart/components/ChartPlacedTooltip.js +65 -0
  18. package/dist/esm/src/components/ui/Chart/components/ChartTooltipContent.js +4 -4
  19. package/dist/esm/src/components/ui/Chart/components/ChartTooltipItem.js +24 -8
  20. package/dist/esm/src/components/ui/Chart/components/CustomChartLegend/CustomChartLegend.js +8 -3
  21. package/dist/esm/src/components/ui/Chart/components/LegendSvg/LegendSvg.js +1 -1
  22. package/dist/esm/src/components/ui/Chart/components/QuantileBands.js +1 -1
  23. package/dist/esm/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.helpers.js +17 -6
  24. package/dist/esm/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.js +40 -10
  25. package/dist/esm/src/components/ui/ChartAreaInteractive/ChartLines.js +55 -19
  26. package/dist/esm/src/components/ui/ChartAreaInteractive/chartHistories.js +44 -0
  27. package/dist/esm/src/components/ui/ChartAreaInteractive/overlays/useChartYRange.js +32 -7
  28. package/dist/esm/src/components/ui/Page/PageTabs/PageTabs.styl.js +2 -2
  29. package/dist/esm/src/components/ui/Page/PageXScroll/PageXScroll.js +50 -15
  30. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.js +212 -0
  31. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.js +123 -0
  32. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.styl.js +7 -0
  33. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.js +28 -0
  34. package/dist/esm/src/components/widgets/DriverCard/DriverPerformanceChart.js +5 -25
  35. package/dist/esm/src/index.js +4 -1
  36. package/dist/esm/src/utils/chartConnectionPoint.js +31 -16
  37. package/dist/esm/types/src/components/ui/AnalysesSelector/AnalysesSelector.d.ts +1 -1
  38. package/dist/esm/types/src/components/ui/AnalysesSelector/AnalysesSelector.types.d.ts +12 -0
  39. package/dist/esm/types/src/components/ui/AnalysesSelector/index.d.ts +1 -1
  40. package/dist/esm/types/src/components/ui/Chart/Chart.d.ts +4 -2
  41. package/dist/esm/types/src/components/ui/Chart/Chart.types.d.ts +5 -0
  42. package/dist/esm/types/src/components/ui/Chart/chartForecastVisualization.types.d.ts +7 -0
  43. package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.d.ts +25 -0
  44. package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.test.d.ts +1 -0
  45. package/dist/esm/types/src/components/ui/Chart/components/ChartAxes.d.ts +8 -1
  46. package/dist/esm/types/src/components/ui/Chart/components/ChartAxes.test.d.ts +1 -0
  47. package/dist/esm/types/src/components/ui/Chart/components/ChartPlacedTooltip.d.ts +31 -0
  48. package/dist/esm/types/src/components/ui/Chart/components/ChartPlacedTooltip.test.d.ts +1 -0
  49. package/dist/esm/types/src/components/ui/Chart/components/ChartTooltipContent.d.ts +1 -1
  50. package/dist/esm/types/src/components/ui/Chart/components/ChartTooltipContent.test.d.ts +1 -0
  51. package/dist/esm/types/src/components/ui/Chart/components/ChartTooltipItem.d.ts +2 -1
  52. package/dist/esm/types/src/components/ui/Chart/components/CustomChartLegend/CustomChartLegend.d.ts +8 -1
  53. package/dist/esm/types/src/components/ui/Chart/components/LegendSvg/LegendSvg.d.ts +2 -0
  54. package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.d.ts +1 -1
  55. package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.helpers.d.ts +5 -0
  56. package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartLines.d.ts +30 -1
  57. package/dist/esm/types/src/components/ui/ChartAreaInteractive/chartHistories.d.ts +40 -0
  58. package/dist/esm/types/src/components/ui/ChartAreaInteractive/index.d.ts +1 -0
  59. package/dist/esm/types/src/components/ui/ChartAreaInteractive/overlays/useChartYRange.d.ts +6 -1
  60. package/dist/esm/types/src/components/ui/Page/PageXScroll/PageXScroll.d.ts +1 -1
  61. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.d.ts +3 -0
  62. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.d.ts +65 -0
  63. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.d.ts +19 -0
  64. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.types.d.ts +85 -0
  65. package/dist/esm/types/src/components/ui/StackedAreaChart/index.d.ts +2 -0
  66. package/dist/esm/types/src/docs/pages/StackedAreaChartPage.d.ts +1 -0
  67. package/dist/esm/types/src/index.d.ts +1 -0
  68. package/dist/esm/types/src/utils/chartConnectionPoint.d.ts +11 -3
  69. package/dist/esm/types/tests/chartHistories.test.d.ts +1 -0
  70. package/dist/esm/types/tests/pageXScroll.test.d.ts +1 -0
  71. package/dist/esm/types/tests/stackedAreaChart.test.d.ts +1 -0
  72. package/package.json +2 -2
  73. package/src/components/ui/AnalysesSelector/AnalysesSelector.tsx +50 -22
  74. package/src/components/ui/AnalysesSelector/AnalysesSelector.types.ts +13 -0
  75. package/src/components/ui/AnalysesSelector/index.ts +1 -0
  76. package/src/components/ui/Chart/AGENT.md +5 -0
  77. package/src/components/ui/Chart/Chart.tsx +7 -1
  78. package/src/components/ui/Chart/Chart.types.ts +5 -0
  79. package/src/components/ui/Chart/chartForecastVisualization.types.ts +7 -0
  80. package/src/components/ui/Chart/components/BaseChartWrapper.test.tsx +344 -0
  81. package/src/components/ui/Chart/components/BaseChartWrapper.tsx +149 -88
  82. package/src/components/ui/Chart/components/ChartAxes.test.tsx +49 -0
  83. package/src/components/ui/Chart/components/ChartAxes.tsx +36 -20
  84. package/src/components/ui/Chart/components/ChartPlacedTooltip.test.tsx +160 -0
  85. package/src/components/ui/Chart/components/ChartPlacedTooltip.tsx +93 -0
  86. package/src/components/ui/Chart/components/ChartTooltipContent.test.tsx +85 -0
  87. package/src/components/ui/Chart/components/ChartTooltipContent.tsx +2 -0
  88. package/src/components/ui/Chart/components/ChartTooltipItem.tsx +41 -10
  89. package/src/components/ui/Chart/components/CustomChartLegend/CustomChartLegend.tsx +21 -1
  90. package/src/components/ui/Chart/components/LegendSvg/LegendSvg.tsx +3 -1
  91. package/src/components/ui/Chart/components/QuantileBands.tsx +1 -1
  92. package/src/components/ui/ChartAreaInteractive/AGENT.md +4 -0
  93. package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.helpers.ts +27 -5
  94. package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.tsx +64 -10
  95. package/src/components/ui/ChartAreaInteractive/ChartLines.tsx +151 -65
  96. package/src/components/ui/ChartAreaInteractive/chartHistories.ts +63 -0
  97. package/src/components/ui/ChartAreaInteractive/index.ts +1 -0
  98. package/src/components/ui/ChartAreaInteractive/overlays/useChartYRange.ts +52 -6
  99. package/src/components/ui/Page/AGENT.md +2 -0
  100. package/src/components/ui/Page/PageTabs/PageTabs.styl +8 -0
  101. package/src/components/ui/Page/PageXScroll/PageXScroll.tsx +57 -13
  102. package/src/components/ui/StackedAreaChart/AGENT.md +26 -0
  103. package/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.ts +268 -0
  104. package/src/components/ui/StackedAreaChart/StackedAreaChart.styl +32 -0
  105. package/src/components/ui/StackedAreaChart/StackedAreaChart.styl.d.ts +13 -0
  106. package/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.tsx +81 -0
  107. package/src/components/ui/StackedAreaChart/StackedAreaChart.tsx +416 -0
  108. package/src/components/ui/StackedAreaChart/StackedAreaChart.types.ts +97 -0
  109. package/src/components/ui/StackedAreaChart/index.ts +2 -0
  110. package/src/components/widgets/DriverCard/DriverPerformanceChart.tsx +2 -2
  111. package/src/docs/pages/ChartAreaInteractivePage.tsx +67 -0
  112. package/src/docs/pages/StackedAreaChartPage.tsx +141 -0
  113. package/src/docs/registry.ts +6 -0
  114. package/src/index.ts +1 -0
  115. package/src/utils/chartConnectionPoint.ts +41 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sybilion/uilib",
3
- "version": "1.7.2",
3
+ "version": "1.8.1",
4
4
  "description": "Sybilion Design System — React UI components (Vite + Stylus)",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -137,7 +137,7 @@
137
137
  "lightweight-charts": "^5.0.9",
138
138
  "lucide-react": "^0.546.0",
139
139
  "motion": "^12.23.12",
140
- "recharts": "^3.2.1",
140
+ "recharts": "^3.7.0",
141
141
  "style-inject": "^0.3.0",
142
142
  "topojson-client": "^3.1.0",
143
143
  "vaul": "^1.1.2",
@@ -32,7 +32,20 @@ import { SelectInlineRenamer } from './SelectInlineRenamer';
32
32
  const isFailed = (item: AnalysesSelectorItem) =>
33
33
  item.status?.toLowerCase() === 'fail';
34
34
 
35
- const isDashedIcon = (item: AnalysesSelectorItem) => item.id !== -1;
35
+ /**
36
+ * An entry listed in the selector. History entries (the single "Historical",
37
+ * id -1, or one per `historicalItems` line, which carries its `seriesKey`) are
38
+ * shown only: like "Historical", they do not toggle or select.
39
+ */
40
+ type ListedItem = AnalysesSelectorItem & {
41
+ dimmed?: boolean;
42
+ seriesKey?: string;
43
+ };
44
+
45
+ const isHistoryItem = (item: ListedItem) =>
46
+ item.id === -1 || item.seriesKey !== undefined;
47
+
48
+ const isDashedIcon = (item: AnalysesSelectorItem) => !isHistoryItem(item);
36
49
 
37
50
  const defaultIsAnalysisInProgress = (status?: string) =>
38
51
  ['pending', 'running', 'cancelling'].includes(status?.toLowerCase() ?? '');
@@ -106,6 +119,7 @@ export function AnalysesSelector({
106
119
  triggerProps,
107
120
  onFailedItemClick,
108
121
  showHistorical = true,
122
+ historicalItems,
109
123
  enableToggleHidden = false,
110
124
  maxVisibleItems = 1,
111
125
  preventDeselection = false,
@@ -138,17 +152,35 @@ export function AnalysesSelector({
138
152
  );
139
153
  }, [selectedIds]);
140
154
 
155
+ // The history entries: one per `historicalItems` line, or the single "Historical".
156
+ const historyEntries = useMemo(() => {
157
+ if (!showHistorical) return [];
158
+ if (historicalItems?.length) {
159
+ return historicalItems.map(
160
+ (h, index): ListedItem => ({
161
+ // Negative ids keep them apart from analyses in keys; the history
162
+ // is recognised by its seriesKey.
163
+ id: -(index + 1),
164
+ label: h.label,
165
+ color: h.color,
166
+ seriesKey: h.seriesKey,
167
+ dimmed: hiddenSeries.has(h.seriesKey),
168
+ }),
169
+ );
170
+ }
171
+ return [
172
+ {
173
+ id: -1,
174
+ label: 'Historical',
175
+ dimmed: hiddenSeries.has('historical'),
176
+ },
177
+ ];
178
+ }, [showHistorical, historicalItems, hiddenSeries]);
179
+
141
180
  // In inlineMode, show Historical + selected analyses (limited by maxVisibleItems)
142
181
  const selectedItems = useMemo(() => {
143
182
  if (inlineMode) {
144
- const list = [];
145
- if (showHistorical) {
146
- list.push({
147
- id: -1,
148
- label: 'Historical',
149
- dimmed: hiddenSeries.has('historical'),
150
- });
151
- }
183
+ const list: ListedItem[] = [...historyEntries];
152
184
  // Filter by selectedIds, exclude failed items and user series
153
185
  const analysesOnly = items.filter(
154
186
  item =>
@@ -167,18 +199,12 @@ export function AnalysesSelector({
167
199
  return list.concat(limitedItems);
168
200
  }
169
201
  // Non-inlineMode: keep original behavior
170
- const list = [];
171
- if (showHistorical) {
172
- list.push({
173
- id: -1,
174
- label: 'Historical',
175
- });
176
- }
202
+ const list: ListedItem[] = [...historyEntries];
177
203
  return list.concat(items.filter(item => selectedSet.has(item.id)));
178
204
  }, [
179
205
  items,
180
206
  selectedSet,
181
- showHistorical,
207
+ historyEntries,
182
208
  inlineMode,
183
209
  hiddenSeries,
184
210
  maxVisibleItems,
@@ -262,7 +288,8 @@ export function AnalysesSelector({
262
288
  handleItemClick(item);
263
289
  };
264
290
 
265
- const handleItemClick = (item: AnalysesSelectorItem) => {
291
+ const handleItemClick = (item: ListedItem) => {
292
+ if (item.seriesKey !== undefined) return;
266
293
  if (isFailed(item) && onFailedItemClick) {
267
294
  onFailedItemClick(item.id);
268
295
  } else if (preventDeselection || (inlineMode && enableToggleHidden)) {
@@ -288,9 +315,10 @@ export function AnalysesSelector({
288
315
  );
289
316
  };
290
317
 
291
- const renderItemLabel = (item: AnalysesSelectorItem) => (
318
+ const renderItemLabel = (item: ListedItem) => (
292
319
  <LabelWithId
293
- id={item.id}
320
+ // -1 shows no id badge: history lines have no analysis id.
321
+ id={isHistoryItem(item) ? -1 : item.id}
294
322
  label={
295
323
  isAnalysisInProgressProp(item.status) ? (
296
324
  <TextShimmer as="span">{item.label}</TextShimmer>
@@ -323,11 +351,11 @@ export function AnalysesSelector({
323
351
  >
324
352
  {selectedItems.map(item => (
325
353
  <div
326
- key={item.id}
354
+ key={item.seriesKey ?? item.id}
327
355
  className={cn(
328
356
  S.item,
329
357
  disabled && S.disabled,
330
- item.id === -1 && S.historical,
358
+ isHistoryItem(item) && S.historical,
331
359
  item.dimmed && S.dimmed,
332
360
  )}
333
361
  onClick={() => !disabled && handleItemClick(item)}
@@ -10,6 +10,12 @@ export interface AnalysesSelectorItem {
10
10
  updated_at?: string;
11
11
  }
12
12
 
13
+ export interface AnalysesSelectorHistoricalItem {
14
+ seriesKey: string;
15
+ label: string;
16
+ color?: string;
17
+ }
18
+
13
19
  export interface AnalysesSelectorProps {
14
20
  items: AnalysesSelectorItem[];
15
21
  /** Single id, set, or array. Omit or `undefined` = nothing selected (Select falls back to first item for display). */
@@ -27,6 +33,13 @@ export interface AnalysesSelectorProps {
27
33
  onFailedItemClick?: (id: number) => void;
28
34
  /** Show historical item as first in the list */
29
35
  showHistorical?: boolean;
36
+ /**
37
+ * Several history lines (one per product), shown in place of the single
38
+ * "Historical" item, each with its own colour. Like "Historical" they are
39
+ * shown only, not toggled. `seriesKey` is the line's data key (e.g.
40
+ * `historical_<id>`); the entry dims while `hiddenSeries` has it.
41
+ */
42
+ historicalItems?: AnalysesSelectorHistoricalItem[];
30
43
  enableToggleHidden?: boolean;
31
44
  maxVisibleItems?: number;
32
45
  /** When true, prevents deselection and shows maxVisibleItems inline + rest in dropdown */
@@ -1,5 +1,6 @@
1
1
  export { AnalysesSelector } from './AnalysesSelector';
2
2
  export type {
3
+ AnalysesSelectorHistoricalItem,
3
4
  AnalysesSelectorItem,
4
5
  AnalysesSelectorProps,
5
6
  } from './AnalysesSelector.types';
@@ -12,6 +12,11 @@ ChartContainer wraps Recharts charts with the library’s theming, mapping a Cha
12
12
  ## Do
13
13
 
14
14
  - Define every series in the ChartConfig so labels and colours stay consistent across tooltips and legends.
15
+ - With BaseChartWrapper or ChartAreaInteractive, pass `formatTooltipValue` so tooltip numbers match the axis format, and `connectHistoryNulls` / `connectForecastNulls` (or a band's `connectNulls: false`) to choose whether a line or range bridges a month with no value.
16
+ - `historyOnTop` draws the history lines over the forecast lines, for many faint forecasts that would otherwise cover them.
17
+ - `showAxes={false}` draws no axes but keeps a hidden x-axis, so the tooltip still names the row's date; fixed `yMin`/`yMax` (with `autoScaleYAxis={false}`) still set the y range.
18
+ - `showEndDots={false}` drops the dot on each line's last point (for many lines drawn as one bundle).
19
+ - Use ChartTooltip (not Recharts' Tooltip directly) so a chart narrower than 500px (`NARROW_CHART_PX`) opens its tooltip under the chart instead of over the lines, starting right under the x-axis labels (over any legend below). BaseChartWrapper, ChartAreaInteractive, BarChart, DonutChart, StackedAreaChart and the driver card chart already do. A parent that hides overflow (a chat bubble, a card) can clip it there. Pass `position` to place it yourself.
15
20
 
16
21
  ## Example
17
22
 
@@ -6,9 +6,14 @@ import {
6
6
  } from './components/BaseChartWrapper';
7
7
  import { ChartContainer, ChartStyle } from './components/ChartContainer';
8
8
  import { ChartLegendContent } from './components/ChartLegendContent';
9
+ import {
10
+ ChartPlacedTooltip,
11
+ NARROW_CHART_PX,
12
+ } from './components/ChartPlacedTooltip';
9
13
  import { ChartTooltipContent } from './components/ChartTooltipContent';
10
14
 
11
- const ChartTooltip = RechartsPrimitive.Tooltip;
15
+ /** recharts' Tooltip, opening under any chart narrower than {@link NARROW_CHART_PX}. */
16
+ const ChartTooltip = ChartPlacedTooltip;
12
17
  const ChartLegend = RechartsPrimitive.Legend;
13
18
 
14
19
  export * from './Chart.types';
@@ -19,6 +24,7 @@ export {
19
24
  ChartContainer,
20
25
  ChartTooltip,
21
26
  ChartTooltipContent,
27
+ NARROW_CHART_PX,
22
28
  ChartLegend,
23
29
  ChartLegendContent,
24
30
  ChartStyle,
@@ -42,5 +42,10 @@ export type ChartTooltipContentProps = ComponentProps<'div'> & {
42
42
  index: number,
43
43
  payload: unknown,
44
44
  ) => ReactNode;
45
+ /**
46
+ * Formats each row's number (both ends of a range row). Without it the
47
+ * value prints as is.
48
+ */
49
+ valueFormatter?: (value: number) => string;
45
50
  color?: string;
46
51
  };
@@ -96,4 +96,11 @@ export interface QuantileBandConfig {
96
96
  | 'basis'
97
97
  | 'natural';
98
98
  fillPattern?: 'solid' | 'dots' | 'lines';
99
+ /**
100
+ * Fill across months with no range (default true). Read by the Recharts
101
+ * charts' bands (BaseChartWrapper, ChartAreaInteractive's default mode),
102
+ * not by LightweightForecastChart or the intervals and thresholds overlays,
103
+ * which build their own bands.
104
+ */
105
+ connectNulls?: boolean;
99
106
  }
@@ -0,0 +1,344 @@
1
+ import { act, fireEvent, render, waitFor } from '@testing-library/react';
2
+
3
+ import {
4
+ BaseChartWrapper,
5
+ type BaseChartWrapperProps,
6
+ } from './BaseChartWrapper';
7
+ import { NARROW_TOOLTIP_GAP_PX } from './ChartPlacedTooltip';
8
+
9
+ // BaseChartWrapper also moves the tooltip itself, a frame after recharts,
10
+ // keeping it inside the plot. A chart narrower than 500px must keep its
11
+ // tooltip under the chart instead (ChartPlacedTooltip), with no slide. Also:
12
+ // the tooltip's number format, and lines drawn across months with no value.
13
+
14
+ // jsdom lays nothing out, so ResponsiveContainer would measure 0×0 and draw
15
+ // no chart. Give every element the chart's size and report it on resize.
16
+ const H = 120;
17
+ let width = 300;
18
+
19
+ beforeAll(() => {
20
+ for (const [prop, get] of [
21
+ ['clientWidth', () => width],
22
+ ['offsetWidth', () => width],
23
+ ['clientHeight', () => H],
24
+ ['offsetHeight', () => H],
25
+ ] as const) {
26
+ Object.defineProperty(HTMLElement.prototype, prop, {
27
+ configurable: true,
28
+ get,
29
+ });
30
+ }
31
+ HTMLElement.prototype.getBoundingClientRect = () =>
32
+ ({
33
+ x: 0,
34
+ y: 0,
35
+ top: 0,
36
+ left: 0,
37
+ right: width,
38
+ bottom: H,
39
+ width,
40
+ height: H,
41
+ toJSON() {},
42
+ }) as DOMRect;
43
+ (globalThis as Record<string, unknown>).ResizeObserver = class {
44
+ private watched = new Set<Element>();
45
+ constructor(private cb: ResizeObserverCallback) {}
46
+ observe(target: Element) {
47
+ this.watched.add(target);
48
+ setTimeout(() => {
49
+ if (!this.watched.has(target)) return;
50
+ this.cb(
51
+ [{ target, contentRect: { width, height: H } } as never],
52
+ this as never,
53
+ );
54
+ });
55
+ }
56
+ unobserve(target: Element) {
57
+ this.watched.delete(target);
58
+ }
59
+ disconnect() {
60
+ this.watched.clear();
61
+ }
62
+ };
63
+ });
64
+
65
+ const chartData = [
66
+ { date: '2026-01-01', historical: 1 },
67
+ { date: '2026-02-01', historical: 2 },
68
+ { date: '2026-03-01', historical: 3 },
69
+ ];
70
+
71
+ const nextFrame = () =>
72
+ act(() => new Promise<void>(r => requestAnimationFrame(() => r())));
73
+
74
+ async function renderChart(
75
+ w: number,
76
+ props: Partial<BaseChartWrapperProps> = {},
77
+ ) {
78
+ width = w;
79
+ const { container } = render(
80
+ <BaseChartWrapper
81
+ chartData={chartData}
82
+ hasCombinedData
83
+ loading={false}
84
+ isDarkTheme={false}
85
+ height={H}
86
+ showLegend={false}
87
+ margin={{ top: 0, right: 0, bottom: 0, left: 0 }}
88
+ {...props}
89
+ />,
90
+ );
91
+ await waitFor(() =>
92
+ expect(container.querySelector('.recharts-line')).toBeTruthy(),
93
+ );
94
+ return container;
95
+ }
96
+
97
+ async function hoverAndRead(
98
+ w: number,
99
+ props: Partial<BaseChartWrapperProps> = {},
100
+ ) {
101
+ const container = await renderChart(w, props);
102
+ const chart = container.querySelectorAll('.recharts-wrapper');
103
+ await act(async () => {
104
+ fireEvent.mouseMove(chart[chart.length - 1], {
105
+ clientX: w - 20,
106
+ clientY: H / 2,
107
+ });
108
+ });
109
+ const wrapper = () =>
110
+ container.querySelector<HTMLElement>('.recharts-tooltip-wrapper');
111
+ await waitFor(() => expect(wrapper()?.style.transform).toMatch(/translate/));
112
+ // Let the wrapper's own placement run.
113
+ await nextFrame();
114
+ await nextFrame();
115
+ const style = wrapper()!.style;
116
+ const y = Number(
117
+ /translate\([-\d.]+px,\s*([-\d.]+)px\)/.exec(style.transform)?.[1],
118
+ );
119
+ return { y, transition: style.transition, container };
120
+ }
121
+
122
+ describe('BaseChartWrapper tooltip placement', () => {
123
+ it('keeps a narrow chart’s tooltip under the chart', async () => {
124
+ const { y, transition } = await hoverAndRead(300);
125
+ expect(y).toBe(H + NARROW_TOOLTIP_GAP_PX);
126
+ expect(transition).toBe('none');
127
+ });
128
+
129
+ it('keeps a wide chart’s tooltip inside the chart, sliding', async () => {
130
+ const { y, transition } = await hoverAndRead(640);
131
+ expect(y).toBeLessThan(H);
132
+ expect(transition).toMatch(/transform/);
133
+ });
134
+ });
135
+
136
+ describe('BaseChartWrapper tooltip values', () => {
137
+ it('formats them with formatTooltipValue', async () => {
138
+ const { container } = await hoverAndRead(640, {
139
+ formatTooltipValue: v => `${v.toFixed(2)} t`,
140
+ });
141
+ await waitFor(() => expect(container.textContent).toContain('3.00 t'));
142
+ });
143
+ });
144
+
145
+ describe('BaseChartWrapper without axes', () => {
146
+ it('draws no axis but the tooltip still names the row’s date', async () => {
147
+ const { container } = await hoverAndRead(640, {
148
+ showAxes: false,
149
+ labelFormatter: (v: unknown) => `month ${String(v)}`,
150
+ });
151
+ await waitFor(() =>
152
+ expect(container.textContent).toContain('month 2026-03-01'),
153
+ );
154
+ expect(
155
+ container.querySelectorAll('.recharts-cartesian-axis-tick'),
156
+ ).toHaveLength(0);
157
+ });
158
+ });
159
+
160
+ describe('BaseChartWrapper lines across missing months', () => {
161
+ const gappy = [
162
+ { date: '2026-01-01', historical: 1, forecast_1: 1 },
163
+ { date: '2026-02-01', historical: null, forecast_1: null },
164
+ { date: '2026-03-01', historical: 3, forecast_1: 3 },
165
+ ];
166
+ const forecastData = [{ id: 1, name: 'Forecast' }];
167
+ // A line broken by a gap is drawn as one path with two "M" moves.
168
+ const moves = (container: HTMLElement, i: number) =>
169
+ (
170
+ container
171
+ .querySelectorAll('.recharts-line-curve')
172
+ [i]?.getAttribute('d')
173
+ ?.match(/M/g) ?? []
174
+ ).length;
175
+ const lastLayer = (container: HTMLElement) => {
176
+ const layers = container.querySelectorAll('.recharts-wrapper');
177
+ return layers[layers.length - 1] as HTMLElement;
178
+ };
179
+
180
+ it('leaves a gap by default', async () => {
181
+ const container = await renderChart(640, {
182
+ chartData: gappy,
183
+ forecastData,
184
+ });
185
+ expect(moves(lastLayer(container), 0)).toBe(2);
186
+ expect(moves(lastLayer(container), 1)).toBe(2);
187
+ });
188
+
189
+ it('joins only history when asked', async () => {
190
+ const history = await renderChart(640, {
191
+ chartData: gappy,
192
+ forecastData,
193
+ connectHistoryNulls: true,
194
+ });
195
+ expect(moves(lastLayer(history), 0)).toBe(1);
196
+ expect(moves(lastLayer(history), 1)).toBe(2);
197
+ });
198
+
199
+ it('joins forecasts when asked', async () => {
200
+ const container = await renderChart(640, {
201
+ chartData: gappy,
202
+ forecastData,
203
+ connectForecastNulls: true,
204
+ });
205
+ expect(moves(lastLayer(container), 0)).toBe(2);
206
+ expect(moves(lastLayer(container), 1)).toBe(1);
207
+ });
208
+ });
209
+
210
+ describe('BaseChartWrapper line order', () => {
211
+ const strokes = (container: HTMLElement) => {
212
+ const layers = container.querySelectorAll('.recharts-wrapper');
213
+ return [
214
+ ...layers[layers.length - 1].querySelectorAll('.recharts-line-curve'),
215
+ ].map(e => e.getAttribute('stroke'));
216
+ };
217
+ const props: Partial<BaseChartWrapperProps> = {
218
+ chartData: [
219
+ { date: '2026-01-01', historical_a: 1, forecast_1: 2 },
220
+ { date: '2026-02-01', historical_a: 2, forecast_1: 3 },
221
+ ],
222
+ historyData: [{ id: 'a', name: 'A', color: 'black' }],
223
+ forecastData: [{ id: 1, name: 'F', color: 'cyan' }],
224
+ };
225
+
226
+ it('draws history first, under the forecasts, by default', async () => {
227
+ const container = await renderChart(640, props);
228
+ expect(strokes(container)).toEqual(['black', 'cyan']);
229
+ });
230
+
231
+ it('draws history last, over the forecasts, when asked', async () => {
232
+ const container = await renderChart(640, { ...props, historyOnTop: true });
233
+ // The history layer mounts a moment after the line layer.
234
+ await waitFor(() => expect(strokes(container)).toEqual(['cyan', 'black']));
235
+ });
236
+
237
+ it('keeps history on top after a forecast is hidden and shown again', async () => {
238
+ width = 640;
239
+ const chart = (hidden: Set<string>) => (
240
+ <BaseChartWrapper
241
+ {...(props as BaseChartWrapperProps)}
242
+ hasCombinedData
243
+ loading={false}
244
+ isDarkTheme={false}
245
+ height={H}
246
+ showLegend={false}
247
+ historyOnTop
248
+ hiddenSeries={hidden}
249
+ />
250
+ );
251
+ const { container, rerender } = render(chart(new Set()));
252
+ await waitFor(() => expect(strokes(container)).toHaveLength(2));
253
+ rerender(chart(new Set(['forecast_1'])));
254
+ await waitFor(() => expect(strokes(container)).toEqual(['black']));
255
+ rerender(chart(new Set()));
256
+ await waitFor(() => expect(strokes(container)).toHaveLength(2));
257
+ expect(strokes(container)).toEqual(['cyan', 'black']);
258
+ });
259
+ });
260
+
261
+ describe('BaseChartWrapper end dots', () => {
262
+ const dots = (container: HTMLElement) => {
263
+ const layers = container.querySelectorAll('.recharts-wrapper');
264
+ return layers[layers.length - 1].querySelectorAll('circle').length;
265
+ };
266
+
267
+ it('marks each line’s last point by default', async () => {
268
+ const container = await renderChart(640);
269
+ await waitFor(() => expect(dots(container)).toBe(1));
270
+ });
271
+
272
+ it('leaves them off when asked', async () => {
273
+ const container = await renderChart(640, { showEndDots: false });
274
+ await nextFrame();
275
+ expect(dots(container)).toBe(0);
276
+ });
277
+ });
278
+
279
+ describe('BaseChartWrapper without axes, fixed y range', () => {
280
+ it('keeps yMin/yMax as the hidden y-axis range', async () => {
281
+ // One value of 0.5 on a fixed 0–1 range sits in the middle of the plot;
282
+ // with recharts' default range it would sit at the top.
283
+ const container = await renderChart(640, {
284
+ chartData: [
285
+ { date: '2026-01-01', historical: 0.5 },
286
+ { date: '2026-02-01', historical: 0.5 },
287
+ ],
288
+ showAxes: false,
289
+ yMin: 0,
290
+ yMax: 1,
291
+ autoScaleYAxis: false,
292
+ });
293
+ const d = container
294
+ .querySelectorAll('.recharts-wrapper')
295
+ [
296
+ container.querySelectorAll('.recharts-wrapper').length - 1
297
+ ].querySelector('.recharts-line-curve')!
298
+ .getAttribute('d')!;
299
+ const y = Number(/^M[-\d.]+,([-\d.]+)/.exec(d)?.[1]);
300
+ expect(y).toBeCloseTo(H / 2, 0);
301
+ });
302
+ });
303
+
304
+ describe('BaseChartWrapper range bands across missing months', () => {
305
+ const rows = [
306
+ { date: '2026-01-01', historical: 1, band: [0, 2] },
307
+ { date: '2026-02-01', historical: 2, band: null },
308
+ { date: '2026-03-01', historical: 3, band: [2, 4] },
309
+ ];
310
+ const band = {
311
+ key: 'band',
312
+ name: 'Range',
313
+ quantiles: ['0.1', '0.9'] as [string, string],
314
+ opacity: 0.12,
315
+ };
316
+ const areaMoves = async (connectNulls?: boolean) => {
317
+ width = 640;
318
+ const { container } = render(
319
+ <BaseChartWrapper
320
+ chartData={rows as never}
321
+ hasCombinedData
322
+ loading={false}
323
+ isDarkTheme={false}
324
+ height={H}
325
+ showLegend={false}
326
+ quantileBands={[{ ...band, connectNulls }]}
327
+ />,
328
+ );
329
+ await waitFor(() =>
330
+ expect(container.querySelector('.recharts-area-area')).toBeTruthy(),
331
+ );
332
+ const areas = container.querySelectorAll('.recharts-area-area');
333
+ return (areas[areas.length - 1].getAttribute('d')?.match(/M/g) ?? [])
334
+ .length;
335
+ };
336
+
337
+ it('fills across them by default', async () => {
338
+ expect(await areaMoves()).toBe(1);
339
+ });
340
+
341
+ it('leaves a gap when the band says so', async () => {
342
+ expect(await areaMoves(false)).toBe(2);
343
+ });
344
+ });