-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/plot-unit-tests' of github.com:Lichtblick-Suite…
…/lichtblick into feature/plot-unit-tests
- Loading branch information
Showing
1 changed file
with
13 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
||
|
@@ -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(); | ||
|
@@ -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); | ||
|