Repository navigation
Expand file tree
/
Copy pathjest.setup.ts
More file actions
184 lines (166 loc) · 4.33 KB
/
Copy pathjest.setup.ts
File metadata and controls
184 lines (166 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import "@testing-library/jest-dom";
import "whatwg-fetch";
if (!global.Request) {
global.Request = class Request {
constructor(url: string | Request, init?: RequestInit) {
void init; // Intentionally unused
this.url = typeof url === "string" ? url : url.url;
}
url: string;
} as typeof Request;
}
if (!global.Response) {
global.Response = class Response {
constructor(body?: BodyInit, init?: ResponseInit) {
void body; // Intentionally unused
this.status = init?.status || 200;
}
status: number;
json() {
return Promise.resolve({});
}
text() {
return Promise.resolve("");
}
} as typeof Response;
}
// Mock Next.js 15 App Router navigation
jest.mock("next/navigation", () => ({
useRouter: jest.fn(() => ({
push: jest.fn(),
replace: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
refresh: jest.fn(),
prefetch: jest.fn()
})),
useParams: jest.fn(() => ({
locale: "en"
})),
useSearchParams: jest.fn(() => new URLSearchParams()),
usePathname: jest.fn(() => "/"),
useSelectedLayoutSegment: jest.fn(() => null),
useSelectedLayoutSegments: jest.fn(() => []),
redirect: jest.fn()
}));
// Mock react-i18next
jest.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key: string) => key,
i18n: { language: "en" }
}),
Trans: ({ children }: { children: React.ReactNode }) => children,
I18nextProvider: ({ children }: { children: React.ReactNode }) => children,
// Add additional exports that might be used
withTranslation: () => (Component: React.ComponentType) => Component,
useSSR: () => ({ language: "en" })
}));
// Mock next-themes
jest.mock("next-themes", () => ({
ThemeProvider: ({
children
}: {
children: React.ReactNode;
[key: string]: unknown;
}) => children,
useTheme: () => ({ theme: "light", setTheme: jest.fn() })
}));
// Mock i18n.mjs module
const i18nMock = {
__esModule: true,
default: {
changeLanguage: jest.fn().mockResolvedValue(undefined),
addResourceBundle: jest.fn(),
language: "en",
use: jest.fn(),
init: jest.fn().mockResolvedValue(undefined),
t: jest.fn((key: string) => key),
getResourceBundle: jest.fn(),
getFixedT: jest.fn(),
services: {
resourceStore: {
data: {}
}
}
},
initTranslations: jest.fn().mockResolvedValue({
i18n: {
language: "en",
t: (key: string) => key
},
resources: {},
t: (key: string) => key
}),
getCurrentLocale: jest.fn((pathname: string | null) => {
const config = jest.requireMock<{ default: { defaultLocale: string; locales: string[] } }>("@/i18nConfig").default;
if (!pathname) return config.defaultLocale;
for (const locale of config.locales) {
if (pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`) {
return locale;
}
}
return config.defaultLocale;
})
};
// Mock both paths that might be used
jest.mock("@/utils/i18n", () => i18nMock);
jest.mock("./utils/i18n", () => i18nMock);
// Mock i18nConfig
jest.mock("@/i18nConfig", () => ({
__esModule: true,
default: {
defaultLocale: "en",
singleLocale: false,
prefixDefault: true,
locales: ["en", "es"]
}
}));
// Browser API mocks
const intersectionObserverMock = () => ({
observe: jest.fn(),
disconnect: jest.fn(),
unobserve: jest.fn()
});
window.IntersectionObserver = jest
.fn()
.mockImplementation(intersectionObserverMock);
global.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};
// File API mocks for uploads
global.File = class MockFile extends Blob {
name: string;
lastModified: number;
webkitRelativePath = "";
constructor(
bits: BlobPart[],
filename: string,
options: FilePropertyBag = {}
) {
super(bits, options);
this.name = filename;
this.lastModified = Date.now();
}
} as unknown as typeof File;
/* import * as nextRouter from "next/router";
nextRouter.useRouter = jest.fn();
nextRouter.useRouter.mockImplementation(() => ({
push: jest.fn(),
pathname: "",
asPath: "",
})); */
/* jest.mock("next-auth/jwt", () => {
return {
getToken: jest.fn(),
};
}); */
// Mock console.error globally to prevent error logs in tests
const originalConsoleError = console.error;
beforeAll(() => {
console.error = jest.fn();
});
afterAll(() => {
console.error = originalConsoleError;
});