pve-eslint/eslint/tests/lib/linter/safe-emitter.js
Thomas Lamprecht 609c276fc2 import 8.3.0 source
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2021-12-01 13:39:06 +01:00

52 lines
1.6 KiB
JavaScript

/**
* @fileoverview Tests for safe-emitter
* @author Teddy Katz
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const createEmitter = require("../../../lib/linter/safe-emitter");
const assert = require("chai").assert;
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
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.deepStrictEqual(colors, ["red", "blue"]);
emitter.on("bar", color => colors.push(color));
emitter.emit("bar", "yellow");
assert.deepStrictEqual(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 -- Checking `this` value
called = true;
});
emitter.emit("foo");
assert(called);
});
});
});