Skip to content

Commit 5886e0b

Browse files
committed
Fixed unit tests failing
1 parent 1043a07 commit 5886e0b

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

_tests/homepage.tests.tsx

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,64 @@ import * as React from "react";
33
import { screen, waitFor } from "@testing-library/react";
44
import { expect, test, vi } from "vitest";
55

6-
import { NextNavigationMock, renderWithStore } from "./test-env";
6+
import { renderWithStore } from "./test-env";
77
import Homepage from "../src/pages";
88

9-
vi.mock("next/navigation", () => NextNavigationMock);
10-
renderWithStore(<Homepage />);
9+
// JSDOM does not implement browser APIs used by embla-carousel
10+
Object.defineProperty(window, "matchMedia", {
11+
writable: true,
12+
value: vi.fn().mockImplementation((query: string) => ({
13+
matches: false,
14+
media: query,
15+
onchange: null,
16+
addListener: vi.fn(),
17+
removeListener: vi.fn(),
18+
addEventListener: vi.fn(),
19+
removeEventListener: vi.fn(),
20+
dispatchEvent: vi.fn(),
21+
})),
22+
});
23+
24+
global.IntersectionObserver = vi.fn().mockImplementation(() => ({
25+
observe: vi.fn(),
26+
unobserve: vi.fn(),
27+
disconnect: vi.fn(),
28+
root: null,
29+
rootMargin: "",
30+
thresholds: [],
31+
takeRecords: vi.fn().mockReturnValue([]),
32+
}));
33+
34+
global.ResizeObserver = vi.fn().mockImplementation(() => ({
35+
observe: vi.fn(),
36+
unobserve: vi.fn(),
37+
disconnect: vi.fn(),
38+
}));
39+
40+
vi.mock("next/navigation", async () => {
41+
const mockRouter = await import("next-router-mock");
42+
43+
return {
44+
...mockRouter,
45+
notFound: vi.fn(),
46+
redirect: vi.fn().mockImplementation((url: string) => {
47+
mockRouter.memoryRouter.setCurrentUrl(url);
48+
}),
49+
};
50+
});
51+
52+
vi.mock("next/router", async () => {
53+
const mockRouter = await import("next-router-mock");
54+
55+
return {
56+
...mockRouter,
57+
useRouter: mockRouter.useRouter,
58+
};
59+
});
1160

1261
test("Homepage", async () => {
62+
renderWithStore(<Homepage />);
63+
1364
await waitFor(
1465
() =>
1566
expect(

0 commit comments

Comments
 (0)