-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjest.setup.js
More file actions
144 lines (123 loc) · 3.36 KB
/
Copy pathjest.setup.js
File metadata and controls
144 lines (123 loc) · 3.36 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
// Setup testing environment
require('@testing-library/jest-dom');
const { TextEncoder, TextDecoder } = require('util');
// Add TextEncoder/TextDecoder to global scope for Node.js
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
// Mock environment variables
process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://test.supabase.co';
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = 'test-anon-key';
process.env.SUPABASE_SERVICE_ROLE_KEY = 'test-service-role-key';
// Mock fetch globally
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve({}),
text: () => Promise.resolve(''),
headers: {
get: (name) => {
if (name === 'content-type') return 'text/event-stream';
return null;
},
},
})
);
// Mock EventSource for SSE testing
class MockEventSource {
constructor(url) {
this.url = url;
this.readyState = 0; // CONNECTING
this.onopen = null;
this.onmessage = null;
this.onerror = null;
// Simulate connection opening
setTimeout(() => {
this.readyState = 1; // OPEN
if (this.onopen) {
this.onopen({ type: 'open' });
}
}, 10);
}
close() {
this.readyState = 2; // CLOSED
}
dispatchEvent(event) {
if (event.type === 'message' && this.onmessage) {
this.onmessage(event);
} else if (event.type === 'error' && this.onerror) {
this.onerror(event);
}
}
}
MockEventSource.CONNECTING = 0;
MockEventSource.OPEN = 1;
MockEventSource.CLOSED = 2;
global.EventSource = MockEventSource;
// Mock IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
disconnect() {}
observe() {}
unobserve() {}
};
// Mock ResizeObserver
global.ResizeObserver = class ResizeObserver {
constructor() {}
disconnect() {}
observe() {}
unobserve() {}
};
// Mock window.matchMedia
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 localStorage
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
global.localStorage = localStorageMock;
// Mock sessionStorage
const sessionStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
global.sessionStorage = sessionStorageMock;
// Suppress console.log in tests unless debugging
const originalConsoleLog = console.log;
const originalConsoleError = console.error;
const originalConsoleWarn = console.warn;
beforeEach(() => {
// Reset all mocks before each test
jest.clearAllMocks();
// Reset localStorage and sessionStorage
localStorageMock.getItem.mockClear();
localStorageMock.setItem.mockClear();
localStorageMock.removeItem.mockClear();
localStorageMock.clear.mockClear();
sessionStorageMock.getItem.mockClear();
sessionStorageMock.setItem.mockClear();
sessionStorageMock.removeItem.mockClear();
sessionStorageMock.clear.mockClear();
});
afterEach(() => {
// Clean up after each test
jest.restoreAllMocks();
});
// Increase timeout for all tests
jest.setTimeout(10000);