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