Skip to content

Commit

Permalink
fix(viewer): add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Sep 11, 2019
1 parent 33a3e54 commit 583ba15
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib/viewers/media/MP3Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
13 changes: 9 additions & 4 deletions src/lib/viewers/media/MediaBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}

Expand Down
13 changes: 13 additions & 0 deletions src/lib/viewers/media/__tests__/MP3Viewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
});

0 comments on commit 583ba15

Please sign in to comment.