-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for the Plot panel module (#318)
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
1 parent
ee0b651
commit 3327a25
Showing
45 changed files
with
3,781 additions
and
837 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/suite-base/src/components/Chart/worker/eventHandler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
packages/suite-base/src/components/Chart/worker/eventHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.