Skip to content

Commit 25219f1

Browse files
authored
Merge pull request #100 from micnews/fix-double-preroll
Fix preroll running twice
2 parents b88a4e2 + 5ffc69a commit 25219f1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/create-event-handlers/on-before-play.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function onBeforePlay(event, player) {
22
const currentVideo = player.getPlaylistItem();
33

4-
if (!this.state.hasPlayed && typeof this.props.generatePrerollUrl === 'function') {
4+
if (!this.state.hasPlayed && !this.state.adHasPlayed && typeof this.props.generatePrerollUrl === 'function') {
55
player.playAd(this.props.generatePrerollUrl(currentVideo));
66
}
77
}

test/on-before-play.text.js renamed to test/on-before-play.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,38 @@ test('eventHandlers.onBeforePlay() with preroll prop and player has played', (t)
8080
t.end();
8181
});
8282

83+
test('eventHandlers.onBeforePlay() with preroll prop and player has not played but ad has player', (t) => {
84+
let generatePrerollUrlCalled = false;
85+
let playAdCalled = false;
86+
87+
const mockCurrentVideo = 'i am the current video';
88+
89+
const mockComponent = new MockComponent({
90+
initialState: { hasPlayed: false, adHasPlayed: true },
91+
generatePrerollUrl() {
92+
generatePrerollUrlCalled = true;
93+
},
94+
});
95+
96+
const mockJWPlayerInstance = {
97+
getPlaylistItem() {
98+
return mockCurrentVideo;
99+
},
100+
playAd() {
101+
playAdCalled = true;
102+
},
103+
};
104+
105+
const mockEvent = 'event';
106+
const onBeforePlay = createEventHandlers(mockComponent).onBeforePlay;
107+
108+
t.doesNotThrow(onBeforePlay.bind(null, mockEvent, mockJWPlayerInstance), 'it runs without error');
109+
t.notOk(generatePrerollUrlCalled, 'it does not call the supplied generatePrerollUrl() prop');
110+
t.notOk(playAdCalled, 'it does not call playAd() on the supplied player instance');
111+
112+
t.end();
113+
});
114+
83115
test('eventHandlers.onBeforePlay() without preroll prop and player has not played', (t) => {
84116
let playAdCalled = false;
85117

0 commit comments

Comments
 (0)