Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for amp-facebook-comments:0.1 #34331

Merged
merged 3 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import '../amp-facebook-comments';
import {createElementWithAttributes} from '../../../../src/dom';
import {doNotLoadExternalResourcesInTest} from '../../../../testing/iframe';
import {resetServiceForTesting} from '../../../../src/service';
import {serializeMessage} from '../../../../src/3p-frame-messaging';
import {setDefaultBootstrapBaseUrlForTesting} from '../../../../src/3p-frame';

describes.realWin(
'amp-facebook-comments',
{
amp: {
extensions: ['amp-facebook-comments:0.1'],
},
},
(env) => {
let win, doc, element;
const href = 'https://cdn.ampproject.org/';

beforeEach(async function () {
win = env.win;
doc = win.document;
// Override global window here because Preact uses global `createElement`.
doNotLoadExternalResourcesInTest(window, env.sandbox);
});

it('renders', async () => {
element = createElementWithAttributes(doc, 'amp-facebook-comments', {
'data-href': href,
'height': 500,
'width': 500,
'layout': 'responsive',
});
doc.body.appendChild(element);
await element.buildInternal();
await element.layoutCallback();

const iframe = element.querySelector('iframe');
expect(iframe.src).to.equal(
'http://ads.localhost:9876/dist.3p/current/frame.max.html'
);
});

it('ensures iframe is not sandboxed in amp-facebook-comments', async () => {
element = createElementWithAttributes(doc, 'amp-facebook-comments', {
'data-href': href,
'height': 500,
'width': 500,
'layout': 'responsive',
});
doc.body.appendChild(element);
await element.buildInternal();
await element.layoutCallback();

const iframe = element.querySelector('iframe');
expect(iframe.hasAttribute('sandbox')).to.be.false;
});

it('renders amp-facebook-comments with specified locale', async () => {
element = createElementWithAttributes(doc, 'amp-facebook-comments', {
'data-href': href,
'data-locale': 'fr_FR',
'height': 500,
'width': 500,
'layout': 'responsive',
});
doc.body.appendChild(element);
await element.buildInternal();
await element.layoutCallback();

const iframe = element.querySelector('iframe');
expect(iframe.getAttribute('name')).to.contain('"locale":"fr_FR"');
});

it("container's height is changed", async () => {
const iframeSrc =
'http://ads.localhost:' +
location.port +
'/test/fixtures/served/iframe.html';
resetServiceForTesting(win, 'bootstrapBaseUrl');
setDefaultBootstrapBaseUrlForTesting(iframeSrc);

const initialHeight = 300;
element = createElementWithAttributes(doc, 'amp-facebook-comments', {
'data-href': '585110598171631616',
'height': initialHeight,
'width': 500,
'layout': 'responsive',
});
doc.body.appendChild(element);
await element.buildInternal();
await element.layoutCallback();

const impl = await element.getImpl(false);
const forceChangeHeightStub = env.sandbox.stub(impl, 'forceChangeHeight');

const mockEvent = new CustomEvent('message');
const sentinel = JSON.parse(
element.querySelector('iframe').getAttribute('name')
)['attributes']['_context']['sentinel'];
mockEvent.data = serializeMessage('embed-size', sentinel, {
'height': 1000,
});
mockEvent.source = element.querySelector('iframe').contentWindow;
win.dispatchEvent(mockEvent);
expect(forceChangeHeightStub).to.be.calledOnce.calledWith(1000);
});
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,12 @@ describes.realWin(
setDefaultBootstrapBaseUrlForTesting(iframeSrc);

const initialHeight = 300;
element = createElementWithAttributes(
win.document,
'amp-facebook-comments',
{
'data-href': '585110598171631616',
'height': initialHeight,
'width': 500,
'layout': 'responsive',
}
);
element = createElementWithAttributes(doc, 'amp-facebook-comments', {
'data-href': '585110598171631616',
'height': initialHeight,
'width': 500,
'layout': 'responsive',
});
doc.body.appendChild(element);
await waitForRender();

Expand All @@ -138,7 +134,7 @@ describes.realWin(
const mockEvent = new CustomEvent('message');
const sentinel = JSON.parse(
element.shadowRoot.querySelector('iframe').getAttribute('name')
)['attributes']['sentinel'];
)['attributes']['_context']['sentinel'];
mockEvent.data = serializeMessage('embed-size', sentinel, {
'height': 1000,
});
Expand Down