| import { expect } from 'vitest'; |
| import { fireEvent } from '@testing-library/react'; |
| import { assertNotNull } from '../../helper/assertNotNull'; |
| import { Coordinate } from '../../../src/util/types'; |
|
|
| export function getTooltip(container: Element): HTMLElement { |
| const allWrappers = container.querySelectorAll('.recharts-tooltip-wrapper'); |
| assertNotNull(allWrappers); |
| expect(allWrappers).toHaveLength(1); |
| const element = allWrappers[0]; |
| assertNotNull(element); |
| if (!(element instanceof HTMLElement)) { |
| throw new Error(`Expected instance of HTMLElement, instead received: [${element}]`); |
| } |
| return element; |
| } |
|
|
| export type MouseCoordinate = { clientX: number; clientY: number }; |
|
|
| const defaultCoordinates: MouseCoordinate = { clientX: 200, clientY: 200 }; |
|
|
| function showTooltipWithEvent( |
| container: Element, |
| selector: string | undefined, |
| maybeCoordinate: MouseCoordinate | undefined, |
| event: 'click' | 'touch' | 'hover', |
| debug?: () => void, |
| ) { |
| const tooltipTriggerElement = selector != null ? container.querySelector(selector) : container; |
| if (tooltipTriggerElement == null && debug != null) { |
| debug(); |
| } |
| assertNotNull(tooltipTriggerElement); |
| expect(tooltipTriggerElement).toBeInTheDocument(); |
| expect(tooltipTriggerElement).toBeVisible(); |
| const coordinate = maybeCoordinate ?? defaultCoordinates; |
| switch (event) { |
| case 'click': { |
| fireEvent.click(tooltipTriggerElement, coordinate); |
| break; |
| } |
| case 'touch': { |
| fireEvent.touchMove(tooltipTriggerElement, { touches: [coordinate] }); |
| break; |
| } |
| case 'hover': { |
| fireEvent.mouseOver(tooltipTriggerElement, coordinate); |
| break; |
| } |
| default: { |
| throw new Error('Unexpected event type'); |
| } |
| } |
| return tooltipTriggerElement; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function showTooltipOnCoordinate( |
| container: Element, |
| selector: string | undefined, |
| coordinates: MouseCoordinate | undefined, |
| debug?: () => void, |
| ): Element { |
| return showTooltipWithEvent(container, selector, coordinates, 'hover', debug); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function showTooltipOnCoordinateTouch( |
| container: Element, |
| selector: string | undefined, |
| coordinates: MouseCoordinate | undefined, |
| debug?: () => void, |
| ): Element { |
| return showTooltipWithEvent(container, selector, coordinates, 'touch', debug); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function showTooltip(container: Element, selector?: string, debug?: () => void): Element { |
| return showTooltipOnCoordinate(container, selector, defaultCoordinates, debug); |
| } |
|
|
| export function hideTooltip(container: Element, mouseHoverSelector: string): void { |
| const element = container.querySelector(mouseHoverSelector); |
| fireEvent.mouseLeave(element); |
| } |
|
|
| export function showTooltipClick(container: Element, selector?: string, debug?: () => void): Element { |
| return showTooltipWithEvent(container, selector, defaultCoordinates, 'click', debug); |
| } |
|
|
| export function expectTooltipNotVisible(container: Element) { |
| assertNotNull(container); |
| const tooltip = getTooltip(container); |
| expect(tooltip).not.toBeVisible(); |
| } |
|
|
| export function expectTooltipPayload( |
| container: Element, |
| expectedTooltipTitle: string, |
| expectedTooltipContent: ReadonlyArray<string>, |
| ) { |
| const tooltip = getTooltip(container); |
| expect(tooltip).toBeInTheDocument(); |
| expect(tooltip).toBeVisible(); |
| expect.soft(tooltip.querySelector('.recharts-tooltip-label').textContent).toBe(expectedTooltipTitle); |
| const tooltipItems = tooltip.querySelectorAll('.recharts-tooltip-item'); |
| expect.soft(Array.from(tooltipItems).map(item => item.textContent)).toEqual(expectedTooltipContent); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function expectTooltipCoordinate(container: Element, expectedCoordinate: Coordinate): void { |
| const tooltip = getTooltip(container); |
| expect(tooltip).toBeInTheDocument(); |
| expect(tooltip).toBeVisible(); |
| const { transform } = tooltip.style; |
| expect(transform).toContain(`translate(${expectedCoordinate.x}px, ${expectedCoordinate.y}px)`); |
| } |
|
|