fix error page test

This commit is contained in:
Naytitorn Chaovirachot 2024-11-26 01:47:05 +07:00
parent 8a65012044
commit aff0bad8bb
3 changed files with 21 additions and 27 deletions

View File

@ -5,6 +5,8 @@ import Error from "@/app/error";
import Loading from "@/app/loading";
import NotFound from "@/app/not-found";
import { mockError, mockReset } from "./utils";
// Mock Cookies
jest.mock("next/headers", () => ({
cookies: jest.fn(() => ({
@ -15,15 +17,15 @@ jest.mock("next/headers", () => ({
// Error and Loading Pages
describe("Error and Loading Pages", () => {
// it("Error Page should render without crashing", async () => {
// render(await Error());
// });
it("Loading Page should render without crashing", async () => {
render(await Loading());
it("Error Page should render without crashing", () => {
render(<Error error={mockError} reset={mockReset} />);
});
it("Not Found Page should render without crashing", async () => {
render(await NotFound());
it("Loading Page should render without crashing", () => {
render(Loading());
});
it("Not Found Page should render without crashing", () => {
render(NotFound());
});
});

4
__tests__/page/utils.ts Normal file
View File

@ -0,0 +1,4 @@
// #TODO move mock functions here
// Circumvent naming conflict, don't delete/move form this file
export const mockError = new Error();
export const mockReset = () => {};

View File

@ -2,26 +2,13 @@ import { render } from "@testing-library/react";
// Pages - Verification
import VerifyPage from "@/app/verify/page";
// Mock Cookies
jest.mock("next/headers", () => ({
cookies: jest.fn(() => ({
getAll: jest.fn(() => [{ name: "test", value: "cookieValue" }]), // Simulate returning cookies
setAll: jest.fn(), // Simulate setAll method
})),
}));
import React from "react";
// Mock Router
jest.mock('next/navigation', () => ({
useRouter: jest.fn().mockReturnValue({
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
pathname: '/',
query: {},
asPath: '/',
}),
usePathname: jest.fn(() => '/'),
useSearchParams: jest.fn(() => ({
get: jest.fn((key) => {
if (key === 'email') return 'test@example.com'; // Mock behavior for 'email' query param
@ -31,8 +18,9 @@ jest.mock('next/navigation', () => ({
}));
// Verification Pages
// describe("Verification Pages", () => {
// it("Verify Page should render without crashing", () => {
// render(VerifyPage());
// });
// });
describe("Verification Pages", () => {
it("Verify Page should render without crashing", () => {
jest.spyOn(React, 'useEffect').mockImplementation((f) => f());
render(VerifyPage());
});
});