@@ -10,7 +10,9 @@ import { describe, expect, it, vi } from "vitest";
1010// asserting against a mock rather than against `chart.tsx`.
1111vi . mock ( "recharts" , async ( importOriginal ) => ( {
1212 ...( await importOriginal < typeof import ( "recharts" ) > ( ) ) ,
13- ResponsiveContainer : ( { children } : { children ?: React . ReactNode } ) => < div data-testid = "responsive" > { children } </ div > ,
13+ ResponsiveContainer : ( { children } : { children ?: React . ReactNode } ) => (
14+ < div data-testid = "responsive" > { children } </ div >
15+ ) ,
1416} ) ) ;
1517
1618import {
@@ -35,8 +37,20 @@ const config = {
3537} satisfies ChartConfig ;
3638
3739const payload = [
38- { dataKey : "revenue" , name : "revenue" , value : 120 , color : "#0ea5e9" , payload : { month : "Jan" , revenue : 120 } } ,
39- { dataKey : "cost" , name : "cost" , value : 80 , color : "#111827" , payload : { month : "Jan" , cost : 80 } } ,
40+ {
41+ dataKey : "revenue" ,
42+ name : "revenue" ,
43+ value : 120 ,
44+ color : "#0ea5e9" ,
45+ payload : { month : "Jan" , revenue : 120 } ,
46+ } ,
47+ {
48+ dataKey : "cost" ,
49+ name : "cost" ,
50+ value : 80 ,
51+ color : "#111827" ,
52+ payload : { month : "Jan" , cost : 80 } ,
53+ } ,
4054] ;
4155
4256/** ChartTooltipContent calls useChart(), so it must render inside a provider. ChartContainer is that
@@ -51,14 +65,19 @@ function renderInChart(node: React.ReactNode) {
5165 ) ;
5266 // Scoped through this render's OWN container, not the shared document: a test that renders twice would
5367 // otherwise match both slots and fail on "found multiple elements".
54- return { ...result , slot : ( ) => result . container . querySelector ( '[data-testid="slot"]' ) ! } ;
68+ return {
69+ ...result ,
70+ slot : ( ) => result . container . querySelector ( '[data-testid="slot"]' ) ! ,
71+ } ;
5572}
5673
5774describe ( "ChartTooltipContent (recharts v3, #8610)" , ( ) => {
5875 it ( "renders one row per payload entry, labelled from the chart config" , ( ) => {
5976 // The config's `label` -- not the raw dataKey -- is what a reader sees. Regressing to the dataKey is the
6077 // most likely silent breakage, and it still "renders fine".
61- const chart = renderInChart ( < ChartTooltipContent active payload = { payload } label = "Jan" /> ) ;
78+ const chart = renderInChart (
79+ < ChartTooltipContent active payload = { payload } label = "Jan" /> ,
80+ ) ;
6281
6382 expect ( chart . getByText ( "Revenue" ) ) . toBeTruthy ( ) ;
6483 expect ( chart . getByText ( "Cost" ) ) . toBeTruthy ( ) ;
@@ -69,35 +88,62 @@ describe("ChartTooltipContent (recharts v3, #8610)", () => {
6988 it ( "INVARIANT: renders nothing when inactive or when the payload is empty" , ( ) => {
7089 // A tooltip that paints on an empty payload follows the cursor around an empty chart. Both arms, because
7190 // `!active || !payload?.length` is two conditions and only testing one leaves the other free to invert.
72- const inactive = renderInChart ( < ChartTooltipContent active = { false } payload = { payload } /> ) ;
91+ const inactive = renderInChart (
92+ < ChartTooltipContent active = { false } payload = { payload } /> ,
93+ ) ;
7394 expect ( inactive . slot ( ) . textContent ) . toBe ( "" ) ;
7495
7596 const empty = renderInChart ( < ChartTooltipContent active payload = { [ ] } /> ) ;
7697 expect ( empty . slot ( ) . textContent ) . toBe ( "" ) ;
7798 } ) ;
7899
79100 it ( "hides the label when asked, and formats it through labelFormatter otherwise" , ( ) => {
80- const formatted = renderInChart ( < ChartTooltipContent active payload = { payload } label = "Jan" labelFormatter = { ( value ) => `Month: ${ String ( value ) } ` } /> ) ;
101+ const formatted = renderInChart (
102+ < ChartTooltipContent
103+ active
104+ payload = { payload }
105+ label = "Jan"
106+ labelFormatter = { ( value ) => `Month: ${ String ( value ) } ` }
107+ /> ,
108+ ) ;
81109 expect ( formatted . getByText ( "Month: Jan" ) ) . toBeTruthy ( ) ;
82110
83- const hidden = renderInChart ( < ChartTooltipContent active payload = { payload } label = "Jan" hideLabel /> ) ;
111+ const hidden = renderInChart (
112+ < ChartTooltipContent active payload = { payload } label = "Jan" hideLabel /> ,
113+ ) ;
84114 expect ( hidden . queryByText ( "Jan" ) ) . toBeNull ( ) ;
85115 } ) ;
86116
87117 it ( "routes the value through a custom formatter when one is supplied" , ( ) => {
88- const chart = renderInChart ( < ChartTooltipContent active payload = { [ payload [ 0 ] ! ] } formatter = { ( value ) => `$${ String ( value ) } ` } /> ) ;
118+ const chart = renderInChart (
119+ < ChartTooltipContent
120+ active
121+ payload = { [ payload [ 0 ] ! ] }
122+ formatter = { ( value ) => `$${ String ( value ) } ` }
123+ /> ,
124+ ) ;
89125 expect ( chart . getByText ( "$120" ) ) . toBeTruthy ( ) ;
90126 } ) ;
91127
92128 it ( "resolves a series by nameKey when the payload's own key is not the config key" , ( ) => {
93- const chart = renderInChart ( < ChartTooltipContent active payload = { [ { ...payload [ 0 ] ! , dataKey : "unmapped" , name : "unmapped" } ] } nameKey = "revenue" /> ) ;
129+ const chart = renderInChart (
130+ < ChartTooltipContent
131+ active
132+ payload = { [ { ...payload [ 0 ] ! , dataKey : "unmapped" , name : "unmapped" } ] }
133+ nameKey = "revenue"
134+ /> ,
135+ ) ;
94136 expect ( chart . getByText ( "Revenue" ) ) . toBeTruthy ( ) ;
95137 } ) ;
96138} ) ;
97139
98140describe ( "ChartLegendContent (recharts v3, #8610)" , ( ) => {
99141 it ( "labels each legend entry from the config" , ( ) => {
100- const chart = renderInChart ( < ChartLegendContent payload = { [ { value : "revenue" , dataKey : "revenue" , color : "#0ea5e9" } ] } /> ) ;
142+ const chart = renderInChart (
143+ < ChartLegendContent
144+ payload = { [ { value : "revenue" , dataKey : "revenue" , color : "#0ea5e9" } ] }
145+ /> ,
146+ ) ;
101147 expect ( chart . getByText ( "Revenue" ) ) . toBeTruthy ( ) ;
102148 } ) ;
103149
@@ -111,7 +157,9 @@ describe("ChartStyle", () => {
111157 it ( "emits per-theme CSS variables only for series that declare a colour" , ( ) => {
112158 // The `theme` and `color` arms of ChartConfig are a union, and both have to reach the stylesheet -- a
113159 // series configured with `theme` produces one variable per theme selector.
114- const { container } = render ( < ChartStyle id = "chart-test" config = { config } /> ) ;
160+ const { container } = render (
161+ < ChartStyle id = "chart-test" config = { config } /> ,
162+ ) ;
115163 const css = container . querySelector ( "style" ) ?. innerHTML ?? "" ;
116164
117165 expect ( css ) . toContain ( "--color-revenue: #0ea5e9" ) ;
@@ -121,7 +169,9 @@ describe("ChartStyle", () => {
121169 } ) ;
122170
123171 it ( "emits no stylesheet at all when no series declares a colour" , ( ) => {
124- const { container } = render ( < ChartStyle id = "chart-empty" config = { { plain : { label : "Plain" } } } /> ) ;
172+ const { container } = render (
173+ < ChartStyle id = "chart-empty" config = { { plain : { label : "Plain" } } } /> ,
174+ ) ;
125175 expect ( container . querySelector ( "style" ) ) . toBeNull ( ) ;
126176 } ) ;
127177} ) ;
@@ -130,6 +180,8 @@ describe("useChart", () => {
130180 it ( "REGRESSION: throws a named error outside a ChartContainer rather than reading null" , ( ) => {
131181 // Without the guard this is a `Cannot read properties of null` deep inside a tooltip render, which is a
132182 // materially harder thing to diagnose than the message the component actually throws.
133- expect ( ( ) => render ( < ChartTooltipContent active payload = { payload } /> ) ) . toThrow ( / u s e C h a r t m u s t b e u s e d w i t h i n a < C h a r t C o n t a i n e r \/ > / ) ;
183+ expect ( ( ) =>
184+ render ( < ChartTooltipContent active payload = { payload } /> ) ,
185+ ) . toThrow ( / u s e C h a r t m u s t b e u s e d w i t h i n a < C h a r t C o n t a i n e r \/ > / ) ;
134186 } ) ;
135187} ) ;
0 commit comments