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

[amp-sidebar 1.0] [Toolbar] Added Tests for Current Toolbar functionality #10084

Merged
merged 11 commits into from
Jun 26, 2017
243 changes: 243 additions & 0 deletions extensions/amp-sidebar/1.0/test/test-amp-sidebar-toolbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to test-toolbar.js

* Copyright 2017 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 {adopt} from '../../../../src/runtime';
import {createIframePromise} from '../../../../testing/iframe';
import {timerFor} from '../../../../src/services';
import {toggleExperiment} from '../../../../src/experiments';
import * as sinon from 'sinon';
import '../amp-sidebar';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this import toolbar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toolbar requires and amp-sidebar element to be instantiated. So we need to create one for toolbar to work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stub one or more dummy amp-sidebar element(s), and test that is ideally how unit tests should work.


/** @const */
const DEFAULT_TOOLBAR_MEDIA = '(min-width: 768px)';

/** @const */
const TOOLBAR_CLASS = 'i-amphtml-toolbar';


adopt(window);

describes.realWin('amp-sidebar 1.0 version', {
win: { /* window spec */
location: '...',
historyOff: false,
},
amp: { /* amp spec */
runtimeOn: false,
extensions: ['amp-sidebar:1.0'],
},
}, () => {
describe('amp-sidebar', () => {
let sandbox;
let timer;

function getAmpSidebar(options) {
options = options || {};
return createIframePromise().then(iframe => {
const ampSidebar = iframe.doc.createElement('amp-sidebar');
if (options.toolbars) {
// Stub our sidebar operations, doing this here as it will
// Ease testing our media queries
const impl = ampSidebar.implementation_;
sandbox.stub(impl.vsync_,
'mutate', callback => {
callback();
});
sandbox.stub(impl.vsync_,
'mutatePromise', callback => {
callback();
return Promise.resolve();
});
// Create our individual toolbars
options.toolbars.forEach(toolbar => {
const navToolbar = iframe.doc.createElement('nav');
if (toolbar.media) {
navToolbar.setAttribute('toolbar', toolbar.media);
} else {
navToolbar.setAttribute('toolbar', DEFAULT_TOOLBAR_MEDIA);
}
if (toolbar.toolbarOnlyOnNav) {
navToolbar.setAttribute('toolbar-only', 'true');
}
const toolbarList = iframe.doc.createElement('ul');
for (let i = 0; i < 3; i++) {
const li = iframe.doc.createElement('li');
li.innerHTML = 'Toolbar item ' + i;
toolbarList.appendChild(li);
}
navToolbar.appendChild(toolbarList);
ampSidebar.appendChild(navToolbar);
});
}
ampSidebar.setAttribute('id', 'sidebar1');
ampSidebar.setAttribute('layout', 'nodisplay');
return iframe.addElement(ampSidebar).then(() => {
timer = timerFor(iframe.win);
if (options.toolbars) {
sandbox.stub(timer, 'delay', function(callback) {
callback();
});
}
return {iframe, ampSidebar};
});
});
}

function resizeIframeToWidth(iframeObject, width, callback) {
iframeObject.iframe.setAttribute('width', width);
// Force the browser to re-draw
iframeObject.win.innerWidth;
callback();
}

beforeEach(() => {
sandbox = sinon.sandbox.create();
toggleExperiment(window, 'amp-sidebar 1.0', true);
});

afterEach(() => {
sandbox.restore();
});

it('toolbar header should be hidden for a \
invalid window size for DEFAULT_TOOLBAR_MEDIA', () => {
return getAmpSidebar({
toolbars: [true],
}).then(obj => {
const sidebarElement = obj.ampSidebar;
const toolbarElements = Array.prototype
.slice.call(sidebarElement.ownerDocument
.getElementsByClassName(TOOLBAR_CLASS), 0);
resizeIframeToWidth(obj.iframe, '1px', () => {
expect(toolbarElements.length).to.be.above(0);
sidebarElement.implementation_.toolbars_.forEach(toolbar => {
toolbar.onLayoutChange();
});
expect(toolbarElements[0].parentElement.style.display)
.to.be.equal('none');
});
});
});

it('toolbar header should be shown for a \
valid window size for DEFAULT_TOOLBAR_MEDIA', () => {
return getAmpSidebar({
toolbars: [true],
}).then(obj => {
const sidebarElement = obj.ampSidebar;
const toolbarElements = Array.prototype
.slice.call(sidebarElement.ownerDocument
.getElementsByClassName(TOOLBAR_CLASS), 0);
resizeIframeToWidth(obj.iframe, '4000px', () => {
expect(toolbarElements.length).to.be.above(0);
sidebarElement.implementation_.toolbars_.forEach(toolbar => {
toolbar.onLayoutChange();
});
expect(toolbarElements[0].parentElement.style.display)
.to.be.equal('');
});
});
});

it('should hide <nav toolbar> elements with toolbar-only, \
inside the sidebar, but not inside the toolbar, for a valid \
window size for DEFAULT_TOOLBAR_MEDIA', () => {
return getAmpSidebar({
toolbars: [{
toolbarOnlyOnNav: true,
}],
}).then(obj => {
const sidebarElement = obj.ampSidebar;
const toolbars = sidebarElement.implementation_.toolbars_;
resizeIframeToWidth(obj.iframe, '4000px', () => {
toolbars.forEach(toolbar => {
toolbar.onLayoutChange();
});
const toolbarNavElements = Array.prototype
.slice.call(sidebarElement.ownerDocument
.querySelectorAll('nav[toolbar]'), 0);
const hiddenToolbarNavElements = Array.prototype
.slice.call(sidebarElement.ownerDocument
.querySelectorAll('nav[style]'), 0);
expect(toolbarNavElements.length).to.be.equal(2);
expect(hiddenToolbarNavElements.length).to.be.equal(1);
expect(toolbars.length).to.be.equal(1);
});
});
});

it('toolbar should be in the hidden state \
when it is not being displayed', () => {
return getAmpSidebar({
toolbars: [true],
}).then(obj => {
const toolbars = obj.ampSidebar.implementation_.toolbars_;
resizeIframeToWidth(obj.iframe, '1px', () => {
toolbars.forEach(toolbar => {
toolbar.onLayoutChange();
expect(toolbar.isToolbarShown_()).to.be.false;
});
});
});
});

it('toolbar should be in the shown state \
when it is being displayed', () => {
return getAmpSidebar({
toolbars: [true],
}).then(obj => {
const toolbars = obj.ampSidebar.implementation_.toolbars_;
resizeIframeToWidth(obj.iframe, '4000px', () => {
toolbars.forEach(toolbar => {
toolbar.onLayoutChange();
expect(toolbar.isToolbarShown_()).to.be.true;
});
});
});
});

it('toolbar should not be able to be shown \
if already in the shown state', () => {
return getAmpSidebar({
toolbars: [true],
}).then(obj => {
const toolbars = obj.ampSidebar.implementation_.toolbars_;
resizeIframeToWidth(obj.iframe, '4000px', () => {
toolbars.forEach(toolbar => {
toolbar.onLayoutChange();
expect(toolbar.attemptShow_()).to.be.undefined;
});
});
});
});

it('toolbar should be able to be shown \
if not in the shown state, and return a promise', () => {
return getAmpSidebar({
toolbars: [true],
}).then(obj => {
const toolbars = obj.ampSidebar.implementation_.toolbars_;
resizeIframeToWidth(obj.iframe, '1px', () => {
toolbars.forEach(toolbar => {
toolbar.onLayoutChange();
expect(toolbar).to.exist;
});
});
});
});
});
});
67 changes: 45 additions & 22 deletions extensions/amp-sidebar/1.0/test/test-amp-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import '../amp-sidebar';

