Skip to content

Commit

Permalink
fix: 馃悳 resize slot on adUnit resize
Browse files Browse the repository at this point in the history
fix #57
  • Loading branch information
andrepolischuk committed Oct 7, 2022
1 parent c467a78 commit 4188075
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 53 deletions.
18 changes: 9 additions & 9 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"main.umd.js": {
"bundled": 288868,
"minified": 89188,
"gzipped": 23733
"bundled": 290625,
"minified": 89746,
"gzipped": 23869
},
"main.esm.js": {
"bundled": 231019,
"minified": 97576,
"gzipped": 22527,
"bundled": 232690,
"minified": 98257,
"gzipped": 22716,
"treeshaked": {
"rollup": {
"code": 10005,
Expand All @@ -19,8 +19,8 @@
}
},
"main.cjs.js": {
"bundled": 236991,
"minified": 102496,
"gzipped": 22890
"bundled": 238662,
"minified": 103177,
"gzipped": 23076
}
}
18 changes: 18 additions & 0 deletions src/adContainer/VideoAdContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import loadScript from './helpers/loadScript';
import createAdVideoElement from './helpers/createAdVideoElement';
import createAdContainer from './helpers/createAdContainer';
import createIframe from './helpers/createIframe';
import createSlot from './helpers/createSlot';
import getContentDocument from './helpers/getContentDocument';
import unique from './helpers/unique';

Expand Down Expand Up @@ -34,6 +35,7 @@ class VideoAdContainer {

this[hidden].id = nextId();
this.element = createAdContainer();
this.slotElement = null;
this.executionContext = null;

this.isOriginalVideoElement = Boolean(videoElement);
Expand Down Expand Up @@ -75,6 +77,22 @@ class VideoAdContainer {
});
}

/**
* Adds the slot to the ad container.
*
* @param {number} width - Slot width.
* @param {number} height - Slot height.
*/
addSlot (width, height) {
if (this.isDestroyed()) {
throw new Error('VideoAdContainer has been destroyed');
}

if (!this.slotElement) {
this.slotElement = createSlot(this.element, width, height);
}
}

/**
* Destroys the VideoAdContainer.
*/
Expand Down
24 changes: 20 additions & 4 deletions src/adContainer/__tests__/VideoAdContainer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ describe('VideoAdContainer', () => {

expect(iframe).toBeInstanceOf(HTMLIFrameElement);
expect(loadScript).toHaveBeenCalledTimes(1);
expect(loadScript).toBeCalledWith(src, expect.objectContaining({
placeholder: iframeBody,
...scriptOpts
}));
expect(loadScript).toBeCalledWith(
src,
expect.objectContaining({
placeholder: iframeBody,
...scriptOpts
})
);
});

test('must reuse the iframe to add scripts', async () => {
Expand Down Expand Up @@ -115,6 +118,19 @@ describe('VideoAdContainer', () => {
});
});

test('must create a slot element and add it to the ad container', () => {
const videoAdContainer = new VideoAdContainer(placeholder);
const adContainerElement = videoAdContainer.element;

videoAdContainer.addSlot(300, 200);

expect(videoAdContainer.slotElement).toBeInstanceOf(Element);
expect(videoAdContainer.slotElement.tagName).toBe('DIV');
expect(videoAdContainer.slotElement.style.width).toBe('300px');
expect(videoAdContainer.slotElement.style.height).toBe('200px');
expect(videoAdContainer.slotElement.parentNode).toBe(adContainerElement);
});

