Fix tests

This commit is contained in:
Robin
2025-01-08 11:41:44 -05:00
parent c66b8c0fd3
commit acd4a1f179

View File

@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details. Please see LICENSE in the repository root for full details.
*/ */
import { renderHook } from "@testing-library/react"; import { act, renderHook } from "@testing-library/react";
import { import {
afterEach, afterEach,
beforeEach, beforeEach,
@@ -39,6 +39,7 @@ describe("useTheme", () => {
vi.spyOn(originalClassList, "add"); vi.spyOn(originalClassList, "add");
vi.spyOn(originalClassList, "remove"); vi.spyOn(originalClassList, "remove");
vi.spyOn(originalClassList, "item").mockReturnValue(null); vi.spyOn(originalClassList, "item").mockReturnValue(null);
(getUrlParams as Mock).mockReturnValue({ theme: "dark" });
}); });
afterEach(() => { afterEach(() => {
@@ -67,7 +68,6 @@ describe("useTheme", () => {
}); });
test("should not reapply the same theme if it hasn't changed", () => { test("should not reapply the same theme if it hasn't changed", () => {
(getUrlParams as Mock).mockReturnValue({ theme: "dark" });
// Simulate a previous theme // Simulate a previous theme
originalClassList.item = vi.fn().mockReturnValue("cpd-theme-dark"); originalClassList.item = vi.fn().mockReturnValue("cpd-theme-dark");
@@ -82,15 +82,17 @@ describe("useTheme", () => {
expect(originalClassList.add).not.toHaveBeenCalled(); expect(originalClassList.add).not.toHaveBeenCalled();
}); });
test("theme changes in response to widget actions", () => { test("theme changes in response to widget actions", async () => {
renderHook(() => useTheme()); renderHook(() => useTheme());
expect(originalClassList.add).toHaveBeenCalledWith("cpd-theme-dark"); expect(originalClassList.add).toHaveBeenCalledWith("cpd-theme-dark");
widget!.lazyActions.emit( await act(() =>
WidgetApiToWidgetAction.ThemeChange, widget!.lazyActions.emit(
new CustomEvent(WidgetApiToWidgetAction.ThemeChange, { WidgetApiToWidgetAction.ThemeChange,
detail: { data: { name: "light" } }, new CustomEvent(WidgetApiToWidgetAction.ThemeChange, {
}), detail: { data: { name: "light" } },
}),
),
); );
expect(originalClassList.remove).toHaveBeenCalledWith( expect(originalClassList.remove).toHaveBeenCalledWith(
"cpd-theme-light", "cpd-theme-light",