Skip to content

Commit

Permalink
Fix countSubscriptions
Browse files Browse the repository at this point in the history
This was introduced in ad93491, but had
a bug that was not covered by unit tests.
  • Loading branch information
mroderick committed Feb 18, 2021
1 parent d5b9fbf commit a810919
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,15 @@
*/
PubSub.countSubscriptions = function countSubscriptions(topic){
var m;
// eslint-disable-next-line no-unused-vars
var token;
var count = 0;
for (m in messages){
if (Object.prototype.hasOwnProperty.call(messages, m) && m.indexOf(topic) === 0){
count++;
for (m in messages) {
if (Object.prototype.hasOwnProperty.call(messages, m) && m.indexOf(topic) === 0) {
for (token in messages[m]) {
count++;
}
break;
}
}
return count;
Expand Down
13 changes: 13 additions & 0 deletions test/test-countSubscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,17 @@ describe('test-countSubscriptions method', function () {

assert.equals(counts,1);
});

it('should count all subscriptions', function() {
var topic = TestHelper.getUniqueString(),
spy1 = sinon.spy(),
spy2 = sinon.spy();

PubSub.subscribe(topic, spy1);
PubSub.subscribe(topic, spy2);

var counts = PubSub.countSubscriptions(topic);

assert.equals(counts, 2);
});
});

0 comments on commit a810919

Please sign in to comment.