text: /**
   * 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-like';
  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';
  import {toggleExperiment} from '../../../../src/experiments';
  import {waitFor} from '../../../../testing/test-helper';
  
  describes.realWin(
    'amp-facebook-like',
    {
      amp: {
        extensions: ['amp-facebook-like:1.0'],
      },
    },
    (env) => {
      let win, doc, element;
  
      const waitForRender = async () => {
        await element.buildInternal();
        const loadPromise = element.layoutCallback();
        const shadow = element.shadowRoot;
        await waitFor(() => shadow.querySelector('iframe'), 'iframe mounted');
        await loadPromise;
      };
  
      beforeEach(async function () {
        win = env.win;
        doc = win.document;
        toggleExperiment(win, 'bento-facebook-like', true, true);
        // Override global window here because Preact uses global `createElement`.
        doNotLoadExternalResourcesInTest(window, env.sandbox);
      });
  
      it('renders', async () => {
        element = createElementWithAttributes(doc, 'amp-facebook-like', {
          'height': 500,
          'width': 500,
          'layout': 'responsive',
        });
        doc.body.appendChild(element);
        await waitForRender();
  
        const iframe = element.shadowRoot.querySelector('iframe');
        expect(iframe.src).to.equal(
          'http://ads.localhost:9876/dist.3p/current/frame.max.html'
        );
      });
  
      it('propagates title to iframe', async () => {
        element = createElementWithAttributes(doc, 'amp-facebook-like', {
          'height': 500,
          'width': 500,
          'layout': 'responsive',
          'title': 'my custom facebook like element',
        });
        doc.body.appendChild(element);
        await waitForRender();
  
        const iframe = element.shadowRoot.querySelector('iframe');
        expect(iframe.title).to.equal('my custom facebook like element');
      });
  
      it('ensures iframe is not sandboxed in amp-facebook-like', async () => {
        element = createElementWithAttributes(doc, 'amp-facebook-like', {
          'height': 500,
          'width': 500,
          'layout': 'responsive',
        });
        doc.body.appendChild(element);
        await waitForRender();
  
        const iframe = element.shadowRoot.querySelector('iframe');
        expect(iframe.hasAttribute('sandbox')).to.be.false;
      });
  
      it('renders amp-facebook-like with specified locale', async () => {
        element = createElementWithAttributes(doc, 'amp-facebook-like', {
          'data-locale': 'fr_FR',
          'height': 500,
          'width': 500,
          'layout': 'responsive',
        });
        doc.body.appendChild(element);
        await waitForRender();
  
        const iframe = element.shadowRoot.querySelector('iframe');
        expect(iframe.getAttribute('name')).to.contain('"locale":"fr_FR"');
      });
  
      it('renders with correct embed type', async () => {
        element = createElementWithAttributes(doc, 'amp-facebook-like', {
          'height': 500,
          'width': 500,
          'layout': 'responsive',
        });
        doc.body.appendChild(element);
        await waitForRender();
  
        const iframe = element.shadowRoot.querySelector('iframe');
        const context = JSON.parse(iframe.getAttribute('name'));
        expect(context.attributes.embedAs).to.equal('like');
      });
  
      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-like', {
          'height': initialHeight,
          'width': 500,
          'layout': 'responsive',
        });
        doc.body.appendChild(element);
        await element.buildInternal();
        await element.layoutCallback();
  
        const impl = await element.getImpl(false);
        const attemptChangeHeightStub = env.sandbox
          .stub(impl, 'attemptChangeHeight')
          .resolves();
  
        const iframe = element.shadowRoot.querySelector('iframe');
        const mockEvent = new CustomEvent('message');
        const sentinel = JSON.parse(iframe.getAttribute('name'))['attributes'][
          '_context'
        ]['sentinel'];
        mockEvent.data = serializeMessage('embed-size', sentinel, {
          'height': 1000,
        });
        mockEvent.source = iframe.contentWindow;
        win.dispatchEvent(mockEvent);
        expect(attemptChangeHeightStub).to.be.calledOnce.calledWith(1000);
      });
    }
  );
  