Skip to content

Commit

Permalink
Document.defaultView needs to be the WorkerDOMGlobalScope (ampproject…
Browse files Browse the repository at this point in the history
…#407)

* Document.defaultView needs to be the WorkerDOMGlobalScope

* Nit formatting of exports

* Glob pattern tweak to prevent tests from running on windows for output/test/*.js

* Move from *.ts to *.test.ts for test files to make glob patters easier to write

* Fix pattern in package.json
  • Loading branch information
kristoferbaxter authored Apr 3, 2019
1 parent 64bf82c commit 48842c6
Show file tree
Hide file tree
Showing 148 changed files with 521 additions and 473 deletions.
33 changes: 0 additions & 33 deletions config/rollup.plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,6 @@ export function babelPlugin({ transpileToES5, allowConsole = false, allowPostMes
});
}

/**
* RollupPlugin that removes the testing document singleton from output source.
*/
export function removeTestingDocument() {
let context;

return {
name: 'remove-testing-document',
buildStart() {
context = this;
},
async renderChunk(code) {
const source = new MagicString(code);
const program = context.parse(code, { ranges: true });

walk.simple(program, {
VariableDeclarator(node) {
if (node.id && node.id.type === 'Identifier' && node.id.name && node.id.name === 'documentForTesting') {
if (node.range) {
source.overwrite(node.range[0], node.range[1], 'documentForTesting = undefined');
}
}
},
});

return {
code: source.toString(),
map: source.generateMap(),
};
},
};
}

/**
* Formats valid output for trimmed ObjectExpressions.
* @param {string} code
Expand Down
43 changes: 2 additions & 41 deletions config/rollup.worker-thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import compiler from '@ampproject/rollup-plugin-closure-compiler';
import { terser } from 'rollup-plugin-terser';
import replace from 'rollup-plugin-replace';
import { babelPlugin, removeTestingDocument } from './rollup.plugins.js';
import { babelPlugin } from './rollup.plugins.js';
import { MINIFY_BUNDLE_VALUE, DEBUG_BUNDLE_VALUE } from './rollup.utils.js';

// Workers do not natively support ES Modules containing `import` or `export` statments.
Expand All @@ -33,7 +33,6 @@ const ESModules = [
sourcemap: true,
},
plugins: [
removeTestingDocument(),
replace({
DEBUG_ENABLED: false,
}),
Expand All @@ -58,7 +57,6 @@ const ESModules = [
sourcemap: true,
},
plugins: [
removeTestingDocument(),
replace({
DEBUG_ENABLED: false,
}),
Expand All @@ -77,7 +75,6 @@ const ESModules = [
sourcemap: true,
},
plugins: [
removeTestingDocument(),
replace({
DEBUG_ENABLED: false,
}),
Expand All @@ -102,7 +99,6 @@ const ESModules = [
sourcemap: true,
},
plugins: [
removeTestingDocument(),
replace({
DEBUG_ENABLED: false,
}),
Expand All @@ -124,7 +120,6 @@ const IIFEModules = [
sourcemap: true,
},
plugins: [
removeTestingDocument(),
replace({
DEBUG_ENABLED: false,
}),
Expand All @@ -149,7 +144,6 @@ const IIFEModules = [
sourcemap: true,
},
plugins: [
removeTestingDocument(),
replace({
DEBUG_ENABLED: false,
}),
Expand All @@ -168,7 +162,6 @@ const IIFEModules = [
sourcemap: true,
},
plugins: [
removeTestingDocument(),
replace({
DEBUG_ENABLED: false,
}),
Expand All @@ -193,7 +186,6 @@ const IIFEModules = [
sourcemap: true,
},
plugins: [
removeTestingDocument(),
replace({
DEBUG_ENABLED: false,
}),
Expand All @@ -205,35 +197,4 @@ const IIFEModules = [
},
];

const debugModules = DEBUG_BUNDLE_VALUE
? [
{
input: 'output/worker-thread/index.js',
output: {
file: 'dist/debug.worker.js',
format: 'iife',
name: 'WorkerThread',
sourcemap: true,
outro: 'window.workerDocument = documentForTesting;',
},
plugins: [
replace({
DEBUG_ENABLED: true,
}),
babelPlugin({
transpileToES5: false,
allowConsole: DEBUG_BUNDLE_VALUE,
allowPostMessage: false,
}),
MINIFY_BUNDLE_VALUE
? compiler({
env: 'CUSTOM',
})
: null,
MINIFY_BUNDLE_VALUE ? terser() : null,
].filter(Boolean),
},
]
: [];

export default [...ESModules, ...IIFEModules, ...debugModules];
export default [...ESModules, ...IIFEModules];
38 changes: 0 additions & 38 deletions demo/debugger/index.html

This file was deleted.

1 change: 0 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ <h3>Basic</h3>
<li><a href='prime-numbers/'>Prime Numbers</a></li>
<li><a href='svg/'>SVG Rendering</a></li>
<li><a href='comments/'>Comment Rendering</a></li>
<li><a href='debugger/'>Debugger</a></li>
</ul>

<h3>Frameworks</h3>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"tsc:build:main": "tsc -p config/tsconfig.build.main-thread.json",
"clean": "rimraf output dist",
"pretest": "npm-run-all clean --parallel tsc:test:*",
"test": "ava -v output/test/**/*.js",
"test": "ava -v output/test/**/*.test.js",
"pre~rollup": "npm-run-all clean --parallel tsc:build:*",
"~rollup": "rollup --config config/rollup.config.js",
"lint:worker": "tslint -c config/tslint.json -p src/worker-thread/",
Expand Down
126 changes: 126 additions & 0 deletions src/test/DocumentCreation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* Copyright 2019 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 { HTMLAnchorElement } from '../worker-thread/dom/HTMLAnchorElement';
import { HTMLButtonElement } from '../worker-thread/dom/HTMLButtonElement';
import { HTMLDataElement } from '../worker-thread/dom/HTMLDataElement';
import { HTMLEmbedElement } from '../worker-thread/dom/HTMLEmbedElement';
import { HTMLFieldSetElement } from '../worker-thread/dom/HTMLFieldSetElement';
import { HTMLFormElement } from '../worker-thread/dom/HTMLFormElement';
import { HTMLIFrameElement } from '../worker-thread/dom/HTMLIFrameElement';
import { HTMLImageElement } from '../worker-thread/dom/HTMLImageElement';
import { HTMLInputElement } from '../worker-thread/dom/HTMLInputElement';
import { HTMLLabelElement } from '../worker-thread/dom/HTMLLabelElement';
import { HTMLLinkElement } from '../worker-thread/dom/HTMLLinkElement';
import { HTMLMapElement } from '../worker-thread/dom/HTMLMapElement';
import { HTMLMeterElement } from '../worker-thread/dom/HTMLMeterElement';
import { HTMLModElement } from '../worker-thread/dom/HTMLModElement';
import { HTMLOListElement } from '../worker-thread/dom/HTMLOListElement';
import { HTMLOptionElement } from '../worker-thread/dom/HTMLOptionElement';
import { HTMLProgressElement } from '../worker-thread/dom/HTMLProgressElement';
import { HTMLQuoteElement } from '../worker-thread/dom/HTMLQuoteElement';
import { HTMLScriptElement } from '../worker-thread/dom/HTMLScriptElement';
import { HTMLSelectElement } from '../worker-thread/dom/HTMLSelectElement';
import { HTMLSourceElement } from '../worker-thread/dom/HTMLSourceElement';
import { HTMLStyleElement } from '../worker-thread/dom/HTMLStyleElement';
import { HTMLTableCellElement } from '../worker-thread/dom/HTMLTableCellElement';
import { HTMLTableColElement } from '../worker-thread/dom/HTMLTableColElement';
import { HTMLTableElement } from '../worker-thread/dom/HTMLTableElement';
import { HTMLTableRowElement } from '../worker-thread/dom/HTMLTableRowElement';
import { HTMLTableSectionElement } from '../worker-thread/dom/HTMLTableSectionElement';
import { HTMLTimeElement } from '../worker-thread/dom/HTMLTimeElement';
import { Document } from '../worker-thread/dom/Document';
import { HydrateableNode } from '../transfer/TransferrableNodes';
import { MutationObserver } from '../worker-thread/MutationObserver';
import { GlobalScope } from '../worker-thread/WorkerDOMGlobalScope';

Object.defineProperty(global, 'ServiceWorkerContainer', {
configurable: true,
value: function() {
return {};
},
});

Object.defineProperty(global, 'StorageManager', {
configurable: true,
value: function() {
return {};
},
});

const GlobalScope: GlobalScope = {
navigator: {
appCodeName: 'Mozilla',
appName: 'Netscape',
appVersion:
'5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Mobile Safari/537.36',
platform: 'MacIntel',
product: 'Gecko',
userAgent:
'Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Mobile Safari/537.36',
serviceWorker: new ServiceWorkerContainer(),
productSub: '20030107',
vendor: 'Google Inc.',
vendorSub: '',
onLine: true,
sendBeacon: () => true,
hardwareConcurrency: 0,
storage: new StorageManager(),
},
localStorage: {},
location: {},
url: '/',
innerWidth: 0,
innerHeight: 0,
initialize: (document: Document, strings: Array<string>, hydrateableNode: HydrateableNode, keys: Array<string>) => void 0,
MutationObserver,
HTMLAnchorElement,
HTMLButtonElement,
HTMLDataElement,
HTMLEmbedElement,
HTMLFieldSetElement,
HTMLFormElement,
HTMLIFrameElement,
HTMLImageElement,
HTMLInputElement,
HTMLLabelElement,
HTMLLinkElement,
HTMLMapElement,
HTMLMeterElement,
HTMLModElement,
HTMLOListElement,
HTMLOptionElement,
HTMLProgressElement,
HTMLQuoteElement,
HTMLScriptElement,
HTMLSelectElement,
HTMLSourceElement,
HTMLStyleElement,
HTMLTableCellElement,
HTMLTableColElement,
HTMLTableElement,
HTMLTableRowElement,
HTMLTableSectionElement,
HTMLTimeElement,
};

export function createTestingDocument(): Document {
const document = new Document(GlobalScope);
document.isConnected = true;
document.appendChild((document.body = document.createElement('body')));

return document;
}
9 changes: 0 additions & 9 deletions src/test/Emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import test from 'ava';
import { Document } from '../worker-thread/dom/Document';
import { MutationFromWorker } from '../transfer/Messages';
import { TransferrableKeys } from '../transfer/TransferrableKeys';
Expand Down Expand Up @@ -61,11 +60,3 @@ export function emitter(document: Document): Emitter {
unsubscribe,
};
}

