Skip to content

Commit

Permalink
fix: 🐜 finish adUnit on adStopped evt
Browse files Browse the repository at this point in the history
  • Loading branch information
carpasse committed Jan 11, 2019
1 parent bf26782 commit f3b1a21
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/adUnit/VpaidAdUnit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import loadCreative from './helpers/vpaid/loadCreative';
import {
adLoaded,
adStarted,
adStopped,
adPlaying,
adPaused,
startAd,
Expand Down Expand Up @@ -163,6 +164,14 @@ class VpaidAdUnit extends VideoAdUnit {
type: creativeView
});
},
[adStopped]: () => {
this.emit(adStopped, {
adUnit: this,
type: adStopped
});

this[_protected].finish();
},
[adUserAcceptInvitation]: () => {
this.emit(acceptInvitation, {
adUnit: this,
Expand Down Expand Up @@ -412,12 +421,17 @@ class VpaidAdUnit extends VideoAdUnit {
*
* @throws if ad unit is finished.
*/
cancel () {
async cancel () {
this[_protected].throwIfFinished();

this.creativeAd[stopAd]();
try {
const adStoppedPromise = waitFor(this.creativeAd, adStopped, 3000);

this[_protected].finish();
this.creativeAd[stopAd]();
await adStoppedPromise;
} catch (error) {
this[_protected].finish();
}
}

/**
Expand Down
57 changes: 53 additions & 4 deletions src/adUnit/__tests__/VpaidAdUnit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ describe('VpaidAdUnit', () => {
mockCreativeAd.emit(adStarted);
});

mockCreativeAd.stopAd.mockImplementationOnce(() => {
mockCreativeAd.emit(adStopped);
});

loadCreative.mockReturnValue(Promise.resolve(mockCreativeAd));
retrieveIcons.mockReturnValue(null);

Expand Down Expand Up @@ -304,6 +308,10 @@ describe('VpaidAdUnit', () => {
mockCreativeAd.emit(adStarted);
});

mockCreativeAd.stopAd.mockImplementationOnce(() => {
mockCreativeAd.emit(adStopped);
});

mockCreativeAd.resizeAd.mockImplementationOnce(() => {
mockCreativeAd.emit(adSizeChange);
});
Expand All @@ -330,7 +338,7 @@ describe('VpaidAdUnit', () => {

await adUnit.start();

adUnit.cancel();
await adUnit.cancel();

expect(mockRemoveIcons).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -466,7 +474,7 @@ describe('VpaidAdUnit', () => {

await adUnit.start();
expect(mockDrawIcons).toHaveBeenCalledTimes(1);
adUnit.cancel();
await adUnit.cancel();

expect(mockDrawIcons).toHaveBeenCalledTimes(1);

Expand Down Expand Up @@ -559,11 +567,18 @@ describe('VpaidAdUnit', () => {
});

describe('cancel', () => {
jest.useFakeTimers();

test('must throw if the adUnit is finished', async () => {
expect.assertions(1);
await adUnit.start();
await adUnit.cancel();

expect(() => adUnit.cancel()).toThrow('VideoAdUnit is finished');
try {
await adUnit.cancel();
} catch (error) {
expect(error.message).toBe('VideoAdUnit is finished');
}
});

test('must call stopAd and finish the adUnit', async () => {
Expand All @@ -573,6 +588,22 @@ describe('VpaidAdUnit', () => {
expect(mockCreativeAd.stopAd).toHaveBeenCalledTimes(1);
expect(adUnit.isFinished()).toBe(true);
});

it('must finish the adUnit if the creative does not emit adStopped event after some time', async () => {
mockCreativeAd.stopAd = jest.fn();
mockCreativeAd.stopAd.mockImplementationOnce(() => {
// empty on purpose
});

await adUnit.start();
const cancelPromise = adUnit.cancel();

expect(mockCreativeAd.stopAd).toHaveBeenCalledTimes(1);
expect(adUnit.isFinished()).toBe(false);
jest.runOnlyPendingTimers();
await cancelPromise;
expect(adUnit.isFinished()).toBe(true);
});
});

describe('onFinish', () => {
Expand Down Expand Up @@ -610,6 +641,20 @@ describe('VpaidAdUnit', () => {
expect(callback).toHaveBeenCalledTimes(1);
});

test('must be called once the ad unit stops', async () => {
const callback = jest.fn();

adUnit.onFinish(callback);

await adUnit.start();

expect(callback).not.toHaveBeenCalled();

adUnit.creativeAd.emit(adStopped);

expect(callback).toHaveBeenCalledTimes(1);
});

test('must be called if the user closes the ad unit', async () => {
const callback = jest.fn();

Expand Down Expand Up @@ -687,6 +732,10 @@ describe('VpaidAdUnit', () => {
mockCreativeAd.emit(adStarted);
});

mockCreativeAd.stopAd.mockImplementationOnce(() => {
mockCreativeAd.emit(adStopped);
});

loadCreative.mockReturnValue(Promise.resolve(mockCreativeAd));
adUnit = new VpaidAdUnit(vpaidChain, videoAdContainer);
});
Expand Down Expand Up @@ -814,7 +863,7 @@ describe('VpaidAdUnit', () => {
adUnit.creativeAd.emit(adPlaying);
expect(adUnit.paused()).toBe(false);

adUnit.cancel();
await adUnit.cancel();
expect(adUnit.paused()).toBe(true);
});
});
Expand Down

0 comments on commit f3b1a21

Please sign in to comment.