Repository navigation
Expand file tree
/
Copy pathjest.setup.js
More file actions
180 lines (160 loc) · 5.1 KB
/
Copy pathjest.setup.js
File metadata and controls
180 lines (160 loc) · 5.1 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
// Learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom'
// Mock de Next.js router
jest.mock('next/navigation', () => ({
useRouter() {
return {
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
refresh: jest.fn(),
pathname: '/',
query: {},
asPath: '/',
}
},
usePathname() {
return '/'
},
useSearchParams() {
return new URLSearchParams()
},
}))
// Mock de Next.js Image
jest.mock('next/image', () => ({
__esModule: true,
default: (props) => {
// eslint-disable-next-line @next/next/no-img-element, jsx-a11y/alt-text
return <img {...props} />
},
}))
// Mock de Supabase
jest.mock('./lib/supabase', () => ({
supabase: {
from: jest.fn(() => ({
select: jest.fn().mockReturnThis(),
insert: jest.fn().mockReturnThis(),
update: jest.fn().mockReturnThis(),
delete: jest.fn().mockReturnThis(),
eq: jest.fn().mockReturnThis(),
neq: jest.fn().mockReturnThis(),
gt: jest.fn().mockReturnThis(),
gte: jest.fn().mockReturnThis(),
lt: jest.fn().mockReturnThis(),
lte: jest.fn().mockReturnThis(),
like: jest.fn().mockReturnThis(),
ilike: jest.fn().mockReturnThis(),
is: jest.fn().mockReturnThis(),
in: jest.fn().mockReturnThis(),
contains: jest.fn().mockReturnThis(),
order: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
range: jest.fn().mockReturnThis(),
maybeSingle: jest.fn().mockResolvedValue({ data: null, error: null }),
single: jest.fn().mockResolvedValue({ data: null, error: null }),
})),
auth: {
getUser: jest.fn().mockResolvedValue({ data: { user: null }, error: null }),
getSession: jest.fn().mockResolvedValue({ data: { session: null }, error: null }),
signOut: jest.fn().mockResolvedValue({ error: null }),
signInWithPassword: jest.fn().mockResolvedValue({ data: { user: null, session: null }, error: null }),
onAuthStateChange: jest.fn(() => ({
data: { subscription: { unsubscribe: jest.fn() } },
})),
},
storage: {
from: jest.fn(() => ({
upload: jest.fn().mockResolvedValue({ data: { path: 'test-path' }, error: null }),
download: jest.fn().mockResolvedValue({ data: new Blob(), error: null }),
getPublicUrl: jest.fn().mockReturnValue({ data: { publicUrl: 'https://test-url.com' } }),
list: jest.fn().mockResolvedValue({ data: { files: [] }, error: null }),
remove: jest.fn().mockResolvedValue({ data: null, error: null }),
})),
},
rpc: jest.fn().mockResolvedValue({ data: null, error: null }),
},
}))
// Mock de Google Generative AI
jest.mock('@google/generative-ai', () => ({
GoogleGenerativeAI: jest.fn().mockImplementation(() => ({
getGenerativeModel: jest.fn().mockReturnValue({
generateContent: jest.fn().mockResolvedValue({
response: {
text: jest.fn().mockReturnValue('Mocked AI response'),
},
}),
}),
})),
}))
// Mock de TanStack Query
jest.mock('@tanstack/react-query', () => {
const actual = jest.requireActual('@tanstack/react-query')
return {
...actual,
useQuery: jest.fn(() => ({
data: undefined,
isLoading: false,
error: null,
refetch: jest.fn(),
})),
useMutation: jest.fn(() => ({
mutate: jest.fn(),
mutateAsync: jest.fn(),
isLoading: false,
error: null,
})),
useQueryClient: jest.fn(() => ({
invalidateQueries: jest.fn(),
setQueryData: jest.fn(),
getQueryData: jest.fn(),
})),
}
})
// Mock de console para tests limpios
global.console = {
...console,
error: jest.fn(),
warn: jest.fn(),
log: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
}
// Mock de window.matchMedia (solo en entornos con window, e.g., jsdom)
if (typeof window !== 'undefined') Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
})
// Mock de ResizeObserver
global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}))
// Mock de IntersectionObserver
global.IntersectionObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}))
// Polyfill para TextEncoder/TextDecoder (requerido por lib/field-encryption en Node.js < 18)
if (typeof globalThis.TextEncoder === 'undefined') {
const { TextEncoder, TextDecoder } = require('util');
globalThis.TextEncoder = TextEncoder;
globalThis.TextDecoder = TextDecoder;
}
// Polyfill para fetch en Node.js (para tests de performance)
// Node.js 18+ tiene fetch nativo, pero Jest con jsdom puede necesitarlo
if (typeof globalThis.fetch === 'undefined' && typeof fetch !== 'undefined') {
globalThis.fetch = fetch;
}