Add support for events
parent
09c7487d34
commit
d77b042299
@ -0,0 +1,39 @@
|
|||||||
|
"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);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue