diff --git a/CHANGELOG.md b/CHANGELOG.md index 1506cb2..512da4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +## v3.2.4 +- Improve tests and coverage + ## v3.2.3 - Return a new instance of `PubSub` if it is invoked without the `new` keyword. - Add code coverage. diff --git a/dist/pubsub.min.js b/dist/pubsub.min.js index 5ba992c..5d9d4a0 100644 --- a/dist/pubsub.min.js +++ b/dist/pubsub.min.js @@ -2,7 +2,7 @@ * PubSub * Javascript implementation of the Publish/Subscribe pattern. * - * @version 3.2.3 + * @version 3.2.4 * @author George Raptis (georapbox.github.io) * @homepage https://github.com/georapbox/PubSub#readme * @repository https://github.com/georapbox/PubSub.git diff --git a/package.json b/package.json index 4d587b1..93ab98f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "PubSub", - "version": "3.2.3", + "version": "3.2.4", "description": "Javascript implementation of the Publish/Subscribe pattern.", "main": "src/pubsub.js", "scripts": { diff --git a/src/pubsub.js b/src/pubsub.js index f980d9c..c37d29b 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -2,7 +2,7 @@ * PubSub.js * Javascript implementation of the Publish/Subscribe pattern. * - * @version 3.2.3 + * @version 3.2.4 * @author George Raptis (georapbox.github.io) * @homepage https://github.com/georapbox/PubSub#readme * @repository git+https://github.com/georapbox/PubSub.git diff --git a/tests/pubsub.specs.js b/tests/pubsub.specs.js index e79ab53..7d7f45c 100644 --- a/tests/pubsub.specs.js +++ b/tests/pubsub.specs.js @@ -1,13 +1,13 @@ /* eslint-disable strict, no-unused-vars, no-use-before-define, new-cap */ -describe('PubSub instance', function () { +describe('Should return a new PubSub instance', function () { it('Creates a new instance of PubSub.', function () { var pubsub = new PubSub(); expect(pubsub).not.toBeNull(); expect(pubsub instanceof PubSub); }); - it('Should return a new instance of PubSub if new keyword is omitted.', function () { + it('Should return a new instance of PubSub if new keyword is omitted', function () { var pubsub = PubSub(); expect(pubsub).not.toBeNull(); expect(pubsub instanceof PubSub); @@ -15,7 +15,7 @@ describe('PubSub instance', function () { }); // Subscribe scenarios. -describe('Subscribe to an event.', function () { +describe('Subscribe/Register to an event', function () { it('Subscribes to an event called: "example-event".', function () { var ps = new PubSub(); @@ -23,15 +23,35 @@ describe('Subscribe to an event.', function () { expect(ps.subscribe('example-event', listener)).toBe(0); }); + + it('Should subscribes to an event only once', function () { + var ps = new PubSub(); + + function listener() {} + + ps.subscribeOnce('one-time-event', listener); + + ps.publishSync('one-time-event'); // Publish once + + expect(ps.hasSubscribers('one-time-event')).toBe(false); + }); + + it('Should throw exception if a callback is not provided', function () { + var ps = new PubSub(); + + expect(function () { + return ps.subscribe('example-event'); + }).toThrow(); + }); }); // Publish scenarios. -describe('Publish/Emmit an event.', function () { +describe('Publish/Emmit an event', function () { var ps = new PubSub(); function listener() {} ps.subscribe('example-event', listener); - it('Publishes "example-event" passing data: "{ dummyKey : \'dummyValue\' }".', function () { + it('Should publish "example-event" passing data: "{ dummyKey : \'dummyValue\' }"', function () { spyOn(ps, 'publish'); ps.publish('example-event', { @@ -41,7 +61,7 @@ describe('Publish/Emmit an event.', function () { expect(ps.publish).toHaveBeenCalledWith('example-event', {dummyKey: 'dummyValue'}); }); - it('Publishes "example-event".', function () { + it('Should publish "example-event"', function () { ps.publish('example-event', { dummyKey: 'dummyValue' }); @@ -49,14 +69,50 @@ describe('Publish/Emmit an event.', function () { expect(ps.publish('example-event')).toBe(true); }); - it('Publishes an event that was not subscribed.', function () { + it('Should not publish an event that was not subscribed', function () { expect(ps.publish('unsubscribed-event')).toBe(false); }); + + it('Should publish asynchronously', function () { + var pubsub = new PubSub(); + var counter = 0; + var handler = function () { + counter += 1; + }; + + jasmine.clock().install(); + + ps.subscribe('async-event', handler); + + ps.publish('async-event'); + + expect(counter).toBe(0); + + jasmine.clock().tick(1); + + expect(counter).toBe(1); + + jasmine.clock().uninstall(); + }); + + it('Should publish synchronously', function () { + var pubsub = new PubSub(); + var counter = 0; + var handler = function () { + counter += 1; + }; + + ps.subscribe('async-event', handler); + + ps.publishSync('async-event'); + + expect(counter).toBe(1); + }); }); // Unsubscribe scenarios. -describe('Unsubscribe from event.', function () { - it('Unsubscribes from event using the event name ("example-event").', function () { +describe('Unsubscribe from event', function () { + it('Should unsubscribe from event using the event name ("example-event")', function () { var ps = new PubSub(); var unsub; @@ -69,7 +125,7 @@ describe('Unsubscribe from event.', function () { expect(ps.hasSubscribers('example-event')).toBe(false); }); - it('Unsubscribes from event using tokenized reference to the subscription.', function () { + it('Should unsubscribe from event using tokenized reference to the subscription', function () { var ps = new PubSub(), sub = ps.subscribe('example-event', listener), sub2 = ps.subscribe('example-event', listener), @@ -81,7 +137,7 @@ describe('Unsubscribe from event.', function () { expect(ps.subscribers()['example-event'].length).toBe(2); }); - it('Unsubscribes from an event that was not subscribed before.', function () { + it('Should unsubscribe from an event that was not subscribed before', function () { var ps = new PubSub(), unsub = ps.unsubscribe('fake-event'); @@ -90,7 +146,7 @@ describe('Unsubscribe from event.', function () { }); // Check if there are subscribers for a specific topic. -describe('Check if there are subscribers for a specific topic.', function () { +describe('Check if there are subscribers for a specific topic', function () { it('Should return true when checking if there are subscribers for "message" event.', function () { var ps = new PubSub(); var onMessage = ps.subscribe('message', function () {}); @@ -98,7 +154,7 @@ describe('Check if there are subscribers for a specific topic.', function () { expect(ps.hasSubscribers('message')).toBe(true); }); - it('Should return false when checking if there are subscribers for "message" event after unsubscribing.', function () { + it('Should return false when checking if there are subscribers for "message" event after unsubscribing', function () { var ps = new PubSub(); var onMessage = ps.subscribe('message', function () {}); @@ -107,13 +163,33 @@ describe('Check if there are subscribers for a specific topic.', function () { expect(ps.hasSubscribers('message')).toBe(false); }); - it('Should return false when checking if there are subscribers for "message" without subscribing before.', function () { + it('Should return false when checking if there are subscribers for "message" without subscribing before', function () { var ps = new PubSub(); expect(ps.hasSubscribers('message')).toBe(false); }); }); +// Check if there are any subscribers +describe('Check if there are any subscribers at all', function () { + it('Should return true when checking if there are any subscribers', function () { + var ps = new PubSub(); + var onMessage = ps.subscribe('message', function () {}); + + expect(ps.hasSubscribers()).toBe(true); + }); + + it('Should return false when checking if there are any subscribers after unsubscribing all subscribers', function () { + var ps = new PubSub(); + var onMessage = ps.subscribe('message', function () {}); + var onMessage2 = ps.subscribe('message2', function () {}); + + ps.unsubscribeAll(); + + expect(ps.hasSubscribers()).toBe(false); + }); +}); + // Clear all subscriptions at once. describe('Clears all subscriptions at once', function () { it('Should unsubscribe from all subscriptions', function () { @@ -164,7 +240,12 @@ describe('Public methods alias', function () { unsubscribe: 'off' }); + var t = ps.on('event-name', function () {}); + expect(PubSub.prototype.on).not.toBeUndefined(); + expect(PubSub.prototype.off).not.toBeUndefined(); + + expect(ps.off(t)).toBe(0); }); });