Skip to content

Commit

Permalink
Remove old tinyevents name
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Apr 9, 2021
1 parent afa73af commit 3325211
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 90 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/serviejs/tinyevents.git"
"url": "git://github.com/serviejs/events.git"
},
"author": {
"name": "Blake Embrey",
"email": "hello@blakeembrey.com",
"url": "http://blakeembrey.me"
},
"homepage": "https://github.com/serviejs/tinyevents",
"homepage": "https://github.com/serviejs/events",
"bugs": {
"url": "https://github.com/serviejs/tinyevents/issues"
"url": "https://github.com/serviejs/events/issues"
},
"main": "dist/index.js",
"scripts": {
Expand Down
172 changes: 85 additions & 87 deletions src/index.spec.ts
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);
});
});

0 comments on commit 3325211

Please sign in to comment.