Skip to content

Commit

Permalink
[amp-delight-player] Add more unit tests to improve coverage. (#32950)
Browse files Browse the repository at this point in the history
Fixes #32949 
This change increases code coverage in `amp-delight-player` from 47.67% to 66.28%
  • Loading branch information
rbeckthomas committed Mar 2, 2021
1 parent b2b920e commit 131deac
Showing 1 changed file with 191 additions and 112 deletions.
303 changes: 191 additions & 112 deletions extensions/amp-delight-player/0.1/test/test-amp-delight-player.js
Expand Up @@ -43,134 +43,213 @@ describes.realWin(
}

async function getDelightPlayer(attributes) {
const delight = doc.createElement('amp-delight-player');
const player = doc.createElement('amp-delight-player');
for (const key in attributes) {
delight.setAttribute(key, attributes[key]);
player.setAttribute(key, attributes[key]);
}
delight.setAttribute('width', '640');
delight.setAttribute('height', '360');
delight.setAttribute('layout', 'responsive');
doc.body.appendChild(delight);
const impl = await delight.getImpl(false);
player.setAttribute('width', '640');
player.setAttribute('height', '360');
player.setAttribute('layout', 'responsive');
doc.body.appendChild(player);
const impl = await player.getImpl(false);
impl.baseURL_ =
// Serve a blank page, since these tests don't require an actual page.
// hash # at the end so path is not affected by param concat
`http://localhost:${location.port}/test/fixtures/served/blank.html#`;
return delight
return player
.buildInternal()
.then(() => delight.layoutCallback())
.then(() => delight);
.then(() => player.layoutCallback())
.then(() => player);
}

it('renders', () => {
return getDelightPlayer({
'data-content-id': '-LLoCCZqWi18O73b6M0w',
}).then(async (delight) => {
const impl = await delight.getImpl(false);
const iframe = delight.querySelector('iframe');
describe('rendering', async () => {
it('renders', () => {
return getDelightPlayer({
'data-content-id': '-LLoCCZqWi18O73b6M0w',
}).then(async (player) => {
const impl = await player.getImpl(false);
const iframe = player.querySelector('iframe');
expect(iframe).to.not.be.null;
expect(iframe.tagName).to.equal('IFRAME');
expect(iframe.src).to.equal(
`${impl.baseURL_}/player/-LLoCCZqWi18O73b6M0w?amp=1`
);
expect(iframe.allow).to.equal('vr');
expect(iframe.className).to.match(/i-amphtml-fill-content/);
});
});

it('fails if no content is specified', () => {
return allowConsoleError(() => {
return getDelightPlayer({
'data-content-id': '',
}).should.eventually.be.rejectedWith(
/The data-content-id attribute is required/
);
});
});

it('removes iframe after unlayoutCallback', async () => {
const player = await getDelightPlayer({
'data-content-id': '-LLoCCZqWi18O73b6M0w',
});
const impl = await player.getImpl(false);
const iframe = player.querySelector('iframe');
expect(iframe).to.not.be.null;
expect(iframe.tagName).to.equal('IFRAME');
expect(iframe.src).to.equal(
`${impl.baseURL_}/player/-LLoCCZqWi18O73b6M0w?amp=1`
);
expect(iframe.allow).to.equal('vr');
expect(iframe.className).to.match(/i-amphtml-fill-content/);
impl.unlayoutCallback();
expect(player.querySelector('iframe')).to.be.null;
expect(impl.iframe_).to.be.null;
});
});

it('fails if no content is specified', () => {
return allowConsoleError(() => {
it('should forward events', () => {
return getDelightPlayer({
'data-content-id': '',
}).should.eventually.be.rejectedWith(
/The data-content-id attribute is required/
);
'data-content-id': '-LLoCCZqWi18O73b6M0w',
}).then((player) => {
return Promise.resolve()
.then(async () => {
const p = listenOncePromise(player, VideoEvents.LOAD);
await fakePostMessage(player, {
type: 'x-dl8-to-parent-ready',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(player, VideoEvents.PLAYING);
await fakePostMessage(player, {
type: 'x-dl8-to-parent-playing',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(player, VideoEvents.PAUSE);
await fakePostMessage(player, {
type: 'x-dl8-to-parent-paused',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(player, VideoEvents.MUTED);
await fakePostMessage(player, {
type: 'x-dl8-to-parent-muted',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(player, VideoEvents.UNMUTED);
await fakePostMessage(player, {
type: 'x-dl8-to-parent-unmuted',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(player, VideoEvents.ENDED);
await fakePostMessage(player, {
type: 'x-dl8-to-parent-ended',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(player, VideoEvents.AD_START);
await fakePostMessage(player, {
type: 'x-dl8-to-parent-amp-ad-start',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(player, VideoEvents.AD_END);
await fakePostMessage(player, {
type: 'x-dl8-to-parent-amp-ad-end',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(player, VideoEvents.CUSTOM_TICK);
await fakePostMessage(player, {
type: 'x-dl8-to-parent-amp-custom-tick',
payload: {
type: 'delight-test-event',
testVar: 42,
},
});
const {data} = await p;
expect(data.eventType).to.equal(
'video-custom-delight-test-event'
);
expect(data.vars.testVar).to.equal(42);
return p;
});
});
});
});

it('should forward events', () => {
return getDelightPlayer({
'data-content-id': '-LLoCCZqWi18O73b6M0w',
}).then((delight) => {
return Promise.resolve()
.then(async () => {
const p = listenOncePromise(delight, VideoEvents.LOAD);
await fakePostMessage(delight, {
type: 'x-dl8-to-parent-ready',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(delight, VideoEvents.PLAYING);
await fakePostMessage(delight, {
type: 'x-dl8-to-parent-playing',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(delight, VideoEvents.PAUSE);
await fakePostMessage(delight, {
type: 'x-dl8-to-parent-paused',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(delight, VideoEvents.MUTED);
await fakePostMessage(delight, {
type: 'x-dl8-to-parent-muted',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(delight, VideoEvents.UNMUTED);
await fakePostMessage(delight, {
type: 'x-dl8-to-parent-unmuted',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(delight, VideoEvents.ENDED);
await fakePostMessage(delight, {
type: 'x-dl8-to-parent-ended',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(delight, VideoEvents.AD_START);
await fakePostMessage(delight, {
type: 'x-dl8-to-parent-amp-ad-start',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(delight, VideoEvents.AD_END);
await fakePostMessage(delight, {
type: 'x-dl8-to-parent-amp-ad-end',
payload: {},
});
return p;
})
.then(async () => {
const p = listenOncePromise(delight, VideoEvents.CUSTOM_TICK);
await fakePostMessage(delight, {
type: 'x-dl8-to-parent-amp-custom-tick',
payload: {
type: 'delight-test-event',
testVar: 42,
},
});
const {data} = await p;
expect(data.eventType).to.equal('video-custom-delight-test-event');
expect(data.vars.testVar).to.equal(42);
return p;
});
describe('methods', async () => {
let impl;
beforeEach(async () => {
const player = await getDelightPlayer({
'data-content-id': '-LLoCCZqWi18O73b6M0w',
});
impl = await player.getImpl(false);
});

it('is interactive', () => {
expect(impl.isInteractive()).to.be.true;
});

it('can play', () => {
const spy = env.sandbox.spy(impl, 'sendCommand_');
impl.play();
expect(spy).to.be.calledWith('x-dl8-to-iframe-play');
});

it('can pause', () => {
const spy = env.sandbox.spy(impl, 'sendCommand_');
impl.pause();
expect(spy).to.be.calledWith('x-dl8-to-iframe-pause');
});

it('can mute', () => {
env.sandbox.spy(impl, 'sendCommand_');
impl.mute();
expect(impl.sendCommand_).calledWith('x-dl8-to-iframe-mute');
});

it('can unmute', () => {
env.sandbox.spy(impl, 'sendCommand_');
impl.unmute();
expect(impl.sendCommand_).calledWith('x-dl8-to-iframe-unmute');
});

it('can enter fullscreen', () => {
env.sandbox.spy(impl, 'sendCommand_');
impl.fullscreenEnter();
expect(impl.sendCommand_).calledWith(
'x-dl8-to-iframe-enter-fullscreen'
);
});

it('can exit fullscreen', () => {
env.sandbox.spy(impl, 'sendCommand_');
impl.fullscreenExit();
expect(impl.sendCommand_).calledWith('x-dl8-to-iframe-exit-fullscreen');
expect(impl.isFullscreen()).to.be.false;
});

it('toggles controls', () => {
const spy = env.sandbox.stub(impl, 'sendCommand_');
impl.showControls();
expect(spy).calledWith('x-dl8-to-iframe-enable-interface');
impl.hideControls();
expect(spy).calledWith('x-dl8-to-iframe-disable-interface');
impl.showControls();
expect(spy).calledWith('x-dl8-to-iframe-enable-interface');
});
});
}
Expand Down

0 comments on commit 131deac

Please sign in to comment.