-
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.
Queue starting/stopping notifications to avoid making a mess.
- Loading branch information
1 parent
b5d093d
commit 2dcce6b
Showing
5 changed files
with
238 additions
and
46 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
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
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,114 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { TrackingEventTarget } from "./events.js"; | ||
|
||
class TestTrackingEventTarget extends TrackingEventTarget { | ||
constructor( | ||
private activate: (type: string) => void, | ||
private deactivate: (type: string) => void, | ||
) { | ||
super(); | ||
} | ||
public getActiveEvents(): string[] { | ||
return super.getActiveEvents(); | ||
} | ||
protected eventActivated(type: string): void { | ||
this.activate(type); | ||
} | ||
protected eventDeactivated(type: string): void { | ||
this.deactivate(type); | ||
} | ||
} | ||
|
||
describe("TrackingEventTarget", () => { | ||
const listener = () => {}; | ||
|
||
it("add remove", () => { | ||
const activate = vi.fn(); | ||
const deactivate = vi.fn(); | ||
const target = new TestTrackingEventTarget(activate, deactivate); | ||
expect(target.getActiveEvents()).toEqual([]); | ||
|
||
target.addEventListener("foo", listener); | ||
expect(activate).toBeCalledTimes(1); | ||
expect(deactivate).toBeCalledTimes(0); | ||
expect(target.getActiveEvents()).toEqual(["foo"]); | ||
|
||
target.removeEventListener("foo", listener); | ||
expect(activate).toBeCalledTimes(1); | ||
expect(deactivate).toBeCalledTimes(1); | ||
expect(target.getActiveEvents()).toEqual([]); | ||
}); | ||
|
||
it("callback equality", () => { | ||
const listenerAlt = () => {}; | ||
|
||
const activate = vi.fn(); | ||
const deactivate = vi.fn(); | ||
const target = new TestTrackingEventTarget(activate, deactivate); | ||
expect(target.getActiveEvents()).toEqual([]); | ||
|
||
target.addEventListener("foo", listenerAlt); | ||
target.addEventListener("foo", listener); | ||
target.addEventListener("foo", listener); | ||
target.removeEventListener("foo", listener); | ||
expect(target.getActiveEvents()).toEqual(["foo"]); | ||
target.removeEventListener("foo", listenerAlt); | ||
expect(target.getActiveEvents()).toEqual([]); | ||
}); | ||
|
||
it("option equality - capture", () => { | ||
const fooListener = vi.fn(); | ||
const activate = vi.fn(); | ||
const deactivate = vi.fn(); | ||
const target = new TestTrackingEventTarget(activate, deactivate); | ||
expect(target.getActiveEvents()).toEqual([]); | ||
|
||
target.addEventListener("foo", fooListener, { capture: true }); | ||
target.addEventListener("foo", fooListener, false); | ||
target.removeEventListener("foo", fooListener, true); | ||
expect(target.getActiveEvents()).toEqual(["foo"]); | ||
target.dispatchEvent(new Event("foo")); | ||
expect(fooListener).toBeCalledTimes(1); | ||
}); | ||
|
||
it("option equality", () => { | ||
const fooListener = vi.fn(); | ||
const activate = vi.fn(); | ||
const deactivate = vi.fn(); | ||
const target = new TestTrackingEventTarget(activate, deactivate); | ||
|
||
// Despite MDN docs claiming all options can result in another listener added | ||
// it seems only capture counts for both add and remove | ||
target.addEventListener("foo", fooListener, { passive: true }); | ||
target.addEventListener("foo", fooListener, { once: true }); | ||
target.addEventListener("foo", fooListener, { capture: true }); | ||
target.addEventListener("foo", fooListener, { capture: false }); | ||
target.dispatchEvent(new Event("foo")); | ||
expect(fooListener).toBeCalledTimes(2); | ||
|
||
target.removeEventListener("foo", fooListener, true); | ||
expect(target.getActiveEvents()).toEqual(["foo"]); | ||
target.dispatchEvent(new Event("foo")); | ||
expect(fooListener).toBeCalledTimes(3); | ||
|
||
target.removeEventListener("foo", fooListener, false); | ||
expect(target.getActiveEvents()).toEqual([]); | ||
target.dispatchEvent(new Event("foo")); | ||
expect(fooListener).toBeCalledTimes(3); | ||
}); | ||
|
||
it("once", () => { | ||
const fooListener = vi.fn(); | ||
const activate = vi.fn(); | ||
const deactivate = vi.fn(); | ||
const target = new TestTrackingEventTarget(activate, deactivate); | ||
|
||
target.addEventListener("foo", fooListener, { once: true }); | ||
target.dispatchEvent(new Event("foo")); | ||
expect(fooListener).toBeCalledTimes(1); | ||
expect(deactivate).toBeCalledTimes(1); | ||
|
||
target.dispatchEvent(new Event("foo")); | ||
expect(fooListener).toBeCalledTimes(1); | ||
}); | ||
}); |
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
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