Skip to content

Commit

Permalink
Merge branch 'feature/plot-unit-tests' of github.com:Lichtblick-Suite…
Browse files Browse the repository at this point in the history
…/lichtblick into feature/plot-unit-tests
  • Loading branch information
luluiz committed Jan 17, 2025
2 parents f4c7a04 + fc88c3b commit b8a4942
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions packages/suite-base/src/panels/Plot/hooks/useRenderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0

import { createTheme } from "@mui/material/styles";
import { createTheme, Theme } from "@mui/material/styles";
import { renderHook } from "@testing-library/react";

import BasicBuilder from "@lichtblick/suite-base/testing/builders/BasicBuilder";

import useRenderer from "./useRenderer";
import { OffscreenCanvasRenderer } from "../OffscreenCanvasRenderer";

Expand All @@ -20,31 +22,32 @@ jest.mock("../OffscreenCanvasRenderer", () => {

Object.defineProperty(HTMLCanvasElement.prototype, "transferControlToOffscreen", {
value: jest.fn().mockImplementation(() => ({
width: 0,
height: 0,
width: BasicBuilder.number(),
height: BasicBuilder.number(),
})),
});

describe("useRenderer hook", () => {
it("should create a renderer and attach canvas to the canvasDiv", () => {
const canvasDiv = document.createElement("div");
const theme = createTheme();
let canvasDiv: HTMLDivElement;
let theme: Theme;

beforeEach(() => {
canvasDiv = document.createElement("div");
theme = createTheme();
});

it("should create a renderer and attach canvas to the canvasDiv", () => {
const { result, unmount } = renderHook(() => useRenderer(canvasDiv, theme));

expect(result.current).toBeInstanceOf(OffscreenCanvasRenderer);
expect(canvasDiv.querySelector("canvas")).not.toBeNull();

//unmounting the hook
unmount();

//Checking that the renderer was destroyed and canvas removed
expect(canvasDiv.querySelector("canvas")).toBeNull();
});

it("should not create renderer if canvasDiv is undefined", () => {
const theme = createTheme();

const { result } = renderHook(() => useRenderer(ReactNull, theme));

expect(result.current).toBeUndefined();
Expand All @@ -53,15 +56,13 @@ describe("useRenderer hook", () => {
it("should correctly reinitialize the renderer if canvasDiv changes", () => {
const canvasDiv1 = document.createElement("div");
const canvasDiv2 = document.createElement("div");
const theme = createTheme();

const { result, rerender } = renderHook(({ div }) => useRenderer(div, theme), {
initialProps: { div: canvasDiv1 },
});

const initialRenderer = result.current;

// Switching canvasDiv
rerender({ div: canvasDiv2 });

expect(result.current).not.toBe(initialRenderer);
Expand Down

0 comments on commit b8a4942

Please sign in to comment.