/** @const */
const TOOLBAR_MEDIA = '(min-width: 768px)';
const DEFAULT_TOOLBAR_MEDIA = '(min-width: 768px)';


adopt(window);
Expand Down Expand Up @@ -60,17 +60,32 @@
const anchor = iframe.doc.createElement('a');
anchor.href = '#section1';
ampSidebar.appendChild(anchor);
if (options.toolbar) {
const navToolbar = iframe.doc.createElement('nav');
navToolbar.setAttribute('toolbar', TOOLBAR_MEDIA);
const toolbarList = iframe.doc.createElement('ul');
for (let i = 0; i < 3; i++) {
const li = iframe.doc.createElement('li');
li.innerHTML = 'Toolbar item ' + i;
toolbarList.appendChild(li);
}
navToolbar.appendChild(toolbarList);
ampSidebar.appendChild(navToolbar);
if (options.toolbars) {
// Stub our sidebar operations, doing this here as it will
// Ease testing our media queries
const impl = ampSidebar.implementation_;
sandbox.stub(impl.vsync_,
'mutate', callback => {
callback();
});
sandbox.stub(impl.vsync_,
'mutatePromise', callback => {
callback();
return Promise.resolve();
});
// Create our individual toolbars
options.toolbars.forEach(() => {
const navToolbar = iframe.doc.createElement('nav');
navToolbar.setAttribute('toolbar', DEFAULT_TOOLBAR_MEDIA);
const toolbarList = iframe.doc.createElement('ul');
for (let i = 0; i < 3; i++) {
const li = iframe.doc.createElement('li');
li.innerHTML = 'Toolbar item ' + i;
toolbarList.appendChild(li);
}
navToolbar.appendChild(toolbarList);
ampSidebar.appendChild(navToolbar);
});
}
if (options.side) {
ampSidebar.setAttribute('side', options.side);
Expand All @@ -82,6 +97,11 @@
ampSidebar.setAttribute('layout', 'nodisplay');
return iframe.addElement(ampSidebar).then(() => {
timer = timerFor(iframe.win);
if (options.toolbars) {
sandbox.stub(timer, 'delay', function(callback) {
callback();
});
}
return {iframe, ampSidebar};
});
});
Expand Down Expand Up @@ -569,36 +589,39 @@
.querySelectorAll('*[toolbar]');
expect(headerElements.length).to.be.equal(0);
expect(toolbarElements.length).to.be.equal(0);
expect(sidebarElement.implementation_.toolbars_.length).to.be.equal(0);
});
});

