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

Fix amp-accordion and fontloader local tests failures #3052

Merged
merged 2 commits into from
May 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 18 additions & 12 deletions extensions/amp-accordion/0.1/amp-accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,26 @@ class AmpAccordion extends AMP.BaseElement {
content.setAttribute('id', contentId);
}
header.setAttribute('aria-controls', contentId);
header.addEventListener('click', event => {
event.preventDefault();
this.mutateElement(() => {
if (section.hasAttribute('expanded')) {
section.removeAttribute('expanded');
content.setAttribute('aria-expanded', 'false');
} else {
section.setAttribute('expanded', '');
content.setAttribute('aria-expanded', 'true');
}
}, content);
});
header.addEventListener('click', this.handleClick_.bind(this));
});
}

/** @private */
handleClick_(event) {
Copy link
Contributor

Choose a reason for hiding this comment

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

rename this to onHeaderClick_

and annotate the function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

event.preventDefault();
const section = event.target.parentNode;
const sectionComponents_ = section.children;
const content = sectionComponents_[1];
this.mutateElement(() => {
if (section.hasAttribute('expanded')) {
section.removeAttribute('expanded');
content.setAttribute('aria-expanded', 'false');
} else {
section.setAttribute('expanded', '');
content.setAttribute('aria-expanded', 'true');
}
}, content);
}
}

AMP.registerElement('amp-accordion', AmpAccordion, CSS);
64 changes: 33 additions & 31 deletions extensions/amp-accordion/0.1/test/test-amp-accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@ import {Timer} from '../../../../src/timer';
import {adopt} from '../../../../src/runtime';
import {createIframePromise} from '../../../../testing/iframe';
require('../amp-accordion');
import * as sinon from 'sinon';
Copy link
Contributor

Choose a reason for hiding this comment

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

wow - wonder how it was working without this:)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test didn't use sinon really, but there are a lot of files who are and not doing the import. I am going to send another PR to fix all the non-imported sinon. I think it depends on how the bundler bundles them but yeah I've seen a nasty test failure in test-3p-frame because sinon wasn't properly imported.


adopt(window);

describe('amp-accordion', () => {
const timer = new Timer(window);
let timer;
let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

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

function getAmpAccordion() {
return createIframePromise().then(iframe => {
timer = new Timer(iframe.win);
const ampAccordion = iframe.doc.createElement('amp-accordion');
ampAccordion.implementation_.mutateElement = fn => fn();
for (let i = 0; i < 3; i++) {
const section = iframe.doc.createElement('section');
section.innerHTML = '<h2>Section ' + i + '</h2><div>Loreum ipsum</div>';
Expand All @@ -46,43 +59,32 @@ describe('amp-accordion', () => {
it('should expand when header of a collapsed section is clicked', () => {
return getAmpAccordion().then(obj => {
const iframe = obj.iframe;
let clickEvent;
if (iframe.doc.createEvent) {
clickEvent = iframe.doc.createEvent('MouseEvent');
clickEvent.initMouseEvent('click', true, true, iframe.win, 1);
} else {
clickEvent = iframe.doc.createEventObject();
clickEvent.type = 'click';
}
const headerElements =
iframe.doc.querySelectorAll('section > *:first-child');
const headerElements = iframe.doc.querySelectorAll(
'section > *:first-child');
const clickEvent = {
target: headerElements[0],
preventDefault: sandbox.spy(),
};
expect(headerElements[0].parentNode.hasAttribute('expanded')).to.be.false;
headerElements[0].dispatchEvent(clickEvent);
return timer.promise(50).then(() => {
expect(headerElements[0].parentNode.hasAttribute('expanded'))
.to.be.true;
});
obj.ampAccordion.implementation_.handleClick_(clickEvent);
expect(headerElements[0].parentNode.hasAttribute('expanded')).to.be.true;
expect(clickEvent.preventDefault.called).to.be.true;
});
});

it('should collapse when header of an expanded section is clicked', () => {
return getAmpAccordion().then(obj => {
const iframe = obj.iframe;
let clickEvent;
if (iframe.doc.createEvent) {
clickEvent = iframe.doc.createEvent('MouseEvent');
clickEvent.initMouseEvent('click', true, true, iframe.win, 1);
} else {
clickEvent = iframe.doc.createEventObject();
clickEvent.type = 'click';
}
const headerElements =
iframe.doc.querySelectorAll('section > *:first-child');
const headerElements = iframe.doc.querySelectorAll(
'section > *:first-child');
const clickEvent = {
target: headerElements[1],
preventDefault: sandbox.spy(),
};
expect(headerElements[1].parentNode.hasAttribute('expanded')).to.be.true;
headerElements[1].dispatchEvent(clickEvent);
return timer.promise(50).then(() => {
expect(headerElements[1].parentNode.hasAttribute('expanded'))
.to.be.false;
});
obj.ampAccordion.implementation_.handleClick_(clickEvent);
expect(headerElements[1].parentNode.hasAttribute('expanded')).to.be.false;
expect(clickEvent.preventDefault.called).to.be.true;
});
});
});
8 changes: 7 additions & 1 deletion extensions/amp-font/0.1/test/test-fontloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ describe('FontLoader', () => {
iframe.doc.body.appendChild(fontDiv);
fontloader.defaultFontElements_ = [defaultDiv];
fontloader.customFontElement_ = fontDiv;
expect(fontloader.compareMeasurements_()).to.be.true;
return fontloader.load(FONT_CONFIG, 3000).then(() => {
fontloader.defaultFontElements_ = [defaultDiv];
fontloader.customFontElement_ = fontDiv;
expect(fontloader.compareMeasurements_()).to.be.true;
}).catch(() => {
assert.fail('Font load failed');
});
});
});
});