forked from webscopeio/react-textarea-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupJest.js
54 lines (49 loc) · 1.19 KB
/
setupJest.js
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
/**
* Global Jest setup for React Testing Library
*/
import "@babel/polyfill";
import '@testing-library/jest-dom';
// Mock MutationObserver
global.MutationObserver = class {
constructor(callback) {
this.callback = callback;
}
disconnect() {}
observe(element, initObject) {}
takeRecords() {
return [];
}
};
// Polyfill for scrollIntoView
window.HTMLElement.prototype.scrollIntoView = function() {};
// Mock for document.getSelection
document.getSelection = () => {
return {
removeAllRanges: jest.fn(),
addRange: jest.fn(),
getRangeAt: jest.fn(),
toString: jest.fn(),
anchorNode: document.createElement('div'),
anchorOffset: 0,
focusNode: document.createElement('div'),
focusOffset: 0,
isCollapsed: true,
type: '',
};
};
// Set up React 18 test environment
window.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
// Mock for createPortal which is used in React 18
if (typeof window !== 'undefined') {
jest.mock('react-dom', () => {
const original = jest.requireActual('react-dom');
return {
...original,
createPortal: (node) => node,
};
});
}