/
safe-emitter.js
43 lines (33 loc) · 1.25 KB
/
safe-emitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @fileoverview Tests for safe-emitter
* @author Teddy Katz
*/
"use strict";
const createEmitter = require("../../../lib/util/safe-emitter");
const assert = require("chai").assert;
describe("safe-emitter", () => {
describe("emit() and on()", () => {
it("allows listeners to be registered calls them when emitted", () => {
const emitter = createEmitter();
const colors = [];
emitter.on("foo", () => colors.push("red"));
emitter.on("foo", () => colors.push("blue"));
emitter.on("bar", () => colors.push("green"));
emitter.emit("foo");
assert.deepEqual(colors, ["red", "blue"]);
emitter.on("bar", color => colors.push(color));
emitter.emit("bar", "yellow");
assert.deepEqual(colors, ["red", "blue", "green", "yellow"]);
});
it("calls listeners with no `this` value", () => {
const emitter = createEmitter();
let called = false;
emitter.on("foo", function() {
assert.strictEqual(this, void 0); // eslint-disable-line no-invalid-this
called = true;
});
emitter.emit("foo");
assert(called);
});
});
});