forked from webex/sdk-component-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.setup.js
60 lines (51 loc) · 1.57 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
// Mock Web Media APIs
global.MediaStream = jest.fn(function(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)),
});
});
expect.extend({
/**
* Custom jest matcher that checks that two media streams have the same tracks
*
* @param {MediaStream} received
* @param {MediaStream} expected
* @returns {object}
*/
toMatchMediaStream(received, expected) {
const options = {
comment: 'Object.is equality',
isNot: this.isNot,
promise: this.promise,
};
const pass = this.equals(received.getTracks(), 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,
};
},
});