Repository navigation
Expand file tree
/
Copy pathjest.setup.js
More file actions
84 lines (74 loc) · 2.12 KB
/
Copy pathjest.setup.js
File metadata and controls
84 lines (74 loc) · 2.12 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
// Mock TextEncoder and TextDecoder for JSDOM
global.TextEncoder = require('util').TextEncoder;
global.TextDecoder = require('util').TextDecoder;
// Mock global objects needed for tests
global.chrome = {
runtime: {
getURL: jest.fn((path) => `http://localhost/mock-extension/${path}`),
onMessage: {
addListener: jest.fn(),
},
sendMessage: jest.fn(),
},
storage: {
local: {
get: jest.fn(),
set: jest.fn(),
},
},
tabs: {
query: jest.fn(),
create: jest.fn(),
sendMessage: jest.fn(),
},
scripting: {
executeScript: jest.fn(),
},
};
// Mock fetch
global.fetch = jest.fn();
// Mock window.supabase
global.supabase = {
createClient: jest.fn(),
};
// Mock Date.now for consistent timestamps in tests
const mockNow = 1625097600000; // 2021-07-01T00:00:00.000Z
global.Date.now = jest.fn(() => mockNow);
// Mock postMessage and addEventListener for bridge messaging
window.postMessage = jest.fn();
const originalAddEventListener = window.addEventListener;
window.addEventListener = jest.fn().mockImplementation((event, handler) => {
if (event !== 'message') {
return originalAddEventListener(event, handler);
}
});
// Helper to create DOM fixtures
global.setupTestDOM = () => {
document.body.innerHTML = '';
const container = document.createElement('div');
document.body.appendChild(container);
return container;
};
// Helper to simulate message events
global.simulateMessageEvent = (data) => {
const messageEvent = new MessageEvent('message', {
source: window,
data,
});
// Find and call the message handler directly
const calls = window.addEventListener.mock.calls;
const messageHandlers = calls
.filter(call => call[0] === 'message')
.map(call => call[1]);
messageHandlers.forEach(handler => handler(messageEvent));
};
// Mock dynamic imports
jest.mock('module', () => ({
...jest.requireActual('module'),
import: jest.fn().mockImplementation((path) => {
if (path.includes('supabase.js')) {
return Promise.resolve(require('./__mocks__/supabase.js'));
}
return Promise.reject(new Error(`Module not mocked: ${path}`));
}),
}));