-
Notifications
You must be signed in to change notification settings - Fork 42
/
jest.setup.js
77 lines (64 loc) · 2.09 KB
/
jest.setup.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* eslint-disable no-unused-expressions */
import mockDevices from './src/mockDevices';
// Mock Web Media APIs
global.MediaStream = jest.fn(function MockMediaStream(tracksOrStream = []) {
let tracks;
if (tracksOrStream instanceof global.MediaStream) {
tracks = [...tracksOrStream.getTracks()];
} else {
tracks = tracksOrStream;
}
for (const track of tracks) {
if (!('enabled' in track)) {
track.enabled = true;
}
if (!('stop' in track)) {
Object.defineProperty(track, 'stop', {
value: jest.fn(),
enumerable: false,
});
}
}
return Object.assign(this, {
getTracks: jest.fn(() => tracks),
getAudioTracks: jest.fn(() => tracks.filter((track) => track.kind === 'audio')),
getVideoTracks: jest.fn(() => tracks.filter((track) => track.kind === 'video')),
removeTrack: jest.fn((toRemove) => tracks.filter((track) => track !== toRemove)),
});
});
global.navigator.mediaDevices = {
enumerateDevices: () => Promise.resolve(mockDevices),
getDisplayMedia: () => Promise.resolve(new MediaStream()),
};
expect.extend({
/**
* Custom jest matcher that checks that two media streams have the same tracks
*
* @param {MediaStream} received The received media stream object
* @param {MediaStream} expected The expected media stream object
* @returns {object} Match result
*/
toMatchMediaStream(received, expected) {
const options = {
comment: 'Object.is equality',
isNot: this.isNot,
promise: this.promise,
};
const pass = this.equals(received && received.getTracks(), expected && expected.getTracks());
const message = () => `${this.utils.matcherHint('toMatchMediaStream', undefined, undefined, options)}\n
Expected: ${this.utils.printExpected(expected && expected.getTracks())}
Received: ${this.utils.printReceived(received && received.getTracks())}`;
return {
actual: received,
message,
pass,
};
},
});
global.document.createElement = (type) => {
const elem = {};
if (type === 'audio' || type === 'video') {
elem.setSinkId = () => {};
}
return elem;
};