Skip to content

Commit

Permalink
[#3639] Update tests for followers-counter module
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandar Jovanov committed Jun 30, 2017
1 parent 61d029b commit c228618
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions ckan/public/base/test/spec/modules/followers-counter.spec.js
Expand Up @@ -7,6 +7,7 @@ describe('ckan.module.FollowersCounterModule()', function() {
this.el = jQuery('<dd><span>' + this.initialCounter + '</span></dd>');
this.sandbox = ckan.sandbox();
this.module = new FollowersCounterModule(this.el, {}, this.sandbox);
this.module.options.num_followers = this.initialCounter;
});

afterEach(function() {
Expand All @@ -25,19 +26,6 @@ describe('ckan.module.FollowersCounterModule()', function() {
target.restore();
});

it('should set this.counterVal to the current counter value in the DOM converted to number', function() {
this.module.initialize();

assert.equal(this.module.counterVal, this.initialCounter);
});

it('should set this.objId to the one on this.options.id', function() {
this.module.options = {id: 'some-id'};
this.module.initialize();

assert.equal(this.module.objId, this.module.options.id);
});

it('should subscribe to the "follow-follow-some-id" event', function() {
var target = sinon.stub(this.sandbox, 'subscribe');

Expand Down Expand Up @@ -103,23 +91,16 @@ describe('ckan.module.FollowersCounterModule()', function() {
assert.called(target);
});

it('should increment this.counterVal on calling _onFollow', function() {
this.module.initialize();
this.module._onFollow();

assert.equal(this.module.counterVal, ++this.initialCounter);
});

it('should increment the counter value in the DOM on calling _onFollow', function() {
var counterVal;
it('should call _updateCounter when ._onFollow is called', function() {
var target = sinon.stub(this.module, '_updateCounter');

this.module.options = {id: 'some-id'};
this.module.initialize();
this.module._onFollow();

counterVal = this.module.counterEl.text();
counterVal = parseInt(counterVal, 10);
this.module._onFollow();

assert.equal(counterVal, ++this.initialCounter);
assert.called(target);
assert.calledWith(target, {action: 'follow'});
});
});

Expand All @@ -135,11 +116,44 @@ describe('ckan.module.FollowersCounterModule()', function() {
assert.called(target);
});

it('should decrement this.counterVal on calling _onUnfollow', function() {
it('should call _updateCounter when ._onUnfollow is called', function() {
var target = sinon.stub(this.module, '_updateCounter');

this.module.options = {id: 'some-id'};
this.module.initialize();

this.module._onUnfollow();

assert.equal(this.module.counterVal, --this.initialCounter);
assert.called(target);
assert.calledWith(target, {action: 'unfollow'});
});
});

describe('._updateCounter', function() {
it('should increment this.options.num_followers on calling _onFollow', function() {
this.module.initialize();
this.module._onFollow();

assert.equal(this.module.options.num_followers, ++this.initialCounter);
});

it('should increment the counter value in the DOM on calling _onFollow', function() {
var counterVal;

this.module.initialize();
this.module._onFollow();

counterVal = this.module.counterEl.text();
counterVal = parseInt(counterVal, 10);

assert.equal(counterVal, ++this.initialCounter);
});

it('should decrement this.options.num_followers on calling _onUnfollow', function() {
this.module.initialize();
this.module._onUnfollow();

assert.equal(this.module.options.num_followers, --this.initialCounter);
});

it('should decrement the counter value in the DOM on calling _onUnfollow', function() {
Expand All @@ -153,5 +167,20 @@ describe('ckan.module.FollowersCounterModule()', function() {

assert.equal(counterVal, --this.initialCounter);
});

it('should not change the counter value in the DOM when the value is greater than 1000', function() {
var beforeCounterVal = 1536;
var afterCounterVal;

this.module.options = {num_followers: beforeCounterVal};
this.module.initialize();
this.module.counterEl.text(this.module.options.num_followers);
this.module._onFollow();

afterCounterVal = this.module.counterEl.text();
afterCounterVal = parseInt(afterCounterVal, 10);

assert.equal(beforeCounterVal, afterCounterVal);
});
});
});

0 comments on commit c228618

Please sign in to comment.