|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | import { act } from "react"; |
| 5 | +import { fireEvent, waitFor } from "@testing-library/react"; |
5 | 6 | import highcharts from "highcharts"; |
6 | 7 |
|
7 | 8 | import { KeyCode } from "@cloudscape-design/component-toolkit/internal"; |
@@ -167,6 +168,61 @@ describe("CoreChart: legend", () => { |
167 | 168 | }, |
168 | 169 | ); |
169 | 170 |
|
| 171 | + test("renders legend tooltip on hover in cartesian chart, if specified", async () => { |
| 172 | + const { wrapper } = renderChart({ |
| 173 | + highcharts, |
| 174 | + options: { |
| 175 | + series: series.filter((s) => s.type === "line"), |
| 176 | + xAxis: { plotLines: [{ id: "L3", value: 0 }] }, |
| 177 | + yAxis: { plotLines: [{ id: "L3", value: 0 }] }, |
| 178 | + }, |
| 179 | + visibleItems: ["L1", "L3"], |
| 180 | + getLegendTooltipContent: ({ legendItem }) => <div>{legendItem.name}</div>, |
| 181 | + }); |
| 182 | + |
| 183 | + expect(wrapper.findTooltip()).toBe(null); |
| 184 | + |
| 185 | + act(() => mouseOver(getItem(0).getElement())); |
| 186 | + expect(wrapper.findTooltip()).not.toBe(null); |
| 187 | + expect(wrapper.findTooltip()!.getElement().textContent).toBe("L1"); |
| 188 | + |
| 189 | + act(() => mouseOut(getItem(0).getElement())); |
| 190 | + act(() => mouseOver(getItem(2).getElement())); |
| 191 | + expect(wrapper.findTooltip()).not.toBe(null); |
| 192 | + expect(wrapper.findTooltip()!.getElement().textContent).toBe("Line 3"); |
| 193 | + |
| 194 | + act(() => mouseOut(getItem(2).getElement())); |
| 195 | + await clearHighlightPause(); |
| 196 | + expect(wrapper.findTooltip()).toBe(null); |
| 197 | + }); |
| 198 | + |
| 199 | + test("renders legend tooltip on focus in cartesian chart, if specified", async () => { |
| 200 | + const { wrapper } = renderChart({ |
| 201 | + highcharts, |
| 202 | + options: { |
| 203 | + series: series.filter((s) => s.type === "line"), |
| 204 | + xAxis: { plotLines: [{ id: "L3", value: 0 }] }, |
| 205 | + yAxis: { plotLines: [{ id: "L3", value: 0 }] }, |
| 206 | + }, |
| 207 | + visibleItems: ["L1", "L3"], |
| 208 | + getLegendTooltipContent: ({ legendItem }) => <div>{legendItem.name}</div>, |
| 209 | + }); |
| 210 | + |
| 211 | + expect(wrapper.findTooltip()).toBe(null); |
| 212 | + |
| 213 | + act(() => getItem(0).focus()); |
| 214 | + await waitFor(() => { |
| 215 | + expect(wrapper.findTooltip()).not.toBe(null); |
| 216 | + }); |
| 217 | + expect(wrapper.findTooltip()!.getElement().textContent).toBe("L1"); |
| 218 | + |
| 219 | + fireEvent.keyDown(getItem(0).getElement(), { keyCode: KeyCode.right }); |
| 220 | + await waitFor(() => { |
| 221 | + expect(wrapper.findTooltip()).not.toBe(null); |
| 222 | + }); |
| 223 | + expect(wrapper.findTooltip()!.getElement().textContent).toBe("L2"); |
| 224 | + }); |
| 225 | + |
170 | 226 | test.each([{ position: "bottom" as const }, { position: "side" as const }])( |
171 | 227 | "legend items are highlighted on hover in pie chart", |
172 | 228 | async ({ position }) => { |
@@ -208,6 +264,53 @@ describe("CoreChart: legend", () => { |
208 | 264 | }, |
209 | 265 | ); |
210 | 266 |
|
| 267 | + test("renders legend tooltip on hover in pie chart, if specified", async () => { |
| 268 | + const { wrapper } = renderChart({ |
| 269 | + highcharts, |
| 270 | + options: { series: series.filter((s) => s.type === "pie") }, |
| 271 | + visibleItems: ["P1", "P3"], |
| 272 | + getLegendTooltipContent: ({ legendItem }) => <div>{legendItem.name}</div>, |
| 273 | + }); |
| 274 | + |
| 275 | + expect(wrapper.findTooltip()).toBe(null); |
| 276 | + |
| 277 | + act(() => mouseOver(getItem(0).getElement())); |
| 278 | + expect(wrapper.findTooltip()).not.toBe(null); |
| 279 | + expect(wrapper.findTooltip()!.getElement().textContent).toBe("P1"); |
| 280 | + |
| 281 | + act(() => mouseOut(getItem(0).getElement())); |
| 282 | + act(() => mouseOver(getItem(2).getElement())); |
| 283 | + expect(wrapper.findTooltip()).not.toBe(null); |
| 284 | + expect(wrapper.findTooltip()!.getElement().textContent).toBe("Pie 3"); |
| 285 | + |
| 286 | + act(() => mouseOut(getItem(2).getElement())); |
| 287 | + await clearHighlightPause(); |
| 288 | + expect(wrapper.findTooltip()).toBe(null); |
| 289 | + }); |
| 290 | + |
| 291 | + test("renders legend tooltip on focus in pie chart, if specified", async () => { |
| 292 | + const { wrapper } = renderChart({ |
| 293 | + highcharts, |
| 294 | + options: { series: series.filter((s) => s.type === "pie") }, |
| 295 | + visibleItems: ["P1", "P3"], |
| 296 | + getLegendTooltipContent: ({ legendItem }) => <div>{legendItem.name}</div>, |
| 297 | + }); |
| 298 | + |
| 299 | + expect(wrapper.findTooltip()).toBe(null); |
| 300 | + |
| 301 | + act(() => getItem(0).focus()); |
| 302 | + await waitFor(() => { |
| 303 | + expect(wrapper.findTooltip()).not.toBe(null); |
| 304 | + }); |
| 305 | + expect(wrapper.findTooltip()!.getElement().textContent).toBe("P1"); |
| 306 | + |
| 307 | + fireEvent.keyDown(getItem(0).getElement(), { keyCode: KeyCode.right }); |
| 308 | + await waitFor(() => { |
| 309 | + expect(wrapper.findTooltip()).not.toBe(null); |
| 310 | + }); |
| 311 | + expect(wrapper.findTooltip()!.getElement().textContent).toBe("P2"); |
| 312 | + }); |
| 313 | + |
211 | 314 | test.each([{ position: "bottom" as const }, { position: "side" as const }])( |
212 | 315 | "legend items are highlighted when cartesian chart series point is highlighted", |
213 | 316 | async () => { |
|
0 commit comments