You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
793 B
JavaScript

"use strict";
const expect = require("chai").expect;
const dm = require("../src");
let User = dm.createType("User", {
name: dm.string(),
messages: dm.setOf(dm.string()),
addMessage: dm.guard([dm.string()], dm.nothing(), function (message) {
this.messages.add(message);
this.emit("message:added", message);
})
});
describe("events", () => {
it("should behave like an EventEmitter", () => {
let joe = User({
name: "Joe",
messages: new Set()
});
let messageCounter = 0;
joe.addListener("message:added", (message) => {
messageCounter += 1;
});
joe.addMessage("Hello world!");
expect(messageCounter).to.equal(1);
joe.removeAllListeners("message:added");
joe.addMessage("This should not fire a handler");
expect(messageCounter).to.equal(1);
});
});