diff --git a/extensions/amp-insticator/0.1/amp-insticator.js b/extensions/amp-insticator/0.1/amp-insticator.js index 3f83be80a107..4e6a1b652544 100644 --- a/extensions/amp-insticator/0.1/amp-insticator.js +++ b/extensions/amp-insticator/0.1/amp-insticator.js @@ -19,8 +19,7 @@ /* ++++++++++ --------------- IMPORTS --------------- ++++++++++ */ import { Layout } from '../../../src/layout'; import { setStyles } from '../../../src/style'; - - +import {removeElement} from '../../../src/dom'; /* ++++++++++ --------------- EXPORT AMP-INSTICATOR --------------- ++++++++++ */ export class AmpInsticator extends AMP.BaseElement { diff --git a/extensions/amp-insticator/0.1/test/test-amp-insticator.js b/extensions/amp-insticator/0.1/test/test-amp-insticator.js index ba8ac7024721..9ebcea452a18 100644 --- a/extensions/amp-insticator/0.1/test/test-amp-insticator.js +++ b/extensions/amp-insticator/0.1/test/test-amp-insticator.js @@ -14,7 +14,7 @@ * limitations under the License. */ -import '../amp-insticator'; +import {AmpInsticator} from '../amp-insticator'; describes.realWin('amp-insticator', { amp: { @@ -23,16 +23,65 @@ describes.realWin('amp-insticator', { }, env => { let win; - let element; + let doc; + let ampInsticator; + let obj; + let layoutCallbackSpy; + let getRequestSpy; - beforeEach(() => { + function getAmpInsticatior() { + const ampInsticator = doc.createElement('amp-insticator'); + ampInsticator.setAttribute('data-embed-id', '5bf0548f-a332-4934-9d35-c9ca9e93d4ff') + doc.body.appendChild(ampInsticator); + + //Seting up spys and fakes + getRequestSpy = env.sandbox.spy(ampInsticator.implementation_, 'getRequest'); + + return ampInsticator.build() + .then( () => ampInsticator.layoutCallback() ) + .then( () => ampInsticator ); + } + + beforeEach(async function() { win = env.win; - element = win.document.createElement('amp-insticator'); - win.document.body.appendChild(element); + doc = win.document; + ampInsticator = await getAmpInsticatior(); + obj = ampInsticator.implementation_; + }); + + describe('renders', function renders(){ + it('renders an iframe', function() { + expect(ampInsticator).to.not.be.undefined; + expect(ampInsticator).to.not.be.null; + const iframe = ampInsticator.querySelector('iframe'); + expect(iframe).to.not.be.undefined; + expect(iframe).to.not.be.null; + }); + it("appends Insticator's script to iframe", function(){ + const iframe = ampInsticator.querySelector('iframe'); + const scriptTagInIframe = iframe.contentWindow.document.querySelector('script'); + expect(scriptTagInIframe).to.not.be.undefined; + expect(scriptTagInIframe).to.not.be.null; + expect(scriptTagInIframe.src).to.include(obj.url.embed); + }); + it("appends div#app to iframe", function(){ + const iframe = ampInsticator.querySelector('iframe'); + const divApp = iframe.contentWindow.document.getElementById('app'); + expect(divApp).to.not.be.undefined; + expect(divApp).to.not.be.null; + }); + }); + + it('makes a request for ads data correctly', function() { + expect(getRequestSpy).to.be.calledOnce; + expect(getRequestSpy.calledWith(sinon.match(obj.url.ads), sinon.match.func)).to.be.true; }); - it('should have hello world when built', () => { - element.build(); - expect(element.querySelector('div').textContent).to.equal('hello world'); + it('removes iframe on unlayoutCallback()', function() { + const iframe = ampInsticator.querySelector('iframe'); + expect(iframe).to.not.be.undefined; + expect(iframe).to.not.be.null; + obj.unlayoutCallback(); + expect(ampInsticator.querySelector('iframe')).to.be.null; }); });