UNPKG

6.82 kBPlain TextView Raw
1//TODO arbitrary script tags in body
2//TODO bare specifiers should load the file first, the ts file second, and then the node module
3//TODO Have one test for loading arbitrary html files that aren't the index.html file, and all of their dependencies
4import jsverify from 'jsverify-es-module';
5const child_process = require('child_process');
6const uuid = require('uuid/v1');
7const path = require('path');
8const fs = require('fs-extra');
9
10const PAGE_LOADED = 'PAGE_LOADED';
11const GET_RESULT = 'GET_RESULT';
12const RESULT = 'RESULT';
13
14import {
15 arbPort,
16 loadZwitterion,
17 getPromisePieces,
18 arbScriptElementsInfo,
19 wait
20} from './test-utilities';
21
22class ZwitterionTest extends HTMLElement {
23 prepareTests(test: any) {
24 test('Load an arbitrary index html file and all of its scripts', [jsverify.number, arbPort, arbScriptElementsInfo(true)], async (arbNumber: number, arbPort: number, arbScriptElementsInfo: any) => {
25 for (let i=0; i < arbScriptElementsInfo.length; i++) {
26 //TODO if this works, make sure to delete the node_modules created
27 const arbScriptElementInfo = arbScriptElementsInfo[i];
28 // if (arbScriptElementInfo.extension === '' && arbScriptElementInfo.nodeModule) {
29 // await fs.outputFile(`./node_modules/${arbScriptElementInfo.fileName}/${arbScriptElementInfo.fileName}.js`, arbScriptElementInfo.contents);
30 // await fs.outputFile(`./node_modules/${arbScriptElementInfo.fileName}/package.json`, `
31 // {
32 // "main": "./${arbScriptElementInfo.fileName}.js"
33 // }
34 // `);
35 // }
36 // else {
37 await fs.outputFile(arbScriptElementInfo.srcPath, arbScriptElementInfo.contents);
38
39 for (let j=0; j < arbScriptElementInfo.moduleDependencies.length; j++) {
40 const moduleDependency = arbScriptElementInfo.moduleDependencies[j];
41 await fs.outputFile(moduleDependency.srcPath, moduleDependency.contents);
42 }
43 // }
44 }
45
46 const html = `
47 <!DOCTYPE html>
48
49 <html>
50 <head>
51 <script>
52 window.addEventListener('load', (e) => {
53 window.opener.postMessage({
54 type: '${PAGE_LOADED}'
55 });
56 });
57
58 window.addEventListener('message', (e) => {
59 if (e.data === '${GET_RESULT}') {
60 window.opener.postMessage({
61 type: '${RESULT}',
62 body: document.body.innerText,
63 zwitterionTest: window.ZWITTERION_TEST
64 });
65 }
66 });
67 </script>
68
69 ${arbScriptElementsInfo.map((arbScriptElementInfo: any) => {
70 return arbScriptElementInfo.element;
71 }).join('\n')}
72 </head>
73
74 <body>${arbNumber}</body>
75 </html>
76 `;
77 await fs.writeFile('./index.html', html);
78
79 const zwitterionProcess = await loadZwitterion(arbPort);
80 const {thePromise, theResolve} = getPromisePieces();
81 window.addEventListener('message', async (e) => {
82 if (e.data.type === PAGE_LOADED) {
83 // await wait(5000);
84 testWindow.postMessage(GET_RESULT, `http://localhost:${arbPort}`);
85 }
86
87 if (e.data.type === RESULT) {
88 //TODO we really need to remove this event listener
89 const bodyHasCorrectContent = +e.data.body === arbNumber;
90
91 //TODO clean this up so that it is more declarative and understandable
92 const allScriptsExecuted = (e.data.zwitterionTest === undefined && arbScriptElementsInfo.length === 0) ||
93 e.data.zwitterionTest && arbScriptElementsInfo.filter((arbScriptElementInfo: any) => {
94 return e.data.zwitterionTest[arbScriptElementInfo.srcPath] &&
95 arbScriptElementInfo.moduleDependencies.filter((moduleDependency: any) => {
96 return e.data.zwitterionTest[moduleDependency.srcPath];
97 }).length === arbScriptElementInfo.moduleDependencies.length;
98 }).length === arbScriptElementsInfo.length;
99
100 if (
101 bodyHasCorrectContent &&
102 allScriptsExecuted
103 ) {
104 theResolve(true);
105 }
106 else {
107 theResolve(false);
108 }
109 }
110 });
111
112 const testWindow = window.open(`http://localhost:${arbPort}`, '_blank');
113
114 const result = await thePromise;
115
116 // if a test case fails, the last files to be tested will not be deleted
117 // You can then go inspect them and manually run them through Zwitterion to find out what happened
118 if (result) {
119 (<any> zwitterionProcess).kill('SIGINT');
120 await fs.unlink('./index.html');
121 for (let i=0; i < arbScriptElementsInfo.length; i++) {
122 const arbScriptElementInfo = arbScriptElementsInfo[i];
123 await fs.remove(`./${arbScriptElementInfo.topLevelDirectory || arbScriptElementInfo.srcPath}`);
124
125 for (let j=0; j < arbScriptElementInfo.moduleDependencies.length; j++) {
126 const moduleDependency = arbScriptElementInfo.moduleDependencies[j];
127 await fs.remove(`./${moduleDependency.topLevelDirectory || moduleDependency.srcPath}`);
128 }
129 }
130 }
131
132 return result;
133 });
134
135 // test('Load a file that does not exist');
136 // test('Load a file that does not exist with SPA support disabled');
137 }
138}
139
140window.customElements.define('zwitterion-test', ZwitterionTest);