-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.ts
More file actions
129 lines (116 loc) · 2.94 KB
/
jest.setup.ts
File metadata and controls
129 lines (116 loc) · 2.94 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
/**
* Jest setup file
* This file is executed before each test file
*/
import '@testing-library/jest-dom';
import React from 'react';
// Mock Next.js Image component
jest.mock('next/image', () => ({
__esModule: true,
default: ({
priority: _priority,
fill: _fill,
unoptimized: _unoptimized,
placeholder: _placeholder,
blurDataURL: _blurDataURL,
loader: _loader,
quality: _quality,
...htmlProps
}: React.ImgHTMLAttributes<HTMLImageElement> & {
priority?: boolean;
fill?: boolean;
unoptimized?: boolean;
placeholder?: string;
blurDataURL?: string;
loader?: unknown;
quality?: number;
}) => {
return React.createElement('img', htmlProps);
},
}));
// Mock Next.js router
jest.mock('next/navigation', () => ({
useRouter() {
return {
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
back: jest.fn(),
pathname: '/',
query: {},
asPath: '/',
};
},
usePathname() {
return '/';
},
useSearchParams() {
return new URLSearchParams();
},
}));
// Mock next-intl
jest.mock('next-intl', () => {
const mockT = (key: string) => key;
mockT.rich = (key: string) => key;
mockT.raw = (key: string) => key;
mockT.markup = (key: string) => key;
return {
useTranslations: () => mockT,
useLocale: () => 'en',
useMessages: () => ({}),
NextIntlClientProvider: ({ children }: { children: React.ReactNode }) => children,
};
});
// Mock TextEncoder/TextDecoder for Node.js test environment
import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder as typeof global.TextDecoder;
// Mock ResizeObserver
global.ResizeObserver = class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
};
// Mock IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
root = null;
rootMargin = '';
thresholds = [];
observe() {}
unobserve() {}
disconnect() {}
takeRecords() { return []; }
} as unknown as typeof IntersectionObserver;
// Only mock browser APIs when window is available (jsdom environment)
if (typeof window !== 'undefined') {
// Mock matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock scrollTo
window.scrollTo = jest.fn();
// Mock localStorage
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
}
// Suppress console errors in tests (optional)
// global.console = {
// ...console,
// error: jest.fn(),
// warn: jest.fn(),
// };