From c228618c9a587e398e93cfa2e62359b1b5866ae7 Mon Sep 17 00:00:00 2001 From: Aleksandar Jovanov Date: Fri, 30 Jun 2017 16:14:51 +0200 Subject: [PATCH] [#3639] Update tests for followers-counter module --- .../spec/modules/followers-counter.spec.js | 85 +++++++++++++------ 1 file changed, 57 insertions(+), 28 deletions(-) diff --git a/ckan/public/base/test/spec/modules/followers-counter.spec.js b/ckan/public/base/test/spec/modules/followers-counter.spec.js index ea923f9e686..d52edf6b2b6 100644 --- a/ckan/public/base/test/spec/modules/followers-counter.spec.js +++ b/ckan/public/base/test/spec/modules/followers-counter.spec.js @@ -7,6 +7,7 @@ describe('ckan.module.FollowersCounterModule()', function() { this.el = jQuery('
' + this.initialCounter + '
'); this.sandbox = ckan.sandbox(); this.module = new FollowersCounterModule(this.el, {}, this.sandbox); + this.module.options.num_followers = this.initialCounter; }); afterEach(function() { @@ -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'); @@ -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'}); }); }); @@ -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() { @@ -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); + }); }); });