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

Fake and real window fixtures for tests #5425

Merged
merged 7 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
"assert": false,
"sinon": true,
"sandbox": true,
"context": false
"context": false,
"global": false,
"describes": true,
"clock": true
},
"rules": {
"array-bracket-spacing": [2, "never"],
Expand Down
2 changes: 2 additions & 0 deletions build-system/tasks/presubmit-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ var forbiddenTerms = {
'src/amp.js',
'src/amp-shadow.js',
'src/service/ampdoc-impl.js',
'testing/describes.js',
'testing/iframe.js',
],
},
Expand Down Expand Up @@ -339,6 +340,7 @@ var forbiddenTerms = {
whitelist: [
'extensions/amp-analytics/0.1/cid-impl.js',
'src/service/storage-impl.js',
'testing/fake-dom.js',
],
},
'sessionStorage': {
Expand Down
6 changes: 6 additions & 0 deletions test/_init_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import {
import {installDocService} from '../src/service/ampdoc-impl';
import {platformFor} from '../src/platform';
import {setDefaultBootstrapBaseUrlForTesting} from '../src/3p-frame';
import * as describes from '../testing/describes';


// All exposed describes.
global.describes = describes;


// Needs to be called before the custom elements are first made.
beforeTest();
Expand Down
263 changes: 98 additions & 165 deletions test/functional/test-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,20 @@ import {
installExtensionsService,
registerExtension,
} from '../../src/service/extensions-impl';
import {adopt} from '../../src/runtime';
import {
createIframePromise,
doNotLoadExternalResourcesInTest,
} from '../../testing/iframe';
import {
initLogConstructor,
resetLogConstructorForTesting,
} from '../../src/log';
import {loadPromise} from '../../src/event-helper';
import * as cust from '../../src/custom-element';
import * as sinon from 'sinon';


describe('Extensions', () => {

let sandbox;

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

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

describe('registerExtension', () => {
describes.sandboxed('Extensions', {}, () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is not needed anymore after you refactoring?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not super needed, no. But I still like the grouping feature since it combines two distinct group of tests inside. The simpler cases will not need it.

describes.fakeWin('registerExtension', {}, env => {
let windowApi;
let extensions;

beforeEach(() => {
const documentElement = document.createElement('div');
const body = document.createElement('div');
const head = document.createElement('head');

const doc = {
documentElement,
body,
head,
};

windowApi = {
document: doc,
};
doc.defaultView = windowApi;

windowApi = env.win;
extensions = new Extensions(windowApi);
});

Expand Down Expand Up @@ -326,160 +294,125 @@ describe('Extensions', () => {
});
});

describe('loadExtension', () => {
describes.realWin('loadExtension', {
amp: true,
fakeRegisterElement: true,
}, env => {
let win, doc, extensions;

beforeEach(() => {
win = env.win;
doc = win.document;
extensions = env.extensions;
});

it('should insert extension script correctly', () => {
return createIframePromise().then(f => {
const win = f.win;
const doc = win.document;
doNotLoadExternalResourcesInTest(win);
adopt(win);
const extensions = installExtensionsService(win);
const stub = sandbox.stub(cust, 'stubElementIfNotKnown');

expect(doc.head.querySelectorAll(
'[custom-element="amp-analytics"]')).to.have.length(0);
expect(extensions.getExtensionHolder_('amp-analytics').scriptPresent)
.to.be.undefined;

extensions.loadExtension('amp-analytics');
expect(doc.head.querySelectorAll(
'[custom-element="amp-analytics"]')).to.have.length(1);
expect(extensions.getExtensionHolder_('amp-analytics').scriptPresent)
.to.be.true;
expect(stub.callCount).to.equal(1);
});
expect(doc.head.querySelectorAll(
'[custom-element="amp-test"]')).to.have.length(0);
expect(extensions.getExtensionHolder_('amp-test').scriptPresent)
.to.be.undefined;

extensions.loadExtension('amp-test');
expect(doc.head.querySelectorAll(
'[custom-element="amp-test"]')).to.have.length(1);
expect(extensions.getExtensionHolder_('amp-test').scriptPresent)
.to.be.true;
expect(win.customElements.elements['amp-test']).to.exist;
});

it('should only insert script once', () => {
return createIframePromise().then(f => {
const win = f.win;
const doc = win.document;
doNotLoadExternalResourcesInTest(win);
adopt(win);
const extensions = installExtensionsService(win);

expect(doc.head.querySelectorAll(
'[custom-element="amp-analytics"]')).to.have.length(0);
expect(extensions.getExtensionHolder_('amp-analytics').scriptPresent)
.to.be.undefined;

extensions.loadExtension('amp-analytics');
expect(doc.head.querySelectorAll('[custom-element="amp-analytics"]'))
.to.have.length(1);
expect(extensions.getExtensionHolder_('amp-analytics').scriptPresent)
.to.be.true;

extensions.loadExtension('amp-analytics');
expect(doc.head.querySelectorAll('[custom-element="amp-analytics"]'))
.to.have.length(1);
});
expect(doc.head.querySelectorAll(
'[custom-element="amp-test"]')).to.have.length(0);
expect(extensions.getExtensionHolder_('amp-test').scriptPresent)
.to.be.undefined;

extensions.loadExtension('amp-test');
expect(doc.head.querySelectorAll('[custom-element="amp-test"]'))
.to.have.length(1);
expect(extensions.getExtensionHolder_('amp-test').scriptPresent)
.to.be.true;

extensions.loadExtension('amp-test');
expect(doc.head.querySelectorAll('[custom-element="amp-test"]'))
.to.have.length(1);
});

it('should not insert when script exists in head', () => {
return createIframePromise().then(f => {
const win = f.win;
const doc = win.document;
doNotLoadExternalResourcesInTest(win);
adopt(win);
const extensions = installExtensionsService(win);
const stub = sandbox.stub(cust, 'stubElementIfNotKnown');

const ampTestScript = doc.createElement('script');
ampTestScript.setAttribute('custom-element', 'amp-analytics');
expect(doc.head.querySelectorAll(
'[custom-element="amp-analytics"]')).to.have.length(0);
doc.head.appendChild(ampTestScript);
expect(doc.head.querySelectorAll(
'[custom-element="amp-analytics"]')).to.have.length(1);
expect(extensions.getExtensionHolder_('amp-analytics').scriptPresent)
.to.be.undefined;

extensions.loadExtension('amp-analytics');
expect(doc.head.querySelectorAll(
'[custom-element="amp-analytics"]')).to.have.length(1);
expect(extensions.getExtensionHolder_('amp-analytics').scriptPresent)
.to.be.true;
expect(stub.callCount).to.equal(0);
});
const ampTestScript = doc.createElement('script');
ampTestScript.setAttribute('custom-element', 'amp-test');
expect(doc.head.querySelectorAll(
'[custom-element="amp-test"]')).to.have.length(0);
doc.head.appendChild(ampTestScript);
expect(doc.head.querySelectorAll(
'[custom-element="amp-test"]')).to.have.length(1);
expect(extensions.getExtensionHolder_('amp-test').scriptPresent)
.to.be.undefined;

extensions.loadExtension('amp-test');
expect(doc.head.querySelectorAll(
'[custom-element="amp-test"]')).to.have.length(1);
expect(extensions.getExtensionHolder_('amp-test').scriptPresent)
.to.be.true;
expect(win.customElements.elements['amp-test']).to.not.exist;
});

it('should give script correct attributes', () => {
return createIframePromise().then(f => {
const win = f.win;
const doc = win.document;
doNotLoadExternalResourcesInTest(win);
adopt(win);
const extensions = installExtensionsService(win);

expect(doc.head.querySelectorAll('[custom-element="amp-analytics"]'))
.to.have.length(0);
extensions.loadExtension('amp-analytics');
expect(doc.head.querySelectorAll('[custom-element="amp-analytics"]'))
.to.have.length(1);

const script = doc.head.querySelector(
'[custom-element="amp-analytics"]');
expect(script.getAttribute('data-script')).to.equal('amp-analytics');
expect(script.getAttribute('async')).to.equal('');
});
expect(doc.head.querySelectorAll('[custom-element="amp-test"]'))
.to.have.length(0);
extensions.loadExtension('amp-test');
expect(doc.head.querySelectorAll('[custom-element="amp-test"]'))
.to.have.length(1);

const script = doc.head.querySelector(
'[custom-element="amp-test"]');
expect(script.getAttribute('data-script')).to.equal('amp-test');
expect(script.getAttribute('async')).to.equal('');
});

it('should insert special-case for amp-embed script', () => {
return createIframePromise().then(f => {
const win = f.win;
const doc = win.document;
doNotLoadExternalResourcesInTest(win);
adopt(win);
const extensions = installExtensionsService(win);

expect(doc.head.querySelectorAll('[custom-element="amp-embed"]'))
.to.have.length(0);

extensions.loadExtension('amp-embed');
expect(doc.head.querySelectorAll('[custom-element="amp-ad"]'))
.to.have.length(1);
expect(extensions.getExtensionHolder_('amp-ad').scriptPresent)
.to.be.true;

// The amp-embed module has never been created.
expect(doc.head.querySelectorAll('[custom-element="amp-embed"]'))
.to.have.length(0);
expect(extensions.extensions_['amp-embed']).to.be.undefined;
});
expect(doc.head.querySelectorAll('[custom-element="amp-embed"]'))
.to.have.length(0);

extensions.loadExtension('amp-embed');
expect(doc.head.querySelectorAll('[custom-element="amp-ad"]'))
.to.have.length(1);
expect(extensions.getExtensionHolder_('amp-ad').scriptPresent)
.to.be.true;

// The amp-embed module has never been created.
expect(doc.head.querySelectorAll('[custom-element="amp-embed"]'))
.to.have.length(0);
expect(extensions.extensions_['amp-embed']).to.be.undefined;
});
});

describe('installExtensionsInChildWindow', () => {

describes.realWin('installExtensionsInChildWindow', {amp: true}, env => {
let parentWin;
let extensions;
let extensionsMock;
let iframe;
let iframeWin;

beforeEach(() => {
return createIframePromise().then(f => {
parentWin = f.win;
extensions = installExtensionsService(parentWin);
extensionsMock = sandbox.mock(extensions);

iframe = parentWin.document.createElement('iframe');
const promise = loadPromise(iframe);
const html = '<div id="one"></div>';
if ('srcdoc' in iframe) {
iframe.srcdoc = html;
} else {
iframe.src = 'about:blank';
const childDoc = iframe.contentWindow.document;
childDoc.open();
childDoc.write(html);
childDoc.close();
}
parentWin.document.body.appendChild(iframe);
return promise.then(() => {
iframeWin = iframe.contentWindow;
});
parentWin = env.win;
extensions = installExtensionsService(parentWin);
extensionsMock = sandbox.mock(extensions);

iframe = parentWin.document.createElement('iframe');
const promise = loadPromise(iframe);
const html = '<div id="one"></div>';
if ('srcdoc' in iframe) {
iframe.srcdoc = html;
} else {
iframe.src = 'about:blank';
const childDoc = iframe.contentWindow.document;
childDoc.open();
childDoc.write(html);
childDoc.close();
}
parentWin.document.body.appendChild(iframe);
return promise.then(() => {
iframeWin = iframe.contentWindow;
});
});

Expand Down