diff options
author | 2022-04-10 22:29:46 -0300 | |
---|---|---|
committer | 2022-04-10 18:29:46 -0700 | |
commit | 1907255ca239a94b76b1fe3a844a35f0436b8e3d (patch) | |
tree | f4c9935e0ea92f07478fece6a60b85795b24143a /packages/webapi/test/basic.js | |
parent | 47f20a189f5479b5e84f99e6feda3be7080e455f (diff) | |
download | astro-1907255ca239a94b76b1fe3a844a35f0436b8e3d.tar.gz astro-1907255ca239a94b76b1fe3a844a35f0436b8e3d.tar.zst astro-1907255ca239a94b76b1fe3a844a35f0436b8e3d.zip |
chore: webapi test now use chai (#3048)
Diffstat (limited to 'packages/webapi/test/basic.js')
-rw-r--r-- | packages/webapi/test/basic.js | 393 |
1 files changed, 182 insertions, 211 deletions
diff --git a/packages/webapi/test/basic.js b/packages/webapi/test/basic.js index 2ea4a613b..5ee48d033 100644 --- a/packages/webapi/test/basic.js +++ b/packages/webapi/test/basic.js @@ -1,214 +1,185 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - polyfill(globalThis) - - return [ - { - name: 'Globals exist', - test() { - const webAPIs = [ - 'AbortController', - 'AbortSignal', - 'Blob', - 'ByteLengthQueuingStrategy', - 'CSSStyleSheet', - 'CountQueuingStrategy', - 'CustomElementRegistry', - 'CustomEvent', - 'DOMException', - 'Document', - 'DocumentFragment', - 'Element', - 'Event', - 'EventTarget', - 'File', - 'FormData', - 'HTMLDocument', - 'HTMLElement', - 'HTMLDivElement', - 'HTMLHeadElement', - 'HTMLHtmlElement', - 'HTMLImageElement', - 'HTMLStyleElement', - 'HTMLTemplateElement', - 'HTMLUnknownElement', - 'Headers', - 'IntersectionObserver', - 'Image', - 'MediaQueryList', - 'MutationObserver', - 'Node', - 'ReadableByteStreamController', - 'ReadableStream', - 'ReadableStreamBYOBReader', - 'ReadableStreamBYOBRequest', - 'ReadableStreamDefaultController', - 'ReadableStreamDefaultReader', - 'Request', - 'Response', - 'ShadowRoot', - 'StyleSheet', - 'TransformStream', - 'WritableStream', - 'WritableStreamDefaultController', - 'WritableStreamDefaultWriter', - 'Window', - 'cancelAnimationFrame', - 'cancelIdleCallback', - 'clearTimeout', - 'fetch', - 'requestAnimationFrame', - 'requestIdleCallback', - 'setTimeout', - ] - - for (const name of webAPIs) { - assert.equal(typeof globalThis[name], 'function') - } - }, - }, - { - name: 'Constructs an Event', - test() { - const e = new Event('test') - - assert.equal(e.type, 'test') - }, - }, - { - name: 'Constructs an EventTarget', - test() { - const t = new EventTarget() - }, - }, - { - name: 'Dispatches an Event on an EventTarget', - test() { - const t = new EventTarget() - - let pass = false - - t.addEventListener('test', (event) => { - pass = true - }) - - const e = new Event('test') - - t.dispatchEvent(e) - - assert.equal(pass, true) - }, - }, - { - name: 'Classes extend as expected', - test() { - assert.equal(HTMLElement.prototype instanceof Element, true) - assert.equal(Element.prototype instanceof Node, true) - assert.equal(Node.prototype instanceof EventTarget, true) - }, - }, - { - name: 'DOM Methods have no effect', - test() { - const element = document.createElement('div') - - assert.equal(element.innerHTML, '') - element.innerHTML = 'frozen' - assert.equal(element.innerHTML, '') - - assert.equal(element.textContent, '') - element.textContent = 'frozen' - assert.equal(element.textContent, '') - }, - }, - { - name: 'globalThis.window === globalThis', - test() { - assert.equal(globalThis.window, globalThis) - }, - }, - { - name: 'Relative Indexing Method (String#at)', - test() { - assert.equal(typeof String.prototype.at, 'function') - assert.equal(String.prototype.at.length, 1) - - const example = 'The quick brown fox jumps over the lazy dog.' - - assert.equal(example.at(2), 'e') - assert.equal(example.at(-2), 'g') - }, - }, - { - name: 'Relative Indexing Method (Array#at)', - test() { - assert.equal(typeof Array.prototype.at, 'function') - assert.equal(Array.prototype.at.length, 1) - - const example = [1, 3, 5, 7, 9] - - assert.equal(example.at(1), 3) - assert.equal(example.at(-1), 9) - }, - }, - { - name: 'Relative Indexing Method (TypedArray#at)', - test() { - assert.equal(typeof Int8Array.prototype.at, 'function') - assert.equal(Int8Array.prototype.at.length, 1) - - const example = new Int8Array([1, 3, 5, 7, 9]) - - assert.equal(example.at(1), 3) - assert.equal(example.at(-1), 9) - }, - }, - { - name: 'Object.hasOwn', - test() { - assert.equal(typeof Object.hasOwn, 'function') - assert.equal(Object.hasOwn.length, 2) - - const example = {} - - assert.equal(Object.hasOwn(example, 'prop'), false) - - example.prop = 'exists' - - assert.equal(Object.hasOwn(example, 'prop'), true) - }, - }, - { - name: 'Promise.any', - test() { - assert.equal(typeof Promise.any, 'function') - assert.equal(Promise.any.length, 1) - - Promise.any([ - Promise.resolve(42), - Promise.reject(-1), - Promise.reject(Infinity), - ]).then((result) => { - assert.equal(result, 42) - }) - }, - }, - { - name: 'String#replaceAll', - test() { - assert.equal(typeof String.prototype.replaceAll, 'function') - assert.equal(String.prototype.replaceAll.length, 2) - - const t1 = - 'Of all the sorcerers in Harry Potter, Halo is my favorite sorcerer.' - const t2 = t1.replaceAll('sorcerer', 'philosopher') - const t3 = - 'Of all the philosophers in Harry Potter, Halo is my favorite philosopher.' - - assert.equal(t2, t3) - }, - }, - ] +describe('Basic', () => { + before(() => polyfill(globalThis)) + + it('Globals exist', () => { + const webAPIs = [ + 'AbortController', + 'AbortSignal', + 'Blob', + 'ByteLengthQueuingStrategy', + 'CSSStyleSheet', + 'CountQueuingStrategy', + 'CustomElementRegistry', + 'CustomEvent', + 'DOMException', + 'Document', + 'DocumentFragment', + 'Element', + 'Event', + 'EventTarget', + 'File', + 'FormData', + 'HTMLDocument', + 'HTMLElement', + 'HTMLDivElement', + 'HTMLHeadElement', + 'HTMLHtmlElement', + 'HTMLImageElement', + 'HTMLStyleElement', + 'HTMLTemplateElement', + 'HTMLUnknownElement', + 'Headers', + 'IntersectionObserver', + 'Image', + 'MediaQueryList', + 'MutationObserver', + 'Node', + 'ReadableByteStreamController', + 'ReadableStream', + 'ReadableStreamBYOBReader', + 'ReadableStreamBYOBRequest', + 'ReadableStreamDefaultController', + 'ReadableStreamDefaultReader', + 'Request', + 'Response', + 'ShadowRoot', + 'StyleSheet', + 'TransformStream', + 'WritableStream', + 'WritableStreamDefaultController', + 'WritableStreamDefaultWriter', + 'Window', + 'cancelAnimationFrame', + 'cancelIdleCallback', + 'clearTimeout', + 'fetch', + 'requestAnimationFrame', + 'requestIdleCallback', + 'setTimeout', + ] + + for (const name of webAPIs) { + expect(globalThis[name]).to.be.a('function') + } + }) + + it('Constructs an Event', () => { + const e = new Event('test') + + expect(e.type).to.equal('test') + }) + + it('Constructs an EventTarget', () => { + const _t = new EventTarget() + }) + + it('Dispatches an Event on an EventTarget', () => { + const t = new EventTarget() + + let pass = false + + t.addEventListener('test', (event) => { + pass = true + }) + + const e = new Event('test') + + t.dispatchEvent(e) + + expect(pass).to.equal(true) + }) + + it('Classes extend as expected', () => { + expect(HTMLElement.prototype).to.be.an.instanceof(Element) + expect(Element.prototype).to.be.an.instanceof(Node) + expect(Node.prototype).to.be.an.instanceof(EventTarget) + }) + + it('DOM Methods have no effect', () => { + const element = document.createElement('div') + + expect(element.innerHTML).to.be.empty + element.innerHTML = 'frozen' + expect(element.innerHTML).to.be.empty + + expect(element.textContent).to.be.empty + element.textContent = 'frozen' + expect(element.textContent).to.be.empty + }) + + it('globalThis.window === globalThis', () => { + expect(globalThis.window).to.equal(globalThis) + }) + + it('Relative Indexing Method (String#at)', () => { + expect(String.prototype.at).to.be.a('function') + expect(String.prototype.at.length).to.equal(1) + + const example = 'The quick brown fox jumps over the lazy dog.' + + expect(example.at(2)).to.equal('e') + expect(example.at(-2)).to.equal('g') + }) + + it('Relative Indexing Method (Array#at)', () => { + expect(Array.prototype.at).to.be.a('function') + expect(Array.prototype.at.length).to.equal(1) + + const example = [1, 3, 5, 7, 9] + + expect(example.at(1)).to.equal(3) + expect(example.at(-1)).to.equal(9) + }) + + it('Relative Indexing Method (TypedArray#at)', () => { + expect(Int8Array.prototype.at).to.be.a('function') + expect(Int8Array.prototype.at.length).to.equal(1) + + const example = new Int8Array([1, 3, 5, 7, 9]) + + expect(example.at(1)).to.equal(3) + expect(example.at(-1)).to.equal(9) + }) + + it('Object.hasOwn', () => { + expect(Object.hasOwn).to.be.a('function') + expect(Object.hasOwn.length).to.equal(2) + + const example = {} + + expect(Object.hasOwn(example, 'prop')).to.equal(false) + + example.prop = 'exists' + + expect(Object.hasOwn(example, 'prop')).to.equal(true) + }) + + it('Promise.any', () => { + expect(Promise.any).to.be.a('function') + expect(Promise.any.length).to.equal(1) + + Promise.any([ + Promise.resolve(42), + Promise.reject(-1), + Promise.reject(Infinity), + ]).then((result) => { + expect(result).to.equal(42) + }) + }) + + it('String#replaceAll', () => { + expect(String.prototype.replaceAll).to.be.a('function') + expect(String.prototype.replaceAll.length).to.equal(2) + + const t1 = + 'Of all the sorcerers in Harry Potter, Halo is my favorite sorcerer.' + const t2 = t1.replaceAll('sorcerer', 'philosopher') + const t3 = + 'Of all the philosophers in Harry Potter, Halo is my favorite philosopher.' + + expect(t2).to.equal(t3) + }) }) |