Skip to content

Commit

Permalink
Improve tests and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
georapbox committed Jul 5, 2017
1 parent 6db33cb commit 9f08223
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion dist/pubsub.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* PubSub.js
* Javascript implementation of the Publish/Subscribe pattern.
*
* @version 3.2.3
* @version 3.2.4
* @author George Raptis <[email protected]> (georapbox.github.io)
* @homepage https://github.com/georapbox/PubSub#readme
* @repository git+https://github.com/georapbox/PubSub.git
Expand Down
109 changes: 95 additions & 14 deletions tests/pubsub.specs.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
/* 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);
});
});

// 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();

function listener() {}

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', {
Expand All @@ -41,22 +61,58 @@ 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'
});

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;

Expand All @@ -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),
Expand All @@ -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');

Expand All @@ -90,15 +146,15 @@ 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 () {});

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 () {});

Expand All @@ -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 () {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 9f08223

Please sign in to comment.