test('destroy must remove the adContainer from the placeHolder', async () => {
const videoAdContainer = new VideoAdContainer(placeholder);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createSlot must return a slot element 1`] = `
<div
style="border: 0px; cursor: pointer; height: 200px; left: 0px; margin: 0px; padding: 0px; position: absolute; top: 0px; width: 300px;"
/>
`;
12 changes: 12 additions & 0 deletions src/adContainer/helpers/__tests__/createSlot.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import createSlot from '../createSlot';

describe('createSlot', () => {
test('must return a slot element', () => {
const slot = createSlot(document.body, 300, 200);

expect(slot).toBeInstanceOf(HTMLDivElement);
expect(slot).toMatchSnapshot();

slot.parentElement.removeChild(slot);
});
});
21 changes: 21 additions & 0 deletions src/adContainer/helpers/createSlot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const createSlot = (placeholder, width, height) => {
const slot = document.createElement('DIV');

Object.assign(slot.style, {
border: '0px',
cursor: 'pointer',
height: `${height}px`,
left: '0px',
margin: '0px',
padding: '0px',
position: 'absolute',
top: '0px',
width: `${width}px`
});

placeholder.appendChild(slot);

return slot;
};

export default createSlot;
7 changes: 7 additions & 0 deletions src/adUnit/VpaidAdUnit.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,13 @@ class VpaidAdUnit extends VideoAdUnit {
async resize (width, height, viewmode) {
await super.resize(width, height, viewmode);

if (this.isStarted() && !this.isFinished()) {
const slot = this.videoAdContainer.slotElement;

slot.style.height = `${height}px`;
slot.style.width = `${width}px`;
}

return callAndWait(this.creativeAd, resizeAd, adSizeChange, width, height, viewmode);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/adUnit/__tests__/VpaidAdUnit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('VpaidAdUnit', () => {
}
];
videoAdContainer = new VideoAdContainer(document.createElement('DIV'));
videoAdContainer.slotElement = document.createElement('DIV');
});

afterEach(() => {
Expand Down
32 changes: 15 additions & 17 deletions src/adUnit/helpers/vpaid/__tests__/initAd.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
vpaidInlineAd,
vpaidInlineParsedXML,
vastVpaidInlineXML
} from '../../../../../fixtures';
import {vpaidInlineAd, vpaidInlineParsedXML, vastVpaidInlineXML} from '../../../../../fixtures';
import VideoAdContainer from '../../../../adContainer/VideoAdContainer';
import initAd from '../initAd';
import MockVpaidCreativeAd from '../../../__tests__/MockVpaidCreativeAd';
Expand Down Expand Up @@ -59,7 +55,7 @@ describe('initAd', () => {
xmlEncoded: false
},
{
slot: expect.any(HTMLDivElement),
slot: videoAdContainer.slotElement,
videoSlot: videoAdContainer.videoElement,
videoSlotCanAutoPlay: false
}
Expand All @@ -68,16 +64,18 @@ describe('initAd', () => {
const {slot} = mockCreativeAd.initAd.mock.calls[0][5];

expect(slot).toBeInstanceOf(HTMLDivElement);
expect(slot.style).toEqual(expect.objectContaining({
border: '0px',
cursor: 'pointer',
height: '0px',
left: '0px',
margin: '0px',
padding: '0px',
position: 'absolute',
top: '0px',
width: '0px'
}));
expect(slot.style).toEqual(
expect.objectContaining({
border: '0px',
cursor: 'pointer',
height: '0px',
left: '0px',
margin: '0px',
padding: '0px',
position: 'absolute',
top: '0px',
width: '0px'
})
);
});
});
29 changes: 6 additions & 23 deletions src/adUnit/helpers/vpaid/initAd.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
import {getCreativeData} from '../../../vastSelectors';
import viewmode from './viewmode';

const createSlot = (placeholder, width, height) => {
const slot = document.createElement('DIV');

Object.assign(slot.style, {
border: '0px',
cursor: 'pointer',
height: `${height}px`,
left: '0px',
margin: '0px',
padding: '0px',
position: 'absolute',
top: '0px',
width: `${width}px`
});

placeholder.appendChild(slot);

return slot;
};
const initAd = (creativeAd, videoAdContainer, vastChain) => {
const placeholder = videoAdContainer.element;
const {width, height} = placeholder.getBoundingClientRect();
const {width, height} = videoAdContainer.element.getBoundingClientRect();
const mode = viewmode(width, height);
const desiredBitrate = -1;
const creativeData = getCreativeData(vastChain[0].XML);

videoAdContainer.addSlot(width, height);

const environmentVars = {
slot: createSlot(placeholder, width, height),
slot: videoAdContainer.slotElement,
videoSlot: videoAdContainer.videoElement,
videoSlotCanAutoPlay: videoAdContainer.isOriginalVideoElement
};
const creativeData = getCreativeData(vastChain[0].XML);

creativeAd.initAd(width, height, mode, desiredBitrate, creativeData, environmentVars);
};
Expand Down

0 comments on commit 4188075

Please sign in to comment.