From 583ba1592efc3d387600902f3f1886ba33e1752b Mon Sep 17 00:00:00 2001 From: Mingze Xiao Date: Tue, 10 Sep 2019 21:01:33 -0700 Subject: [PATCH] fix(viewer): add test --- src/lib/viewers/media/MP3Viewer.js | 2 +- src/lib/viewers/media/MediaBaseViewer.js | 13 +++++++++---- src/lib/viewers/media/__tests__/MP3Viewer-test.js | 13 +++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/lib/viewers/media/MP3Viewer.js b/src/lib/viewers/media/MP3Viewer.js index a518bb6b8c..393ddf042c 100644 --- a/src/lib/viewers/media/MP3Viewer.js +++ b/src/lib/viewers/media/MP3Viewer.js @@ -46,9 +46,9 @@ class MP3Viewer extends MediaBaseViewer { const autoPlayPromise = this.mediaEl.play(); if (autoPlayPromise && typeof autoPlayPromise.then === 'function') { - this.handleRate(); return autoPlayPromise .then(() => { + this.handleRate(); this.handleVolume(); }) .catch(() => { diff --git a/src/lib/viewers/media/MediaBaseViewer.js b/src/lib/viewers/media/MediaBaseViewer.js index 0a077fc6ec..8b2271467f 100644 --- a/src/lib/viewers/media/MediaBaseViewer.js +++ b/src/lib/viewers/media/MediaBaseViewer.js @@ -317,17 +317,22 @@ class MediaBaseViewer extends BaseViewer { const autoPlayPromise = this.mediaEl.play(); if (autoPlayPromise && typeof autoPlayPromise.then === 'function') { - this.handleRate(); return autoPlayPromise .then(() => { + this.handleRate(); this.handleVolume(); }) .catch(() => { // Auto-play was prevented, try muted play this.setVolume(0); - this.mediaEl.play().catch(() => { - this.mediaEl.pause(); - }); + this.mediaEl + .play() + .then(() => { + this.handleRate(); + }) + .catch(() => { + this.mediaEl.pause(); + }); }); } diff --git a/src/lib/viewers/media/__tests__/MP3Viewer-test.js b/src/lib/viewers/media/__tests__/MP3Viewer-test.js index e38edbc1ef..4edf691bf7 100644 --- a/src/lib/viewers/media/__tests__/MP3Viewer-test.js +++ b/src/lib/viewers/media/__tests__/MP3Viewer-test.js @@ -74,4 +74,17 @@ describe('lib/viewers/media/MP3Viewer', () => { mp3.loadUI(); }); }); + + describe('autoplay()', () => { + it('should pause autoplay if the promise is rejected', done => { + mp3.mediaEl = { + play: sandbox.stub().returns(Promise.reject()), + pause: sandbox.stub(), + }; + mp3.autoplay().then(() => { + expect(mp3.mediaEl.pause).to.be.called; + done(); + }); + }); + }); });