-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afa73af
commit 3325211
Showing
2 changed files
with
88 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,120 @@ | ||
import { Emitter, once } from "./index"; | ||
|
||
describe("tiny events", () => { | ||
interface Events { | ||
test: [boolean]; | ||
other: [string]; | ||
} | ||
interface Events { | ||
test: [boolean]; | ||
other: [string]; | ||
} | ||
|
||
describe("events", () => { | ||
it("should emit events", () => { | ||
const events = new Emitter<Events>(); | ||
const spy = jest.fn(); | ||
describe("events", () => { | ||
it("should emit events", () => { | ||
const events = new Emitter<Events>(); | ||
const spy = jest.fn(); | ||
|
||
const off = events.on("test", spy); | ||
events.emit("test", true); | ||
const off = events.on("test", spy); | ||
events.emit("test", true); | ||
|
||
off(); | ||
events.emit("test", false); | ||
off(); | ||
events.emit("test", false); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenLastCalledWith(true); | ||
}); | ||
|
||
it("should remove function only once", () => { | ||
const events = new Emitter<Events>(); | ||
const spy = jest.fn(); | ||
|
||
const off1 = events.on("test", spy); | ||
const off2 = events.on("test", spy); | ||
events.emit("test", true); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenLastCalledWith(true); | ||
}); | ||
|
||
off1(); | ||
off1(); | ||
it("should remove function only once", () => { | ||
const events = new Emitter<Events>(); | ||
const spy = jest.fn(); | ||
|
||
events.emit("test", true); | ||
expect(spy).toHaveBeenCalledTimes(3); | ||
const off1 = events.on("test", spy); | ||
const off2 = events.on("test", spy); | ||
events.emit("test", true); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
|
||
off2(); | ||
off1(); | ||
off1(); | ||
|
||
events.emit("test", false); | ||
expect(spy).toHaveBeenCalledTimes(3); | ||
}); | ||
events.emit("test", true); | ||
expect(spy).toHaveBeenCalledTimes(3); | ||
|
||
it("should emit `each` event", () => { | ||
const events = new Emitter<Events>(); | ||
const spy = jest.fn(); | ||
off2(); | ||
|
||
events.emit("test", false); | ||
expect(spy).not.toHaveBeenCalled(); | ||
events.emit("test", false); | ||
expect(spy).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
const off = events.each(spy); | ||
events.emit("test", true); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
it("should emit `each` event", () => { | ||
const events = new Emitter<Events>(); | ||
const spy = jest.fn(); | ||
|
||
off(); | ||
events.emit("test", false); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
events.emit("test", false); | ||
expect(spy).not.toHaveBeenCalled(); | ||
|
||
it("should forward `each` events easily", () => { | ||
const events1 = new Emitter<Events>(); | ||
const events2 = new Emitter<Pick<Events, "test">>(); | ||
const spy = jest.fn(); | ||
const off = events.each(spy); | ||
events.emit("test", true); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
|
||
events2.each(({ type, args }) => { | ||
events1.emit(type, ...args); | ||
}); | ||
off(); | ||
events.emit("test", false); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
events1.on("test", spy); | ||
events2.emit("test", true); | ||
it("should forward `each` events easily", () => { | ||
const events1 = new Emitter<Events>(); | ||
const events2 = new Emitter<Pick<Events, "test">>(); | ||
const spy = jest.fn(); | ||
|
||
expect(spy).toHaveBeenLastCalledWith(true); | ||
events2.each(({ type, args }) => { | ||
events1.emit(type, ...args); | ||
}); | ||
|
||
it("should filter `each` events easily", () => { | ||
const events1 = new Emitter<Events>(); | ||
const events2 = new Emitter<Pick<Events, "test">>(); | ||
const spy = jest.fn(); | ||
events1.on("test", spy); | ||
events2.emit("test", true); | ||
|
||
events1.each((e) => { | ||
if (e.type === "test") { | ||
events2.emit(e.type, ...e.args); | ||
} | ||
}); | ||
expect(spy).toHaveBeenLastCalledWith(true); | ||
}); | ||
|
||
events2.on("test", spy); | ||
events1.emit("test", true); | ||
events1.emit("other", ""); | ||
it("should filter `each` events easily", () => { | ||
const events1 = new Emitter<Events>(); | ||
const events2 = new Emitter<Pick<Events, "test">>(); | ||
const spy = jest.fn(); | ||
|
||
expect(spy).toHaveBeenLastCalledWith(true); | ||
events1.each((e) => { | ||
if (e.type === "test") { | ||
events2.emit(e.type, ...e.args); | ||
} | ||
}); | ||
|
||
events2.on("test", spy); | ||
events1.emit("test", true); | ||
events1.emit("other", ""); | ||
|
||
expect(spy).toHaveBeenLastCalledWith(true); | ||
}); | ||
}); | ||
|
||
describe("once", () => { | ||
it("should support a once listener", () => { | ||
const events = new Emitter<Events>(); | ||
const spy = jest.fn(); | ||
describe("once", () => { | ||
it("should support a once listener", () => { | ||
const events = new Emitter<Events>(); | ||
const spy = jest.fn(); | ||
|
||
once(events, "test", spy); | ||
once(events, "test", spy); | ||
|
||
events.emit("test", true); | ||
events.emit("test", false); | ||
events.emit("test", true); | ||
events.emit("test", false); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should not break emitter by removing element within listener", () => { | ||
const events = new Emitter<Events>(); | ||
const onSpy = jest.fn(); | ||
const onceSpy = jest.fn(); | ||
it("should not break emitter by removing element within listener", () => { | ||
const events = new Emitter<Events>(); | ||
const onSpy = jest.fn(); | ||
const onceSpy = jest.fn(); | ||
|
||
once(events, "test", onceSpy); | ||
events.on("test", onSpy); | ||
once(events, "test", onceSpy); | ||
events.on("test", onSpy); | ||
|
||
events.emit("test", true); | ||
events.emit("test", true); | ||
|
||
expect(onSpy).toHaveBeenCalledTimes(1); | ||
expect(onceSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
expect(onSpy).toHaveBeenCalledTimes(1); | ||
expect(onceSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |