-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
236 lines (166 loc) · 5.75 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"use strict";
const {after, before, beforeEach, describe, it} = require("mocha");
const cartesianProduct = require("cartesian-product");
const puppeteer = require("puppeteer");
const puppeteerCoverage = require("puppeteer-to-istanbul");
let browser, page;
const getDocumentNodes = realms => mapRealms(realms, ({document}) => document);
const getDocumentTypeNodes = (realms, qualifiedNameStr="html", publicId="", systemId="") => mapRealms(realms, ({document}) =>
{
return document.implementation.createDocumentType(qualifiedNameStr, publicId, systemId);
});
const mapRealms = (realms, callback) => (Array.isArray(realms) ? realms : [realms]).map(callback);
// @todo https://github.com/tc39/proposal-optional-chaining
const removeDoctypes = realms => realms.forEach(({document}) => document.doctype && document.doctype.remove());
const resetDoctypes = realms => realms.forEach(realm =>
{
const {document, originalDoctype} = realm;
// @todo https://github.com/tc39/proposal-optional-chaining
document.doctype && document.doctype.remove();
document.documentElement.before(originalDoctype);
});
const runInBrowser = func => async () =>
{
const realms = await page.evaluateHandle(() => [globalThis, iframeGlobalThis]);
return page.evaluate(func, realms);
};
// @todo also use npmjs.com/puppeteer-firefox
before(async () =>
{
browser = await puppeteer.launch({ args: ["--no-sandbox"] });
page = await browser.newPage();
page.on("console", async msg => console[msg._type](...await Promise.all(msg.args().map(arg => arg.jsonValue()))));
page.on("pageerror", console.error);
await Promise.all(
[
page.addScriptTag({ path: "node_modules/chai/chai.js" }),
page.addScriptTag({ path: "temp.js" }),
// @todo https://github.com/GoogleChrome/puppeteer/issues/5108
page.addScriptTag(
{
content: Object.entries(
{
cartesianProduct,
getDocumentNodes,
getDocumentTypeNodes,
mapRealms,
removeDoctypes,
resetDoctypes
})
.map(([name, value]) => `globalThis.${name} = ${value.toString()}`)
.join(";\n")
}),
// @todo https://github.com/GoogleChrome/puppeteer/issues/3570
page.coverage.startJSCoverage({ reportAnonymousScripts: true })
]);
await page.evaluate(() =>
{
globalThis.expect = chai.expect;
delete globalThis.chai; // cleanup
// Add Realm
const iframe = document.createElement("iframe");
document.body.append(iframe);
globalThis.iframeGlobalThis = iframe.contentWindow;
globalThis.originalDoctype = getDocumentTypeNodes(globalThis)[0];
iframeGlobalThis.originalDoctype = getDocumentTypeNodes(iframeGlobalThis)[0];
});
});
beforeEach(runInBrowser(realms => resetDoctypes(realms)));
after(async () =>
{
let coverage = await page.coverage.stopJSCoverage();
// Exclude tools
coverage = coverage.filter(({url}) => !url.includes("chai"));
puppeteerCoverage.write(coverage);
await browser.close();
});
it("is a (bundled) function", runInBrowser(() =>
{
expect(assignDoctype).to.be.a("function");
}));
it("accepts a DocumentType & Document node from any Realm", runInBrowser(realms =>
{
const fixtureArgs = cartesianProduct(
[
getDocumentTypeNodes(realms),
getDocumentNodes(realms)
]);
fixtureArgs.forEach(args => expect(() => assignDoctype(...args)).not.to.throw());
}));
it("rejects a non-DocumentType & non-Document node from any Realm", runInBrowser(realms =>
{
const documents = getDocumentNodes(realms);
const documentTypes = getDocumentTypeNodes(realms);
const others = realms
.map(realm =>
[
"string",
new String("string"),
Symbol(),
{},
[],
/regex/,
true,
false,
0,
new Number(0),
1,
new Number(1),
1n,
NaN,
// null is supported
undefined,
realm,
realm.document.createComment("data"),
realm.document.createDocumentFragment(),
realm.document.createElement("div"),
realm.document.createProcessingInstruction("target", "data"),
realm.document.createTextNode("data"),
realm.document.implementation.createDocument(null, "root-node").createCDATASection("data")
])
.flat();
const fixtures = [...documents, ...documentTypes, ...others];
cartesianProduct([fixtures, fixtures])
.filter(([documentType, document]) => !documents.includes(document) || !documentTypes.includes(documentType))
.forEach(args => expect(() => assignDoctype(...args)).to.throw(TypeError));
}));
it("inserts a cross- and same-Realm DocumentType node", runInBrowser(realms =>
{
const documents = getDocumentNodes(realms);
const documentTypes = getDocumentTypeNodes(realms);
cartesianProduct([documentTypes, documents]).forEach(([documentType, document]) =>
{
removeDoctypes(realms);
expect(assignDoctype(documentType, document)).to.equal(null);
expect(document.doctype).to.equal(documentType);
});
}));
it("replaces a cross- and same-Realm DocumentType node", runInBrowser(realms =>
{
const documents = getDocumentNodes(realms);
const documentTypes = getDocumentTypeNodes(realms);
cartesianProduct([documentTypes, documents]).forEach(([documentType, document]) =>
{
resetDoctypes(realms);
expect(assignDoctype(documentType, document)).to.equal(document.defaultView.originalDoctype);
expect(document.doctype).to.equal(documentType);
});
}));
it("removes an existing DocumentType node when its replacement is null", runInBrowser(realms =>
{
getDocumentNodes(realms).forEach(document =>
{
resetDoctypes(realms);
expect(assignDoctype(null, document)).to.equal(document.defaultView.originalDoctype);
expect(document.doctype).to.equal(null);
});
}));
it("does nothing when there is no existing DocumentType node and its replacement is null", runInBrowser(realms =>
{
getDocumentNodes(realms).forEach(document =>
{
removeDoctypes(realms);
expect(assignDoctype(null, document)).to.equal(null);
expect(document.doctype).to.equal(null);
});
}));