Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
laisspportugal committed Jan 13, 2025
1 parent 3470a91 commit 2764d8c
Showing 1 changed file with 143 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,166 +5,171 @@

import { renderHook, act } from "@testing-library/react";
import { MutableRefObject } from "react";
import usePlotInteractionHandlers from "./usePlotInteractionHandlers";

import { debouncePromise } from "@lichtblick/den/async";
import BasicBuilder from "@lichtblick/suite-base/testing/builders/BasicBuilder";
import { PlotCoordinator } from "@lichtblick/suite-base/panels/Plot/PlotCoordinator";
import {
useSetHoverValue,
useClearHoverValue,
} from "@lichtblick/suite-base/context/TimelineInteractionStateContext";
import { OffscreenCanvasRenderer } from "@lichtblick/suite-base/panels/Plot/OffscreenCanvasRenderer";
import { PlotCoordinator } from "@lichtblick/suite-base/panels/Plot/PlotCoordinator";
import { PlotConfig } from "@lichtblick/suite-base/panels/Plot/config";
import { useSetHoverValue, useClearHoverValue } from "@lichtblick/suite-base/context/TimelineInteractionStateContext";
import BasicBuilder from "@lichtblick/suite-base/testing/builders/BasicBuilder";

import usePlotInteractionHandlers from "./usePlotInteractionHandlers";

jest.mock("@lichtblick/den/async", () => ({
debouncePromise: jest.fn(),
debouncePromise: jest.fn(),
}));

jest.mock("@lichtblick/suite-base/context/TimelineInteractionStateContext", () => ({
useSetHoverValue: jest.fn(),
useClearHoverValue: jest.fn(),
useTimelineInteractionState: jest.fn(),
useSetHoverValue: jest.fn(),
useClearHoverValue: jest.fn(),
useTimelineInteractionState: jest.fn(),
}));

jest.mock("@lichtblick/suite-base/components/MessagePipeline", () => ({
useMessagePipelineGetter: jest.fn(),
useMessagePipelineGetter: jest.fn(),
}));