it('should create a toolbar target element, \
containing the navigation toolbar element', () => {
return getAmpSidebar({
toolbar: true,
toolbars: [true],
}).then(obj => {
const sidebarElement = obj.ampSidebar;
const toolbarNavElements = Array.prototype
.slice.call(sidebarElement.ownerDocument
.querySelectorAll('nav[toolbar]'), 0);
expect(toolbarNavElements.length).to.be.above(1);
expect(sidebarElement.implementation_.toolbars_.length).to.be.equal(1);
});
});

it('toolbar header should be hidden for the const TOOLBAR_MEDIA', () => {
it('should create multiple toolbar target elements, \
containing the navigation toolbar element', () => {
return getAmpSidebar({
toolbar: true,
toolbars: [true,
{
media: '(min-width: 1024px)',
},
],
}).then(obj => {
const sidebarElement = obj.ampSidebar;
const toolbarNavElements = Array.prototype
.slice.call(sidebarElement.ownerDocument
.querySelectorAll('nav[toolbar]'), 0);
expect(toolbarNavElements.length).to.be.above(1);
expect(toolbarNavElements.some(navElement => {
if (navElement.parentElement.style.display == 'none') {
return true;
}
return false;
})).to.be.true;
expect(toolbarNavElements.length).to.be.equal(4);
expect(sidebarElement.implementation_.toolbars_.length).to.be.equal(2);
});
});
});
Expand Down