test('test allowing windows builds to pass', t => {
// Without a test in this file, the transpiled output is picked up by `ava`
// only on Windows systems.
// This means a build will fail because this helper file does not have any found tests.
// EX: 'No tests found in output\test\reflectPropertiesHelper.js'
t.pass();
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
*/

import anyTest, { TestInterface } from 'ava';
import { createDocument } from '../../worker-thread/dom/Document';
import { Element } from '../../worker-thread/dom/Element';
import { Comment } from '../../worker-thread/dom/Comment';
import { TransferrableKeys } from '../../transfer/TransferrableKeys';
import { createTestingDocument } from '../DocumentCreation';

const test = anyTest as TestInterface<{
parent: Element;
comment: Comment;
}>;

test.beforeEach(t => {
const document = createDocument();
const document = createTestingDocument();
const parent = document.createElement('div');
const comment = document.createComment('Super Comment');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

import anyTest, { TestInterface } from 'ava';
import { Comment } from '../../worker-thread/dom/Comment';
import { createDocument } from '../../worker-thread/dom/Document';
import { createTestingDocument } from '../DocumentCreation';

const test = anyTest as TestInterface<{
comment: Comment;
}>;

test.beforeEach(t => {
const document = createDocument();
const document = createTestingDocument();

t.context = {
comment: document.createComment('default value'),
Expand Down
Loading

0 comments on commit 48842c6

Please sign in to comment.