From 7ffc9a0d1c3610a45f9e0f38bb296cf19f00c1a2 Mon Sep 17 00:00:00 2001 From: Carlos Serrano Date: Tue, 27 Nov 2018 13:28:01 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9C=20destroy=20video=20ad=20co?= =?UTF-8?q?ntainer=20on=20run=20finish?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/runner/__tests__/run.spec.js | 25 +++++++++++++++++++++++++ src/runner/run.js | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/src/runner/__tests__/run.spec.js b/src/runner/__tests__/run.spec.js index ecd15107..44fcf5fb 100644 --- a/src/runner/__tests__/run.spec.js +++ b/src/runner/__tests__/run.spec.js @@ -66,6 +66,31 @@ describe('run', () => { expect(startVideoAd).toHaveBeenCalledWith(vastAdChain, adContainer, options); }); + test('must destroy the ad container on adUnit finish', async () => { + adContainer.destroy = jest.fn(); + + expect(await run(vastAdChain, placeholder, options)).toBe(adUnit); + expect(adContainer.destroy).toHaveBeenCalledTimes(0); + + adUnit.cancel(); + expect(adContainer.destroy).toHaveBeenCalledTimes(1); + }); + + test('must propagate adUnit creation errors', async () => { + expect.assertions(1); + const testError = new Error('Ad container creation error'); + + createVideoAdContainer.mockImplementation(() => { + throw testError; + }); + + try { + await run(vastAdChain, placeholder, options); + } catch (error) { + expect(error).toBe(testError); + } + }); + test('must destroy the adContainer if there is a problem starting the adUnit', async () => { const adUnitError = new Error('boom'); diff --git a/src/runner/run.js b/src/runner/run.js index 3affef03..c27fb376 100644 --- a/src/runner/run.js +++ b/src/runner/run.js @@ -69,6 +69,10 @@ const run = async (vastChain, placeholder, options) => { const adUnit = await adUnitPromise; + adUnit.onFinish(() => { + videoAdContainer.destroy(); + }); + return adUnit; } catch (error) { if (videoAdContainer) {