Files
bun.sh/test/js/deno/event/event-target.test.ts
Jarred Sumner e848c3f226 Get Bun.write tests to pass on Windows and bun:sqlite tests to pass (#8393)
* Move ReadFile and WriteFile to separate file

* Use libuv for Bun.write()

* Update windows_event_loop.zig

* build

* Get bun-write tests to pass. Implement Bun.write with two files.

* UPdate

* Update

* Update failing test list

* update

* More

* More

* More

* More

* Mark the rest

* ok

* oops

* Update bun-write.test.js

* Update blob.zig

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Dave Caruso <me@paperdave.net>
Co-authored-by: Georgijs Vilums <georgijs.vilums@gmail.com>
2024-01-23 20:03:56 -08:00

228 lines
7.3 KiB
TypeScript

// GENERATED - DO NOT EDIT
// Copyright 2018+ the Deno authors. All rights reserved. MIT license.
// https://raw.githubusercontent.com/denoland/deno/main/cli/tests/unit/event_target_test.ts
import { createDenoTest } from "deno:harness";
const { test, assertEquals, assertThrows } = createDenoTest(import.meta.path);
test(function addEventListenerTest() {
const document = new EventTarget();
assertEquals(document.addEventListener("x", null, false), undefined);
assertEquals(document.addEventListener("x", null, true), undefined);
assertEquals(document.addEventListener("x", null), undefined);
});
test(function constructedEventTargetCanBeUsedAsExpected() {
const target = new EventTarget();
const event = new Event("foo", {
bubbles: true,
cancelable: false
});
let callCount = 0;
const listener = (e: Event)=>{
assertEquals(e, event);
++callCount;
};
target.addEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 1);
target.dispatchEvent(event);
assertEquals(callCount, 2);
target.removeEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 2);
});
test(function anEventTargetCanBeSubclassed() {
class NicerEventTarget extends EventTarget {
on(type: string, callback: ((e: Event) => void) | null, options?: AddEventListenerOptions) {
this.addEventListener(type, callback, options);
}
off(type: string, callback: ((e: Event) => void) | null, options?: EventListenerOptions) {
this.removeEventListener(type, callback, options);
}
}
const target = new NicerEventTarget();
new Event("foo", {
bubbles: true,
cancelable: false
});
let callCount = 0;
const listener = ()=>{
++callCount;
};
target.on("foo", listener);
assertEquals(callCount, 0);
target.off("foo", listener);
assertEquals(callCount, 0);
});
test(function removingNullEventListenerShouldSucceed() {
const document = new EventTarget();
assertEquals(document.removeEventListener("x", null, false), undefined);
assertEquals(document.removeEventListener("x", null, true), undefined);
assertEquals(document.removeEventListener("x", null), undefined);
});
test(function constructedEventTargetUseObjectPrototype() {
const target = new EventTarget();
const event = new Event("toString", {
bubbles: true,
cancelable: false
});
let callCount = 0;
const listener = (e: Event)=>{
assertEquals(e, event);
++callCount;
};
target.addEventListener("toString", listener);
target.dispatchEvent(event);
assertEquals(callCount, 1);
target.dispatchEvent(event);
assertEquals(callCount, 2);
target.removeEventListener("toString", listener);
target.dispatchEvent(event);
assertEquals(callCount, 2);
});
test(function toStringShouldBeWebCompatible() {
const target = new EventTarget();
assertEquals(target.toString(), "[object EventTarget]");
});
test(function dispatchEventShouldNotThrowError() {
let hasThrown = false;
try {
const target = new EventTarget();
const event = new Event("hasOwnProperty", {
bubbles: true,
cancelable: false
});
const listener = ()=>{};
target.addEventListener("hasOwnProperty", listener);
target.dispatchEvent(event);
} catch {
hasThrown = true;
}
assertEquals(hasThrown, false);
});
test.ignore(function eventTargetThisShouldDefaultToWindow() {
const { addEventListener , dispatchEvent , removeEventListener } = EventTarget.prototype;
let n = 1;
const event = new Event("hello");
const listener = ()=>{
n = 2;
};
addEventListener("hello", listener);
window.dispatchEvent(event);
assertEquals(n, 2);
n = 1;
removeEventListener("hello", listener);
window.dispatchEvent(event);
assertEquals(n, 1);
window.addEventListener("hello", listener);
dispatchEvent(event);
assertEquals(n, 2);
n = 1;
window.removeEventListener("hello", listener);
dispatchEvent(event);
assertEquals(n, 1);
});
test(function eventTargetShouldAcceptEventListenerObject() {
const target = new EventTarget();
const event = new Event("foo", {
bubbles: true,
cancelable: false
});
let callCount = 0;
const listener = {
handleEvent (e: Event) {
assertEquals(e, event);
++callCount;
}
};
target.addEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 1);
target.dispatchEvent(event);
assertEquals(callCount, 2);
target.removeEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 2);
});
test(function eventTargetShouldAcceptAsyncFunction() {
const target = new EventTarget();
const event = new Event("foo", {
bubbles: true,
cancelable: false
});
let callCount = 0;
const listener = (e: Event)=>{
assertEquals(e, event);
++callCount;
};
target.addEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 1);
target.dispatchEvent(event);
assertEquals(callCount, 2);
target.removeEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 2);
});
test(function eventTargetShouldAcceptAsyncFunctionForEventListenerObject() {
const target = new EventTarget();
const event = new Event("foo", {
bubbles: true,
cancelable: false
});
let callCount = 0;
const listener = {
handleEvent (e: Event) {
assertEquals(e, event);
++callCount;
}
};
target.addEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 1);
target.dispatchEvent(event);
assertEquals(callCount, 2);
target.removeEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 2);
});
test(function eventTargetDispatchShouldSetTargetNoListener() {
const target = new EventTarget();
const event = new Event("foo");
assertEquals(event.target, null);
target.dispatchEvent(event);
assertEquals(event.target, target);
});
test(function eventTargetDispatchShouldSetTargetInListener() {
const target = new EventTarget();
const event = new Event("foo");
assertEquals(event.target, null);
let called = false;
target.addEventListener("foo", (e)=>{
assertEquals(e.target, target);
called = true;
});
target.dispatchEvent(event);
assertEquals(called, true);
});
test(function eventTargetAddEventListenerGlobalAbort() {
return new Promise((resolve)=>{
const c = new AbortController();
c.signal.addEventListener("abort", ()=>resolve());
addEventListener("test", ()=>{}, {
signal: c.signal
});
c.abort();
});
});
test(function eventTargetBrandChecking() {
const self = {};
assertThrows(()=>{
EventTarget.prototype.addEventListener.call(self, "test", null);
}, TypeError);
assertThrows(()=>{
EventTarget.prototype.removeEventListener.call(self, "test", null);
}, TypeError);
assertThrows(()=>{
EventTarget.prototype.dispatchEvent.call(self, new Event("test"));
}, TypeError);
});