-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathjest.setup.js
More file actions
136 lines (125 loc) · 3.91 KB
/
jest.setup.js
File metadata and controls
136 lines (125 loc) · 3.91 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
/* istanbul ignore file */
require("jest-fetch-mock").enableMocks();
require("@testing-library/jest-dom");
// ResizeObserver is not in JSDOM; required by many layout components.
global.ResizeObserver = class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
};
// IntersectionObserver is not in JSDOM; required by visibility/lazy-load logic.
global.IntersectionObserver = class IntersectionObserver {
constructor(callback) {
this.callback = callback;
}
observe() {}
unobserve() {}
disconnect() {}
};
// matchMedia is not in JSDOM; required by responsive hooks and Konva.
Object.defineProperty(window, "matchMedia", {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Canvas 2D context is not fully implemented in JSDOM; required by Konva/canvas usage.
HTMLCanvasElement.prototype.getContext = jest.fn().mockImplementation((contextType) => {
if (contextType === "2d") {
return {
fillRect: jest.fn(),
clearRect: jest.fn(),
getImageData: jest.fn(() => ({ data: new Array(4) })),
putImageData: jest.fn(),
createImageData: jest.fn(() => []),
setTransform: jest.fn(),
drawImage: jest.fn(),
save: jest.fn(),
restore: jest.fn(),
beginPath: jest.fn(),
moveTo: jest.fn(),
lineTo: jest.fn(),
closePath: jest.fn(),
stroke: jest.fn(),
translate: jest.fn(),
scale: jest.fn(),
rotate: jest.fn(),
arc: jest.fn(),
fill: jest.fn(),
measureText: jest.fn(() => ({ width: 0 })),
transform: jest.fn(),
rect: jest.fn(),
clip: jest.fn(),
};
}
return null;
});
// Mock HTMLMediaElement data and methods not implemented by jsdom.
window.HTMLMediaElement.prototype._mock = {
paused: true,
duration: Number.NaN,
_loaded: false,
// Emulates the media file loading
_load: function mediaInit(media) {
media.dispatchEvent(new Event("loadedmetadata"));
media.dispatchEvent(new Event("loadeddata"));
media.dispatchEvent(new Event("canplaythrough"));
},
// Reset to the initial state
_resetMock: function resetMock(media) {
media._mock = Object.assign({}, window.HTMLMediaElement.prototype._mock);
},
_supportsTypes: ["video/mp4", "video/webm", "video/ogg", "audio/mp3", "audio/webm", "audio/ogg", "audio/wav"],
};
// Get "paused" value, it is automatically set to true / false when we play / pause the media.
Object.defineProperty(window.HTMLMediaElement.prototype, "paused", {
get() {
return this._mock.paused;
},
configurable: true,
});
// Get and set media duration
Object.defineProperty(window.HTMLMediaElement.prototype, "duration", {
get() {
return this._mock.duration;
},
set(value) {
// Reset the mock state to initial (paused) when we set the duration.
this._mock._resetMock(this);
this._mock.duration = value;
},
configurable: true,
});
// Load the media file
window.HTMLMediaElement.prototype.load = function loadMock() {
if (!this._mock._loaded) {
// emulate the media file load and metadata initialization
this._mock._load(this);
}
this.dispatchEvent(new Event("load"));
};
// Start the playback.
window.HTMLMediaElement.prototype.play = function playMock() {
if (!this._mock._loaded) {
// emulate the media file load and metadata initialization
this._mock._load(this);
}
this._mock.paused = false;
this.dispatchEvent(new Event("play"));
};
// Pause the playback
window.HTMLMediaElement.prototype.pause = function pauseMock() {
this._mock.paused = true;
this.dispatchEvent(new Event("pause"));
};
// Can play the media file
window.HTMLMediaElement.prototype.canPlayType = function canPlayTypeMock(type) {
return this._mock._supportsTypes.includes(type) ? "maybe" : "";
};