Skip to content

Commit

Permalink
test: add unit tests for the Plot panel module (#318)
Browse files Browse the repository at this point in the history
This PR aims to add unit tests in order to improve code coverage.

**Tests added:**
-
[datum.test.ts](https://github.com/Lichtblick-Suite/lichtblick/compare/feature/plot-unit-tests?expand=1#diff-5ab10b2d7c5c7bd71b3f141b00be5323b24eb2803a897df6086bcf70062f0b50)
-
[mathFunctions.test.ts](https://github.com/Lichtblick-Suite/lichtblick/compare/feature/plot-unit-tests?expand=1#diff-5436696450e7992ce0b8cd2d53928a5f8d7c77506083184b5f35b356f0db4eee)
-
[ChartRenderer.test.ts](https://github.com/Lichtblick-Suite/lichtblick/pull/318/files#diff-bcbe2d77e38d978e7fa5ceb7fb22b182d7cb3c4b656cbdfd4988d035c950fa02)
-
[ChartOptions.test.ts](https://github.com/Lichtblick-Suite/lichtblick/pull/318/files#diff-69f32140f0b885e05f3edcc09fb2490369ce9c195d881e36892aa9c6dd078cb7)
-
[EventHandler.test.ts](https://github.com/Lichtblick-Suite/lichtblick/pull/318/files#diff-0c4e9a66020c6841d5dce97f3ff65525e10d3c26668999356f6bb9a5fad07ac7)
-
[PlotLegend.test.tsx](https://github.com/Lichtblick-Suite/lichtblick/pull/318/files#diff-ad1a5a15b54b7bbff1c859455b20aba377cd13f04d7a79bf6707318742703098)
-
[VerticalBars.test.tsx](https://github.com/Lichtblick-Suite/lichtblick/pull/318/files#diff-40b67c2f856aa5d90968791969e7e373e91d827918387edfde7f679ecdf76d62)
- Hooks folder and it tests

**Additional modifications:**

- [Removing unreacheable return undefined](
https://github.com/Lichtblick-Suite/lichtblick/compare/feature/plot-unit-tests?expand=1#diff-c42c5542a4998dffa9179f7024706aeb59728ebd3f6448c145455d9d50dd78fcL277)
- [Removing unnecessary if condition on
usePlotInteractionHandlers](d4c7568#diff-31396d27bed0b5df718340db617a485c4403c32bcc3a17544ae678bd1f1d3308L79-L83)
- Moving constants, types and styles  
- Splitting Plot.ts into different hooks


**Checklist**

- [x] The web version was tested and it is running ok
- [x] The desktop version was tested and it is running ok
- [x] This change is covered by unit tests
- [x] Files constants.ts, types.ts and *.style.ts have been checked and
relevant code snippets have been relocated

---------

Co-authored-by: ctw-joao-luis <[email protected]>
Co-authored-by: Alexandre Neuwald <[email protected]>
Co-authored-by: Luiz Bezerra <[email protected]>
  • Loading branch information
4 people authored Jan 20, 2025
1 parent ee0b651 commit 3327a25
Show file tree
Hide file tree
Showing 45 changed files with 3,781 additions and 837 deletions.
2 changes: 2 additions & 0 deletions packages/suite-base/src/components/Chart/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ export type RpcElement = {
y: number;
};
};

export type EventListenerHandler = (eventName: string, fn?: () => void) => void;
23 changes: 4 additions & 19 deletions packages/suite-base/src/components/Chart/worker/ChartJSManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import EventEmitter from "eventemitter3";
import { Zoom as ZoomPlugin } from "@lichtblick/chartjs-plugin-zoom";
import Logger from "@lichtblick/log";
import { RpcElement, RpcScales } from "@lichtblick/suite-base/components/Chart/types";
import {
addEventListener,
removeEventListener,
} from "@lichtblick/suite-base/components/Chart/worker/eventHandler";
import { maybeCast } from "@lichtblick/suite-base/util/maybeCast";
import { fontMonospace } from "@lichtblick/theme";

Expand All @@ -54,25 +58,6 @@ export type InitOpts = {
// allows us to override the chart.ctx instance field which zoom plugin uses for adding event listeners
type MutableContext<T> = Omit<Chart, "ctx"> & { ctx: T };

function addEventListener(emitter: EventEmitter) {
return (eventName: string, fn?: () => void) => {
const existing = emitter.listeners(eventName);
if (!fn || existing.includes(fn)) {
return;
}

emitter.on(eventName, fn);
};
}

function removeEventListener(emitter: EventEmitter) {
return (eventName: string, fn?: () => void) => {
if (fn) {
emitter.off(eventName, fn);
}
};
}

type ZoomableChart = Chart & {
$zoom: {
panStartHandler(event: HammerInput): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0

import EventEmitter from "eventemitter3";

import { addEventListener, removeEventListener } from "./eventHandler";

describe("EventHandler", () => {
let emitter: EventEmitter;

beforeEach(() => {
emitter = new EventEmitter();
});

describe("addEventListener", () => {
it("should add an event listener if it doesn't already exist", () => {
const handler = jest.fn();
const addListener = addEventListener(emitter);

addListener("testEvent", handler);
emitter.emit("testEvent");

expect(handler).toHaveBeenCalledTimes(1);
});

it("should not add the same event listener multiple times", () => {
const handler = jest.fn();
const addListener = addEventListener(emitter);

addListener("testEvent", handler);
addListener("testEvent", handler);
emitter.emit("testEvent");

expect(handler).toHaveBeenCalledTimes(1);
});

it("should not add a listener if the function is undefined", () => {
const addListener = addEventListener(emitter);

addListener("testEvent");
expect(emitter.listeners("testEvent")).toHaveLength(0);
});
});

describe("removeEventListener", () => {
it("should remove an existing event listener", () => {
const handler = jest.fn();
const addListener = addEventListener(emitter);
const removeListener = removeEventListener(emitter);

addListener("testEvent", handler);
removeListener("testEvent", handler);
emitter.emit("testEvent");

expect(handler).not.toHaveBeenCalled();
});

it("should not throw if removing a listener that doesn't exist", () => {
const handler = jest.fn();
const removeListener = removeEventListener(emitter);

expect(() => {
removeListener("testEvent", handler);
}).not.toThrow();
});

it("should not remove listeners if the function is undefined", () => {
const handler = jest.fn();
const addListener = addEventListener(emitter);
const removeListener = removeEventListener(emitter);

addListener("testEvent", handler);
removeListener("testEvent");
emitter.emit("testEvent");

expect(handler).toHaveBeenCalledTimes(1);
});
});
});
25 changes: 25 additions & 0 deletions packages/suite-base/src/components/Chart/worker/eventHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0

import EventEmitter from "eventemitter3";

import { EventListenerHandler } from "@lichtblick/suite-base/components/Chart/types";

export function addEventListener(emitter: EventEmitter): EventListenerHandler {
return (eventName: string, fn?: () => void): void => {
const existing = emitter.listeners(eventName);
if (!fn || existing.includes(fn)) {
return;
}

emitter.on(eventName, fn);
};
}

export function removeEventListener(emitter: EventEmitter): EventListenerHandler {
return (eventName: string, fn?: () => void) => {
if (fn) {
emitter.off(eventName, fn);
}
};
}
1 change: 1 addition & 0 deletions packages/suite-base/src/i18n/en/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const plot = {
min: "Min",
position: "Position",
receiveTime: "Receive Time",
resetView: "Reset view",
secondsRange: "Range (seconds)",
series: "Series",
showLabels: "Show labels",
Expand Down
Loading

0 comments on commit 3327a25

Please sign in to comment.