From e3b8a8f9dd21ef5c5579608e354b3b1985fc3cc7 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Wed, 12 Feb 2014 02:02:25 +0200 Subject: [PATCH] Move event emitter tests out into their own spec --- spec/eventemitter.js | 67 ++++++++++++++++++++++++++++++++++++++++++++ tests_main.js | 1 + 2 files changed, 68 insertions(+) create mode 100644 spec/eventemitter.js diff --git a/spec/eventemitter.js b/spec/eventemitter.js new file mode 100644 index 000000000..2035439bd --- /dev/null +++ b/spec/eventemitter.js @@ -0,0 +1,67 @@ +(function (root, factory) { + define([ + "mock", + "utils" + ], function (mock, utils) { + return factory(mock, utils); + } + ); +} (this, function (mock, utils) { + return describe("The Converse Event Emitter", $.proxy(function(mock, utils) { + window.localStorage.clear(); + + 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)); +})); diff --git a/tests_main.js b/tests_main.js index 294092558..2bfeb13e8 100644 --- a/tests_main.js +++ b/tests_main.js @@ -115,6 +115,7 @@ require([ "jasmine-console-reporter", "jasmine-junit-reporter", "spec/converse", + "spec/eventemitter", "spec/controlbox", "spec/chatbox", "spec/chatroom"