summaryrefslogtreecommitdiff
path: root/packages/webapi/test/basic.js
blob: 02284233cb9d2e1ff939145536de40bfabb81391 (plain) (blame)
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
import { assert, test } from '../run/test.setup.js'
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)
			},
		},
	]
})