fix: update Navigation mock structure to use default export for consistency in tests

This commit is contained in:
Simon Larsen
2025-06-11 13:38:16 +01:00
parent 8a3afb8fb0
commit 29339b75ba
2 changed files with 24 additions and 13 deletions

View File

@@ -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<typeof Navigation>;
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()),
);
});

View File

@@ -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<typeof Navigation>;
describe("DuplicateModel", () => {
const fieldsToDuplicate: Select<TestModel> = {};
const fieldsToChange: Array<ModelField<TestModel>> = [
@@ -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", () => {