Skip to content
This repository has been archived by the owner on Nov 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #48 from Financial-Times/matth/fix-ended
Browse files Browse the repository at this point in the history
Fix autoplay option adding ended listener when it shouldn't
  • Loading branch information
i-like-robots authored Sep 15, 2016
2 parents 33e5cdc + 55216f0 commit fe46ed4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/js/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ class Playlist {
const currentId = opts.player.videoData ? opts.player.videoData.id : opts.player.opts.id;
this.currentIndex = currentId ? opts.queue.indexOf(currentId.toString()) : -1;

this.opts.player.containerEl.addEventListener('ended', this.next.bind(this), true);

this.cache = {};

if (this.opts.autoplay && this.currentIndex === -1) {
this.next();
if (this.opts.autoplay) {
this.opts.player.containerEl.addEventListener('ended', this.next.bind(this), true);

if ( this.currentIndex === -1) {
this.next();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/scss/_info.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
font-weight: 600;

&:empty {
display: none;
display: none !important;
}
}

Expand Down
14 changes: 12 additions & 2 deletions test/playlist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,27 @@ describe('Playlist', () => {
sinon.assert.calledOnce(player.update);
});

it('listens for the video to end to trigger the next in the queue', () => {
it('listens for the video to end to trigger the next in the queue when autoplay is set', () => {
player.videoData = { id: 'bar' };

new Subject({ player, queue });
new Subject({ player, queue, autoplay: true });

// no DOM so trigger this on the listener directly
player.containerEl.dispatchEvent(new CustomEvent('ended', { bubbles: false }));

sinon.assert.calledOnce(player.update);
sinon.assert.calledWith(player.update, sinon.match({ id: 'baz' }));
});

it('doesn\'t listen for the video to end when autoplay is not set', () => {
player.videoData = { id: 'bar' };

new Subject({ player, queue, autoplay: false });

player.containerEl.dispatchEvent(new CustomEvent('ended', { bubbles: false }));

sinon.assert.notCalled(player.update);
});
});

describe('#next', () => {
Expand Down

0 comments on commit fe46ed4

Please sign in to comment.