From c8bf0491c8bd1fac019b8b92fb1a5d4268a00228 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Mon, 16 Dec 2013 20:00:21 +0200 Subject: [PATCH] Add new spec to test converse.js's API. Fixes #48 --- spec/converse.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 spec/converse.js diff --git a/spec/converse.js b/spec/converse.js new file mode 100644 index 000000000..fb348e860 --- /dev/null +++ b/spec/converse.js @@ -0,0 +1,65 @@ +(function (root, factory) { + define([ + "mock", + "utils" + ], function (mock, utils) { + return factory(mock, utils); + } + ); +} (this, function (mock, utils) { + return describe("Converse", $.proxy(function(mock, utils) { + it("allows you to subscribe to emitted events", function () { + this.callback = function () {}; + spyOn(this, 'callback'); + converse.on('onInitialized', this.callback); + converse.emit('onInitialized'); + expect(this.callback).toHaveBeenCalled(); + converse.emit('onInitialized'); + expect(this.callback.callCount, 2); + converse.emit('onInitialized'); + expect(this.callback.callCount, 3); + }); + + it("allows you to listen once for an emitted event", function () { + this.callback = function () {}; + spyOn(this, 'callback'); + converse.once('onInitialized', this.callback); + converse.emit('onInitialized'); + expect(this.callback).toHaveBeenCalled(); + converse.emit('onInitialized'); + expect(this.callback.callCount, 1); + converse.emit('onInitialized'); + expect(this.callback.callCount, 1); + }); + + it("allows you to stop listening or subscribing to an event", function () { + this.callback = function () {}; + this.anotherCallback = function () {}; + this.neverCalled = function () {}; + + spyOn(this, 'callback'); + spyOn(this, 'anotherCallback'); + spyOn(this, 'neverCalled'); + converse.on('onInitialized', this.callback); + converse.on('onInitialized', this.anotherCallback); + + converse.emit('onInitialized'); + expect(this.callback).toHaveBeenCalled(); + expect(this.anotherCallback).toHaveBeenCalled(); + + converse.off('onInitialized', this.callback); + + converse.emit('onInitialized'); + expect(this.callback.callCount, 1); + expect(this.anotherCallback.callCount, 2); + + converse.once('onInitialized', this.neverCalled); + converse.off('onInitialized', this.neverCalled); + + converse.emit('onInitialized'); + expect(this.callback.callCount, 1); + expect(this.anotherCallback.callCount, 3); + expect(this.neverCalled).not.toHaveBeenCalled(); + }); + }, converse, mock, utils)); +}));