Skip to content

Commit f9183b9

Browse files
committed
test: add unit test for shared-audio-context.js
1 parent 39ceac4 commit f9183b9

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/lib/audio/shared-audio-context.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import StartAudioContext from 'startaudiocontext';
2-
import bowser from 'bowser';
1+
import StartAudioContext from "startaudiocontext";
2+
import bowser from "bowser";
33

44
let AUDIO_CONTEXT;
55

@@ -8,7 +8,9 @@ if (!bowser.msie) {
88
* AudioContext can be initialized only when user interaction event happens
99
*/
1010
const event =
11-
typeof document.ontouchend === 'undefined' ? 'mouseup' : 'touchend';
11+
typeof document.ontouchstart === "undefined"
12+
? "mousedown"
13+
: "touchstart";
1214
const initAudioContext = () => {
1315
document.removeEventListener(event, initAudioContext);
1416
AUDIO_CONTEXT = new (window.AudioContext ||

test/unit/util/audio-context.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import "web-audio-test-api";
2+
import SharedAudioContext from "../../../src/lib/audio/shared-audio-context";
3+
4+
describe("Shared Audio Context", () => {
5+
const audioContext = new AudioContext();
6+
7+
test("returns empty object without user gesture", () => {
8+
const sharedAudioContext = new SharedAudioContext();
9+
expect(sharedAudioContext).toMatchObject({});
10+
});
11+
12+
test("returns AudioContext when mousedown is triggered", () => {
13+
const sharedAudioContext = new SharedAudioContext();
14+
const event = new Event("mousedown");
15+
document.dispatchEvent(event);
16+
expect(sharedAudioContext).toMatchObject(audioContext);
17+
});
18+
19+
test("returns AudioContext when touchstart is triggered", () => {
20+
const sharedAudioContext = new SharedAudioContext();
21+
const event = new Event("touchstart");
22+
document.dispatchEvent(event);
23+
expect(sharedAudioContext).toMatchObject(audioContext);
24+
});
25+
});

0 commit comments

Comments
 (0)