-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move EventEmitter to EventEmitter.ts and add tests
- Loading branch information
Showing
6 changed files
with
249 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"deno.enable": true, | ||
"deno.lint": true, | ||
"deno.unstable": true, | ||
"deno.config": "../deno.jsonc" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import type { CustomEventMap, TypedCustomEvent } from "./EventEmitter.ts"; | ||
import { | ||
assertEquals, | ||
assertThrows, | ||
fail, | ||
} from "https://deno.land/std@0.92.0/testing/asserts.ts"; | ||
|
||
import { EventEmitter } from "./EventEmitter.ts"; | ||
|
||
interface Events extends CustomEventMap { | ||
ping: TypedCustomEvent<"ping", undefined>; | ||
pong: TypedCustomEvent<"pong", string>; | ||
peng: TypedCustomEvent<"peng", { data: string }>; | ||
} | ||
|
||
Deno.test("types of detail", () => { | ||
const emitter = new EventEmitter<Events>(); | ||
|
||
emitter.on("ping", (event) => { | ||
assertEquals(event.detail, undefined); | ||
}); | ||
|
||
emitter.emit("ping"); | ||
|
||
emitter.on("pong", (event) => { | ||
assertEquals(event.detail, "hello"); | ||
}); | ||
|
||
emitter.emit("pong", "hello"); | ||
|
||
emitter.on("peng", (event) => { | ||
assertEquals(event.detail.data, "peng emitted!") | ||
}); | ||
|
||
emitter.emit("peng", { | ||
data: "peng emitted!", | ||
}); | ||
}); | ||
|
||
/** | ||
* @see https://deno.land/x/event@2.0.0/test.ts | ||
*/ | ||
|
||
Deno.test("on", () => { | ||
const ee = new EventEmitter<Events>(); | ||
|
||
ee.on("foo", (event) => { | ||
assertEquals(event.detail, "bar"); | ||
}); | ||
|
||
ee.emit("foo", "bar"); | ||
}); | ||
|
||
Deno.test("once", () => { | ||
const ee = new EventEmitter<Events>(); | ||
|
||
ee.once("foo", (event) => { | ||
assertEquals(event.detail, "bar"); | ||
}); | ||
|
||
ee.emit("foo", "bar"); | ||
}); | ||
|
||
Deno.test("off", () => { | ||
const ee = new EventEmitter<Events>(); | ||
|
||
function foo() { | ||
fail(); | ||
} | ||
|
||
ee.on("foo", foo); | ||
|
||
ee.off("foo", foo); | ||
|
||
ee.emit("foo", "bar"); | ||
}); | ||
|
||
Deno.test("offEvent", () => { | ||
const ee = new EventEmitter<Events>(); | ||
|
||
let i = 0; | ||
|
||
ee.on("foo", () => i++); | ||
|
||
ee.on("foo", () => i++); | ||
|
||
ee.off(); | ||
|
||
ee.emit("foo", "bar"); | ||
|
||
assertEquals(i, 0); | ||
}); | ||
|
||
Deno.test("offAll", () => { | ||
const ee = new EventEmitter<Events>(); | ||
|
||
let i = 0; | ||
|
||
ee.on("foo", () => i++); | ||
|
||
ee.on("bar", () => i++); | ||
|
||
ee.off(); | ||
|
||
ee.emit("foo", "bar"); | ||
|
||
assertEquals(i, 0); | ||
}); | ||
|
||
Deno.test("chainable", () => { | ||
const ee = new EventEmitter<Events>(); | ||
|
||
function foo() { | ||
fail(); | ||
} | ||
|
||
ee | ||
.on("foo", foo) | ||
.off("foo", foo); | ||
|
||
ee.emit("foo", "bar"); | ||
}); |
Oops, something went wrong.