Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

3.7 KB
Raw Blame
import { renderHook } from '@testing-library/react';
import { LegendAction } from 'lib/uPlotV2/components/types';
import { usePlotContext } from 'lib/uPlotV2/context/PlotContext';
import { useLegendActions } from 'lib/uPlotV2/hooks/useLegendActions';

jest.mock('lib/uPlotV2/context/PlotContext');

const mockUsePlotContext = usePlotContext as jest.MockedFunction<
	typeof usePlotContext
>;

describe('useLegendActions', () => {
	let onToggleSeriesVisibility: jest.Mock;
	let onToggleSeriesOnOff: jest.Mock;
	let onShowOnlySeries: jest.Mock;
	let onShowAllSeries: jest.Mock;
	let onFocusSeries: jest.Mock;
	let onHighlightSeries: jest.Mock;
	let setPlotContextInitialState: jest.Mock;
	let syncSeriesVisibilityToLocalStorage: jest.Mock;
	let cancelAnimationFrameSpy: jest.SpyInstance<void, [handle: number]>;

	beforeAll(() => {
		jest
			.spyOn(global, 'requestAnimationFrame')
			.mockImplementation((cb: FrameRequestCallback): number => {
				cb(0);
				return 1;
			});

		cancelAnimationFrameSpy = jest
			.spyOn(global, 'cancelAnimationFrame')
			.mockImplementation(() => {});
	});

	afterAll(() => {
		jest.restoreAllMocks();
	});

	beforeEach(() => {
		onToggleSeriesVisibility = jest.fn();
		onToggleSeriesOnOff = jest.fn();
		onShowOnlySeries = jest.fn();
		onShowAllSeries = jest.fn();
		onFocusSeries = jest.fn();
		onHighlightSeries = jest.fn();
		setPlotContextInitialState = jest.fn();
		syncSeriesVisibilityToLocalStorage = jest.fn();

		mockUsePlotContext.mockReturnValue({
			onToggleSeriesVisibility,
			onToggleSeriesOnOff,
			onShowOnlySeries,
			onShowAllSeries,
			onFocusSeries,
			onHighlightSeries,
			setPlotContextInitialState,
			syncSeriesVisibilityToLocalStorage,
		});

		cancelAnimationFrameSpy.mockClear();
	});

	describe('visibility actions', () => {
		it('toggles a single series on row click', () => {
			const { result } = renderHook(() => useLegendActions());

			result.current({ type: LegendAction.TOGGLE, seriesIndex: 2 });

			expect(onToggleSeriesOnOff).toHaveBeenCalledWith(2);
			// The row must never isolate — that is what "Only" is for.
			expect(onToggleSeriesVisibility).not.toHaveBeenCalled();
		});

		it('forwards the Only and All actions to the plot', () => {
			const { result } = renderHook(() => useLegendActions());

			result.current({ type: LegendAction.SHOW_ONLY, seriesIndex: 1 });
			result.current({ type: LegendAction.SHOW_ALL });

			expect(onShowOnlySeries).toHaveBeenCalledWith(1);
			expect(onShowAllSeries).toHaveBeenCalled();
		});
	});

	describe('hover highlight', () => {
		it('highlights the hovered series', () => {
			const { result } = renderHook(() => useLegendActions());

			result.current({ type: LegendAction.HOVER, seriesIndex: 2 });

			expect(onHighlightSeries).toHaveBeenCalledWith(2);
		});

		it('clears the highlight on leave', () => {
			const { result } = renderHook(() => useLegendActions());

			result.current({ type: LegendAction.HOVER, seriesIndex: null });

			expect(onHighlightSeries).toHaveBeenCalledWith(null);
		});

		it('coalesces rapid hovers into one frame', () => {
			const { result } = renderHook(() => useLegendActions());

			result.current({ type: LegendAction.HOVER, seriesIndex: 1 });
			result.current({ type: LegendAction.HOVER, seriesIndex: 2 });

			// Each new hover cancels the frame the previous one queued.
			expect(cancelAnimationFrameSpy).toHaveBeenCalled();
		});

		it('cancels a pending highlight frame on unmount', () => {
			jest
				.spyOn(global, 'requestAnimationFrame')
				.mockImplementation((): number => 7);

			const { result, unmount } = renderHook(() => useLegendActions());
			result.current({ type: LegendAction.HOVER, seriesIndex: 1 });
			unmount();

			expect(cancelAnimationFrameSpy).toHaveBeenCalledWith(7);
		});
	});
});