From 29339b75ba229914654f8ef631d3bdf7dc2e472d Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Wed, 11 Jun 2025 13:38:16 +0100 Subject: [PATCH] fix: update Navigation mock structure to use default export for consistency in tests --- Common/Tests/UI/Components/404.test.tsx | 18 +++++++++++------- .../UI/Components/DuplicateModel.test.tsx | 19 +++++++++++++------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Common/Tests/UI/Components/404.test.tsx b/Common/Tests/UI/Components/404.test.tsx index 8056f54348..5654e869c7 100644 --- a/Common/Tests/UI/Components/404.test.tsx +++ b/Common/Tests/UI/Components/404.test.tsx @@ -6,14 +6,20 @@ import URL from "../../../Types/API/URL"; import Email from "../../../Types/Email"; import * as React from "react"; import { describe, expect, jest } from "@jest/globals"; +import * as Navigation from "../../../UI/Utils/Navigation"; // Mock the Navigation module to avoid real navigation jest.mock("../../../UI/Utils/Navigation", () => { return { - navigate: jest.fn(), + default: { + navigate: jest.fn(), + }, }; }); +// Type assertion for the mocked Navigation module +const MockedNavigation = Navigation as jest.Mocked; + describe("NotFound Component", () => { const mockProps: ComponentProps = { homeRoute: new Route("/"), // Replace with your actual home route object @@ -55,18 +61,16 @@ describe("NotFound Component", () => { test('should navigate to the home route when "Go Home" button is clicked', () => { const goHomeButton: HTMLElement = screen.getByText("Go Home"); fireEvent.click(goHomeButton); - expect( - require("../../../UI/Utils/Navigation").navigate, - ).toHaveBeenCalledWith(mockProps.homeRoute); + expect(MockedNavigation.default.navigate).toHaveBeenCalledWith( + mockProps.homeRoute, + ); }); test('should navigate to the support email when "Contact Support" button is clicked', () => { const contactSupportButton: HTMLElement = screen.getByText("Contact Support"); fireEvent.click(contactSupportButton); - expect( - require("../../../UI/Utils/Navigation").navigate, - ).toHaveBeenCalledWith( + expect(MockedNavigation.default.navigate).toHaveBeenCalledWith( URL.fromString("mailto:" + mockProps.supportEmail.toString()), ); }); diff --git a/Common/Tests/UI/Components/DuplicateModel.test.tsx b/Common/Tests/UI/Components/DuplicateModel.test.tsx index c3086631f4..c9e737eadd 100644 --- a/Common/Tests/UI/Components/DuplicateModel.test.tsx +++ b/Common/Tests/UI/Components/DuplicateModel.test.tsx @@ -17,6 +17,7 @@ import ObjectID from "../../../Types/ObjectID"; import React from "react"; import { act } from "react-test-renderer"; import Select from "../../../Types/BaseDatabase/Select"; +import * as Navigation from "../../../UI/Utils/Navigation"; @TableMetaData({ tableName: "Foo", @@ -61,10 +62,15 @@ jest.mock("../../../UI/Utils/ModelAPI/ModelAPI", () => { jest.mock("../../../UI/Utils/Navigation", () => { return { - navigate: jest.fn(), + default: { + navigate: jest.fn(), + }, }; }); +// Type assertion for the mocked Navigation module +const MockedNavigation = Navigation as jest.Mocked; + describe("DuplicateModel", () => { const fieldsToDuplicate: Select = {}; const fieldsToChange: Array> = [ @@ -158,11 +164,12 @@ describe("DuplicateModel", () => { }); }); await waitFor(() => { - return expect( - require("../../../UI/Utils/Navigation").navigate, - ).toBeCalledWith(new Route("/done/foobar"), { - forceNavigate: true, - }); + return expect(MockedNavigation.default.navigate).toBeCalledWith( + new Route("/done/foobar"), + { + forceNavigate: true, + }, + ); }); }); it("closes confirmation dialog when close button is clicked", () => {