mirror of
https://git.proxmox.com/git/pve-eslint
synced 2025-08-18 19:00:03 +00:00
52 lines
1.6 KiB
JavaScript
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);
|
|
});
|
|
});
|
|
});
|