forked from Talenttrust/Talenttrust-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.ts
More file actions
77 lines (70 loc) · 2.68 KB
/
Copy pathjest.setup.ts
File metadata and controls
77 lines (70 loc) · 2.68 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
import React from 'react';
import '@testing-library/jest-dom';
import { toHaveNoViolations } from 'jest-axe';
expect.extend(toHaveNoViolations);
// Mock matchMedia (not implemented in jsdom)
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock next/link to a plain <a> to avoid intersection/prefetch behavior
jest.mock('next/link', () => {
const React = require('react');
const MockLink = ({ children, href, ...props }: any) => React.createElement('a', { href, ...props }, children);
MockLink.displayName = 'MockNextLink';
return MockLink;
});
// Mock next/navigation hooks used by app components
jest.mock('next/navigation', () => ({
usePathname: () => '/',
useRouter: () => ({ push: jest.fn(), replace: jest.fn(), prefetch: jest.fn() }),
useSearchParams: () => new URLSearchParams(),
}));
// Polyfill requestIdleCallback / cancelIdleCallback used by next's request-idle-callback
if (typeof global.requestIdleCallback === 'undefined') {
global.requestIdleCallback = (cb: any) => setTimeout(() => cb({ timeRemaining: () => 50 }), 0) as unknown as number;
global.cancelIdleCallback = (id: any) => clearTimeout(id as any);
}
// Provide a simple IntersectionObserver stub so next/use-intersection does not schedule async work
class MockIntersectionObserver {
constructor() {}
observe() {}
unobserve() {}
disconnect() {}
}
if (typeof global.IntersectionObserver === 'undefined') {
global.IntersectionObserver = MockIntersectionObserver as any;
}
// Mock localStorage
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
get length() { return Object.keys(store).length; },
key: (index: number) => Object.keys(store)[index] ?? null,
getItem: (key: string) => store[key] ?? null,
setItem: (key: string, value: string) => { store[key] = value; },
clear: () => { store = {}; },
removeItem: (key: string) => { delete store[key]; },
};
})();
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
// Global mock for WalletContext so components using useWallet work without a provider
jest.mock('@/contexts/WalletContext', () => ({
useWallet: jest.fn().mockReturnValue({
address: 'GBDGTR4S5O3K7I6E7K5QH3Y2W6Z4JFQ2X3C5V7M8N9P0Q1R2S3T4U5V6W7X',
isConnecting: false,
error: null,
connect: jest.fn(),
disconnect: jest.fn(),
}),
WalletProvider: ({ children }: { children: React.ReactNode }) => children,
}));