diff --git a/extensions/amp-sidebar/0.1/test/test-amp-sidebar.js b/extensions/amp-sidebar/0.1/test/test-amp-sidebar.js index 0390f944b572..66dd863245fd 100644 --- a/extensions/amp-sidebar/0.1/test/test-amp-sidebar.js +++ b/extensions/amp-sidebar/0.1/test/test-amp-sidebar.js @@ -16,16 +16,12 @@ */ import {KeyCodes} from '../../../../src/utils/key-codes'; -import {adopt} from '../../../../src/runtime'; -import {createIframePromise} from '../../../../testing/iframe'; import {Services} from '../../../../src/services'; import {assertScreenReaderElement} from '../../../../testing/test-helper'; import {toggleExperiment} from '../../../../src/experiments'; -import * as sinon from 'sinon'; import '../amp-sidebar'; import * as lolex from 'lolex'; -adopt(window); describes.realWin('amp-sidebar 0.1 version', { win: { /* window spec */ @@ -36,54 +32,61 @@ describes.realWin('amp-sidebar 0.1 version', { runtimeOn: false, extensions: ['amp-sidebar:0.1'], }, -}, () => { - let sandbox; +}, env => { + let win, doc; let platform; let clock; let timer; + beforeEach(() => { + win = env.win; + doc = win.document; + timer = Services.timerFor(win); + platform = Services.platformFor(win); + }); + function getAmpSidebar(options) { options = options || {}; - return createIframePromise().then(iframe => { - const ampSidebar = iframe.doc.createElement('amp-sidebar'); - const list = iframe.doc.createElement('ul'); - for (let i = 0; i < 10; i++) { - const li = iframe.doc.createElement('li'); - li.innerHTML = 'Menu item ' + i; - list.appendChild(li); - } - ampSidebar.appendChild(list); - const anchor = iframe.doc.createElement('a'); - anchor.href = '#section1'; - ampSidebar.appendChild(anchor); + const ampSidebar = doc.createElement('amp-sidebar'); + const list = doc.createElement('ul'); + for (let i = 0; i < 10; i++) { + const li = doc.createElement('li'); + li.innerHTML = 'Menu item ' + i; + list.appendChild(li); + } + ampSidebar.appendChild(list); + const anchor = doc.createElement('a'); + anchor.href = '#section1'; + ampSidebar.appendChild(anchor); + if (options.toolbars) { + getToolbars(options, ampSidebar); + } + if (options.side) { + ampSidebar.setAttribute('side', options.side); + } + if (options.open) { + ampSidebar.setAttribute('open', ''); + } + if (options.closeText) { + ampSidebar.setAttribute('data-close-button-aria-label', + options.closeText); + }; + ampSidebar.setAttribute('id', 'sidebar1'); + ampSidebar.setAttribute('layout', 'nodisplay'); + doc.body.appendChild(ampSidebar); + return ampSidebar.build().then(() => { + return ampSidebar.layoutCallback(); + }).then(() => { if (options.toolbars) { - getToolbars(options, ampSidebar, iframe); - } - if (options.side) { - ampSidebar.setAttribute('side', options.side); - } - if (options.open) { - ampSidebar.setAttribute('open', ''); + sandbox.stub(timer, 'delay', function(callback) { + callback(); + }); } - if (options.closeText) { - ampSidebar.setAttribute('data-close-button-aria-label', - options.closeText); - }; - ampSidebar.setAttribute('id', 'sidebar1'); - ampSidebar.setAttribute('layout', 'nodisplay'); - return iframe.addElement(ampSidebar).then(() => { - timer = Services.timerFor(iframe.win); - if (options.toolbars) { - sandbox.stub(timer, 'delay', function(callback) { - callback(); - }); - } - return {iframe, ampSidebar}; - }); + return ampSidebar; }); } - function getToolbars(options, ampSidebar, iframe) { + function getToolbars(options, ampSidebar) { // Stub our sidebar operations, doing this here as it will // Ease testing our media queries const impl = ampSidebar.implementation_; @@ -98,10 +101,10 @@ describes.realWin('amp-sidebar 0.1 version', { }); // Create our individual toolbars options.toolbars.forEach(toolbarObj => { - const navToolbar = iframe.doc.createElement('nav'); + const navToolbar = doc.createElement('nav'); //Create/Set toolbar-target - const toolbarTarget = iframe.doc.createElement('div'); + const toolbarTarget = doc.createElement('div'); if (toolbarObj.toolbarTarget) { toolbarTarget.setAttribute('id', toolbarObj.toolbarTarget); @@ -111,7 +114,7 @@ describes.realWin('amp-sidebar 0.1 version', { toolbarTarget.setAttribute('id', 'toolbar-target'); navToolbar.setAttribute('toolbar-target', 'toolbar-target'); } - iframe.win.document.body.appendChild(toolbarTarget); + doc.body.appendChild(toolbarTarget); // Set the toolbar media if (toolbarObj.media) { @@ -119,9 +122,9 @@ describes.realWin('amp-sidebar 0.1 version', { } else { navToolbar.setAttribute('toolbar', '(min-width: 768px)'); } - const toolbarList = iframe.doc.createElement('ul'); + const toolbarList = doc.createElement('ul'); for (let i = 0; i < 3; i++) { - const li = iframe.doc.createElement('li'); + const li = doc.createElement('li'); li.innerHTML = 'Toolbar item ' + i; toolbarList.appendChild(li); } @@ -131,18 +134,8 @@ describes.realWin('amp-sidebar 0.1 version', { } describe('amp-sidebar', () => { - beforeEach(() => { - sandbox = sinon.sandbox.create(); - platform = Services.platformFor(window); - }); - - afterEach(() => { - sandbox.restore(); - }); - it('should apply overlay class', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { expect(sidebarElement.classList.contains('i-amphtml-overlay')); }); }); @@ -150,8 +143,7 @@ describes.realWin('amp-sidebar 0.1 version', { it('should replace text to screen reader \ button in data-close-button-aria-label', () => { return getAmpSidebar({'closeText': - 'data-close-button-aria-label'}).then(obj => { - const sidebarElement = obj.ampSidebar; + 'data-close-button-aria-label'}).then(sidebarElement => { const closeButton = sidebarElement.lastElementChild; expect(closeButton.textContent) .to.equal('data-close-button-aria-label'); @@ -159,23 +151,19 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should open from left is side is not specified', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { expect(sidebarElement.getAttribute('side')).to.equal('left'); }); }); it('should open from right is side right is specified', () => { - return getAmpSidebar({'side': 'right'}).then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar({'side': 'right'}).then(sidebarElement => { expect(sidebarElement.getAttribute('side')).to.equal('right'); }); }); it('should create mask element in DOM', () => { - return getAmpSidebar().then(obj => { - const iframe = obj.iframe; - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const impl = sidebarElement.implementation_; impl.vsync_ = { mutate(callback) { @@ -183,15 +171,14 @@ describes.realWin('amp-sidebar 0.1 version', { }, }; impl.open_(); - expect(iframe.doc.querySelectorAll('.i-amphtml-sidebar-mask').length) + expect(doc.querySelectorAll('.i-amphtml-sidebar-mask').length) .to.equal(1); }); }); it('should create an invisible close \ button for screen readers only', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const impl = sidebarElement.implementation_; impl.close_ = sandbox.spy(); const closeButton = sidebarElement.lastElementChild; @@ -206,8 +193,7 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should open sidebar on button click', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const impl = sidebarElement.implementation_; clock = lolex.install(impl.win, 0, ['Date', 'setTimeout']); const historyPushSpy = sandbox.spy(); @@ -243,7 +229,7 @@ describes.realWin('amp-sidebar 0.1 version', { expect(impl.scheduleLayout).to.not.be.called; clock.tick(600); - expect(obj.iframe.doc.activeElement).to.equal(sidebarElement); + expect(doc.activeElement).to.equal(sidebarElement); expect(sidebarElement.style.display).to.equal(''); expect(impl.scheduleLayout).to.be.calledOnce; @@ -257,8 +243,7 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should close sidebar on button click', () => { - return getAmpSidebar({'open': true}).then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar({'open': true}).then(sidebarElement => { const impl = sidebarElement.implementation_; clock = lolex.install(impl.win, 0, ['Date', 'setTimeout']); impl.schedulePause = sandbox.spy(); @@ -302,8 +287,7 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should toggle sidebar on button click', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const impl = sidebarElement.implementation_; clock = lolex.install(impl.win, 0, ['Date', 'setTimeout']); impl.scheduleLayout = sandbox.spy(); @@ -317,12 +301,12 @@ describes.realWin('amp-sidebar 0.1 version', { expect(sidebarElement.hasAttribute('open')).to.be.false; expect(sidebarElement.getAttribute('aria-hidden')).to.equal('true'); expect(sidebarElement.getAttribute('role')).to.equal('menu'); - expect(obj.iframe.doc.activeElement).to.not.equal(sidebarElement); + expect(doc.activeElement).to.not.equal(sidebarElement); impl.toggle_(); expect(sidebarElement.hasAttribute('open')).to.be.true; expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false'); clock.tick(600); - expect(obj.iframe.doc.activeElement).to.equal(sidebarElement); + expect(doc.activeElement).to.equal(sidebarElement); expect(sidebarElement.style.display).to.equal(''); expect(impl.scheduleLayout).to.be.calledOnce; impl.toggle_(); @@ -335,9 +319,7 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should close sidebar on escape', () => { - return getAmpSidebar().then(obj => { - const iframe = obj.iframe; - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const impl = sidebarElement.implementation_; clock = lolex.install(impl.win, 0, ['Date', 'setTimeout']); impl.schedulePause = sandbox.spy(); @@ -350,14 +332,14 @@ describes.realWin('amp-sidebar 0.1 version', { impl.open_(); expect(sidebarElement.hasAttribute('open')).to.be.true; expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false'); - const eventObj = document.createEventObject ? - document.createEventObject() : document.createEvent('Events'); + const eventObj = doc.createEventObject ? + doc.createEventObject() : doc.createEvent('Events'); if (eventObj.initEvent) { eventObj.initEvent('keydown', true, true); } eventObj.keyCode = KeyCodes.ESCAPE; eventObj.which = KeyCodes.ESCAPE; - const el = iframe.doc.documentElement; + const el = doc.documentElement; el.dispatchEvent ? el.dispatchEvent(eventObj) : el.fireEvent('onkeydown', eventObj); expect(sidebarElement.hasAttribute('open')).to.be.false; @@ -369,8 +351,7 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should reflect state of the sidebar', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const impl = sidebarElement.implementation_; clock = lolex.install(impl.win, 0, ['Date', 'setTimeout']); impl.schedulePause = sandbox.spy(); @@ -411,8 +392,7 @@ describes.realWin('amp-sidebar 0.1 version', { it.skip('should fix scroll leaks on ios safari', () => { sandbox.stub(platform, 'isIos').returns(true); sandbox.stub(platform, 'isSafari').returns(true); - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const impl = sidebarElement.implementation_; impl.vsync_ = { mutate(callback) { @@ -431,8 +411,7 @@ describes.realWin('amp-sidebar 0.1 version', { it.skip('should adjust for IOS safari bottom bar', () => { sandbox.stub(platform, 'isIos').returns(true); sandbox.stub(platform, 'isSafari').returns(true); - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const impl = sidebarElement.implementation_; impl.vsync_ = { mutate(callback) { @@ -454,8 +433,7 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should close sidebar if clicked on a non-local anchor', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const anchor = sidebarElement.getElementsByTagName('a')[0]; anchor.href = '#newloc'; const impl = sidebarElement.implementation_; @@ -470,8 +448,8 @@ describes.realWin('amp-sidebar 0.1 version', { impl.open_(); expect(sidebarElement.hasAttribute('open')).to.be.true; expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false'); - const eventObj = document.createEventObject ? - document.createEventObject() : document.createEvent('Events'); + const eventObj = doc.createEventObject ? + doc.createEventObject() : doc.createEvent('Events'); if (eventObj.initEvent) { eventObj.initEvent('click', true, true); } @@ -497,8 +475,7 @@ describes.realWin('amp-sidebar 0.1 version', { it('should not close sidebar if \ clicked on a new origin navigation', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const anchor = sidebarElement.getElementsByTagName('a')[0]; anchor.href = '#newloc'; const impl = sidebarElement.implementation_; @@ -515,8 +492,8 @@ describes.realWin('amp-sidebar 0.1 version', { impl.open_(); expect(sidebarElement.hasAttribute('open')).to.be.true; expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false'); - const eventObj = document.createEventObject ? - document.createEventObject() : document.createEvent('Events'); + const eventObj = doc.createEventObject ? + doc.createEventObject() : doc.createEvent('Events'); if (eventObj.initEvent) { eventObj.initEvent('click', true, true); } @@ -541,8 +518,7 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should not close sidebar if clicked on new page navigation', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const anchor = sidebarElement.getElementsByTagName('a')[0]; anchor.href = '#newloc'; const impl = sidebarElement.implementation_; @@ -559,8 +535,8 @@ describes.realWin('amp-sidebar 0.1 version', { impl.open_(); expect(sidebarElement.hasAttribute('open')).to.be.true; expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false'); - const eventObj = document.createEventObject ? - document.createEventObject() : document.createEvent('Events'); + const eventObj = doc.createEventObject ? + doc.createEventObject() : doc.createEvent('Events'); if (eventObj.initEvent) { eventObj.initEvent('click', true, true); } @@ -586,8 +562,7 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should not close sidebar if clicked on non-anchor', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const li = sidebarElement.getElementsByTagName('li')[0]; const impl = sidebarElement.implementation_; impl.schedulePause = sandbox.spy(); @@ -603,8 +578,8 @@ describes.realWin('amp-sidebar 0.1 version', { impl.open_(); expect(sidebarElement.hasAttribute('open')).to.be.true; expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false'); - const eventObj = document.createEventObject ? - document.createEventObject() : document.createEvent('Events'); + const eventObj = doc.createEventObject ? + doc.createEventObject() : doc.createEvent('Events'); if (eventObj.initEvent) { eventObj.initEvent('click', true, true); } @@ -619,8 +594,7 @@ describes.realWin('amp-sidebar 0.1 version', { }); it('should listen to animationend/transitionend event', () => { - return getAmpSidebar().then(obj => { - const sidebarElement = obj.ampSidebar; + return getAmpSidebar().then(sidebarElement => { const impl = sidebarElement.implementation_; clock = lolex.install(impl.win, 0, ['Date', 'setTimeout']); impl.boundOnAnimationEnd_ = sandbox.spy(); @@ -649,23 +623,14 @@ describes.realWin('amp-sidebar 0.1 version', { describe('amp-sidebar - toolbars in amp-sidebar', () => { beforeEach(() => { - sandbox = sinon.sandbox.create(); - platform = Services.platformFor(window); - toggleExperiment(window, 'amp-sidebar toolbar', true); - }); - - afterEach(() => { - sandbox.restore(); + toggleExperiment(win, 'amp-sidebar toolbar', true); }); // Tests for amp-sidebar 1.0 it('should not create toolbars without