describe("usePlotInteractionHandlers", () => {
const mockCoordinator = {
getXValueAtPixel: jest.fn(() => BasicBuilder.number()),
resetBounds: jest.fn(),
setZoomMode: jest.fn(),
getCsvData: jest.fn(),
addInteractionEvent: jest.fn(),
} as unknown as PlotCoordinator;
const mockRenderer = {
getElementsAtPixel: jest.fn(),
} as unknown as OffscreenCanvasRenderer;
const mockSubscriberId = BasicBuilder.string();
const mockCustomTitle = BasicBuilder.string();
const mockConfig = {
xAxisVal: "timestamp",
PANEL_TITLE_CONFIG_KEY: mockCustomTitle,
} as unknown as PlotConfig;
const mockSetActiveTooltip = jest.fn();
const mockDraggingRef = { current: false } as MutableRefObject<boolean>;
const mockSetHoverValue = jest.fn();
const mockClearHoverValue = jest.fn();

const setup = () => {
jest.clearAllMocks();
(useSetHoverValue as jest.Mock).mockReturnValue(mockSetHoverValue);
(useClearHoverValue as jest.Mock).mockReturnValue(mockClearHoverValue);

return renderHook(() =>
usePlotInteractionHandlers(
mockCoordinator,
mockRenderer,
mockSubscriberId,
mockConfig,
mockSetActiveTooltip,
{ shouldSync: false },
mockDraggingRef,
),
);
};

describe("onMouseMove", () => {
it("sets hover value correctly", () => {
const mockBuildTooltip = jest.fn();
(debouncePromise as jest.Mock).mockReturnValue(mockBuildTooltip);

const { result } = setup();

act(() => {
result.current.onMouseMove({
clientX: 100,
clientY: 100,
currentTarget: {
getBoundingClientRect: jest.fn(() => ({
left: 50,
top: 50,
})),
},
} as any);
});

expect(mockCoordinator.getXValueAtPixel).toHaveBeenCalledWith(50); // 100 - 50
expect(mockBuildTooltip).toHaveBeenCalledWith({
clientX: 100,
clientY: 100,
canvasX: 50,
canvasY: 50,
});
expect(mockSetHoverValue).toHaveBeenCalledWith({
componentId: mockSubscriberId,
value: expect.any(Number),
type: "PLAYBACK_SECONDS",
});
});
const mockCoordinator = {
getXValueAtPixel: jest.fn(() => BasicBuilder.number()),
resetBounds: jest.fn(),
setZoomMode: jest.fn(),
getCsvData: jest.fn(),
addInteractionEvent: jest.fn(),
} as unknown as PlotCoordinator;
const mockRenderer = {
getElementsAtPixel: jest.fn(),
} as unknown as OffscreenCanvasRenderer;
const mockSubscriberId = BasicBuilder.string();
const mockCustomTitle = BasicBuilder.string();
const mockConfig = {
xAxisVal: "timestamp",
PANEL_TITLE_CONFIG_KEY: mockCustomTitle,
} as unknown as PlotConfig;
const mockSetActiveTooltip = jest.fn();
const mockDraggingRef = { current: false } as MutableRefObject<boolean>;
const mockSetHoverValue = jest.fn();
const mockClearHoverValue = jest.fn();

const setup = () => {
jest.clearAllMocks();
(useSetHoverValue as jest.Mock).mockReturnValue(mockSetHoverValue);
(useClearHoverValue as jest.Mock).mockReturnValue(mockClearHoverValue);

return renderHook(() =>
usePlotInteractionHandlers(
mockCoordinator,
mockRenderer,
mockSubscriberId,
mockConfig,
mockSetActiveTooltip,
{ shouldSync: false },
mockDraggingRef,
),
);
};

describe("onMouseMove", () => {
it("sets hover value correctly", () => {
const mockBuildTooltip = jest.fn();
(debouncePromise as jest.Mock).mockReturnValue(mockBuildTooltip);

const { result } = setup();

act(() => {
result.current.onMouseMove({

Check failure on line 83 in packages/suite-base/src/panels/Plot/hooks/usePlotInteractionHandlers.test.ts

View workflow job for this annotation

GitHub Actions / lint (ubuntu-20.04)

Unsafe argument of type `any` assigned to a parameter of type `MouseEvent<HTMLElement, MouseEvent>`
clientX: 100,
clientY: 100,
currentTarget: {
getBoundingClientRect: jest.fn(() => ({
left: 50,
top: 50,
})),
},
} as any);
});

expect(mockCoordinator.getXValueAtPixel).toHaveBeenCalledWith(50); // 100 - 50
expect(mockBuildTooltip).toHaveBeenCalledWith({
clientX: 100,
clientY: 100,
canvasX: 50,
canvasY: 50,
});
expect(mockSetHoverValue).toHaveBeenCalledWith({
componentId: mockSubscriberId,
value: expect.any(Number),
type: "PLAYBACK_SECONDS",
});
});
});

describe("onMouseOut", () => {
it("clears hover value", () => {
const { result } = setup();
describe("onMouseOut", () => {
it("clears hover value", () => {
const { result } = setup();

act(() => {
result.current.onMouseOut();
});
act(() => {
result.current.onMouseOut();
});

expect(mockSetActiveTooltip).toHaveBeenCalledWith(undefined);
expect(mockClearHoverValue).toHaveBeenCalledWith(mockSubscriberId);
});
expect(mockSetActiveTooltip).toHaveBeenCalledWith(undefined);
expect(mockClearHoverValue).toHaveBeenCalledWith(mockSubscriberId);
});

describe("onWheel", () => {
it("handles wheel event correctly", () => {
const { result } = setup();

const boundingRect = {
left: 5,
top: 5,
width: 100,
height: 100,
toJSON: jest.fn().mockReturnValue({
left: 5,
top: 5,
width: 100,
height: 100,
}),
};

act(() => {
result.current.onWheel({
deltaX: 1,
deltaY: -1,
clientX: 10,
clientY: 20,
currentTarget: {
getBoundingClientRect: jest.fn(() => boundingRect),
},
} as any);
});

expect(mockCoordinator.addInteractionEvent).toHaveBeenCalledWith({
type: "wheel",
cancelable: false,
deltaY: -1,
deltaX: 1,
clientX: 10,
clientY: 20,
boundingClientRect: boundingRect.toJSON(),
});
});
});

describe("onWheel", () => {
it("handles wheel event correctly", () => {
const { result } = setup();

const boundingRect = {
left: 5,
top: 5,
width: 100,
height: 100,
toJSON: jest.fn().mockReturnValue({
left: 5,
top: 5,
width: 100,
height: 100,
}),
};

act(() => {
result.current.onWheel({
deltaX: 1,
deltaY: -1,
clientX: 10,
clientY: 20,
currentTarget: {
getBoundingClientRect: jest.fn(() => boundingRect),
},
} as any);
});

expect(mockCoordinator.addInteractionEvent).toHaveBeenCalledWith({
type: "wheel",
cancelable: false,
deltaX: 1,
deltaY: -1,
clientX: 10,
clientY: 20,
boundingClientRect: boundingRect.toJSON(),
});
});
});

describe("onResetView", () => {
it("resets coordinator bounds", () => {
const { result } = setup();
describe("onResetView", () => {
it("resets coordinator bounds", () => {
const { result } = setup();

act(() => {
result.current.onResetView();
});
act(() => {
result.current.onResetView();
});

expect(mockCoordinator.resetBounds).toHaveBeenCalled();
});
expect(mockCoordinator.resetBounds).toHaveBeenCalled();
});
});
});

0 comments on commit 2764d8c

Please sign in to comment.