From 3a947ccb1bbbab9dc909130db46ace0e876266b7 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 15 Sep 2020 13:21:54 -0700 Subject: [PATCH] Restore deleted jsdom tests (#47600) * Restore deleted jsdom tests Deleted by mistake in the move in #47200 * use approved directory structure --- types/jsdom/jsdom-tests.ts | 2 +- types/jsdom/test/core.ts | 203 +++++++++++++++++++++++++++++++ types/jsdom/ts3.3/jsdom-tests.ts | 2 +- types/jsdom/ts3.4/jsdom-tests.ts | 2 +- types/jsdom/ts3.5/jsdom-tests.ts | 2 +- types/jsdom/tsconfig.json | 1 + 6 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 types/jsdom/test/core.ts diff --git a/types/jsdom/jsdom-tests.ts b/types/jsdom/jsdom-tests.ts index 8cf753bd41..d9fa94f606 100644 --- a/types/jsdom/jsdom-tests.ts +++ b/types/jsdom/jsdom-tests.ts @@ -1,4 +1,4 @@ -import './ts3.5/jsdom-tests'; +import './test/core'; import jsdom = require('jsdom'); const dom = new jsdom.JSDOM(); diff --git a/types/jsdom/test/core.ts b/types/jsdom/test/core.ts new file mode 100644 index 0000000000..2354faf128 --- /dev/null +++ b/types/jsdom/test/core.ts @@ -0,0 +1,203 @@ +import { + JSDOM, + VirtualConsole, + CookieJar, + BaseOptions, + FileOptions, + DOMWindow, + ResourceLoader, + FetchOptions, + ConstructorOptions, +} from 'jsdom'; +import { CookieJar as ToughCookieJar, MemoryCookieStore } from 'tough-cookie'; +import { Script } from 'vm'; + +function test_basic_usage() { + const dom = new JSDOM(`
Hello world
`); + console.log(dom.window.document.querySelector('p')!.textContent); // "Hello world" + + const { window } = new JSDOM(`...`); + // or even + const { document } = new JSDOM(`...`).window; +} + +function test_executing_scripts1() { + const dom = new JSDOM(` + +`); + + // The script will not be executed, by default: + dom.window.document.body.children.length === 1; +} + +function test_executing_scripts2() { + const dom = new JSDOM( + ` + +`, + { runScripts: 'dangerously' }, + ); + + // The script will be executed and modify the DOM: + dom.window.document.body.children.length === 2; +} + +function test_executing_scripts3() { + const window = new JSDOM(``, { runScripts: 'outside-only' }).window; + + window.eval(`document.body.innerHTML = "Hello, world!
";`); + window.document.body.children.length === 1; +} + +function test_virtualConsole() { + const virtualConsole = new VirtualConsole(); + const dom = new JSDOM(``, { virtualConsole }); + + virtualConsole.on('error', () => {}); + virtualConsole.on('warn', () => {}); + virtualConsole.on('info', () => {}); + virtualConsole.on('dir', () => {}); + // ... etc. See https://console.spec.whatwg.org/#logging + + virtualConsole.sendTo(console); + + const c = console; + virtualConsole.sendTo(c, { omitJSDOMErrors: true }); +} + +function test_cookieJar(store: MemoryCookieStore, options: ToughCookieJar.Options) { + const cookieJar: CookieJar = new CookieJar(store, options); + const constructorOptions: ConstructorOptions = { cookieJar }; + const dom = new JSDOM(``, constructorOptions); +} + +function test_beforeParse() { + const dom = new JSDOM(`Hello
`, { + beforeParse(window) { + window.document.childNodes.length === 0; + }, + }); +} + +function test_storageQuota() { + new JSDOM('', { storageQuota: 1337 }); +} + +function test_pretendToBeVisual() { + new JSDOM('', { pretendToBeVisual: true }); +} + +function test_serialize() { + const dom = new JSDOM(`hello`); + + dom.serialize() === 'hello'; + + // Contrast with: + // tslint:disable-next-line no-unnecessary-type-assertion + dom.window.document.documentElement!.outerHTML === 'hello'; +} + +function test_nodeLocation() { + const dom = new JSDOM( + `Hello
+
+
Hello
Hi!`); + + frag.childNodes.length === 2; + frag.querySelector('strong')!.textContent = 'Why hello there!'; + // etc. +} + +function test_fragment_serialization() { + const frag = JSDOM.fragment(`
Hello
`); + if (frag instanceof Element) { + if (frag.firstChild instanceof Element) { + console.log(frag.firstChild.outerHTML); // logs "Hello
" + } + } +} + +function test_custom_resource_loader() { + class CustomResourceLoader extends ResourceLoader { + fetch(url: string, options: FetchOptions) { + if (options.element) { + console.log(`Element ${options.element.localName} is requesting the url ${url}`); + + if (options.element.localName === "iframe") { + console.log("Ignoring resource requested by iframe element"); + return null; + } + } + + return super.fetch(url, options); + } + } + new JSDOM('', { resources: new CustomResourceLoader() }); +} diff --git a/types/jsdom/ts3.3/jsdom-tests.ts b/types/jsdom/ts3.3/jsdom-tests.ts index efd5cf2620..3dec6507f7 100644 --- a/types/jsdom/ts3.3/jsdom-tests.ts +++ b/types/jsdom/ts3.3/jsdom-tests.ts @@ -1,4 +1,4 @@ -import '../jsdom-tests'; +import '../test/core'; import jsdom = require('jsdom'); const dom = new jsdom.JSDOM(); diff --git a/types/jsdom/ts3.4/jsdom-tests.ts b/types/jsdom/ts3.4/jsdom-tests.ts index 3caada5645..928da93bf4 100644 --- a/types/jsdom/ts3.4/jsdom-tests.ts +++ b/types/jsdom/ts3.4/jsdom-tests.ts @@ -1,4 +1,4 @@ -import '../ts3.1/jsdom-tests'; +import '../test/core'; import jsdom = require('jsdom'); const dom = new jsdom.JSDOM(); diff --git a/types/jsdom/ts3.5/jsdom-tests.ts b/types/jsdom/ts3.5/jsdom-tests.ts index 1b9196a0cf..9beeeb70e1 100644 --- a/types/jsdom/ts3.5/jsdom-tests.ts +++ b/types/jsdom/ts3.5/jsdom-tests.ts @@ -1,4 +1,4 @@ -import '../ts3.4/jsdom-tests'; +import '../test/core'; import jsdom = require('jsdom'); const dom = new jsdom.JSDOM(); diff --git a/types/jsdom/tsconfig.json b/types/jsdom/tsconfig.json index 390f0e8364..b92c90845f 100644 --- a/types/jsdom/tsconfig.json +++ b/types/jsdom/tsconfig.json @@ -14,6 +14,7 @@ }, "files": [ "jsdom-tests.ts", + "test/core.ts", "index.d.ts" ] }