From 93c5cc0385624d9f07d6c94b3d0b8639438ada29 Mon Sep 17 00:00:00 2001 From: Kaitlin Hipkin Date: Tue, 15 Mar 2016 16:03:36 -0400 Subject: [PATCH 1/6] add tests for awarding party {up,on} achievs --- test/spec/controllers/partyCtrlSpec.js | 94 +++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/test/spec/controllers/partyCtrlSpec.js b/test/spec/controllers/partyCtrlSpec.js index 092072988d..e39748a18f 100644 --- a/test/spec/controllers/partyCtrlSpec.js +++ b/test/spec/controllers/partyCtrlSpec.js @@ -8,7 +8,8 @@ describe("Party Controller", function() { user._id = "unique-user-id"; User = { user: user, - sync: sandbox.spy + sync: sandbox.spy, + set: sandbox.spy } module(function($provide) { @@ -33,6 +34,97 @@ describe("Party Controller", function() { }); }); + describe('initialization', function() { + context('party has 1 member', function() { + beforeEach(function() { + sinon.stub(groups.party.memberCount).returns(1); + }); + + it('awards no new achievements', function() { + expect(User.set).to.not.be.called; + expect(rootScope.openModal).to.not.be.called; + }); + }); + + context('party has 2 members', function() { + beforeEach(function() { + sinon.stub(groups.party.memberCount).returns(2); + }); + + context('user does not have "Party Up" achievement', function() { + it('awards "Party Up" achievement', function() { + expect(User.set).to.be.calledOnce; + expect(User.set).to.be.calledWith( + { 'achievements.partyUp': true} + ); + expect(rootScope.openModal).to.be.calledOnce; + expect(rootScope.openModal).to.be.calledWith( + 'achievements/partyUp', + { controller: 'UserCtrl' } + ); + }); + }); + }); + + context('party has 4 members', function() { + beforeEach(function() { + sinon.stub(groups.party.memberCount).returns(4); + }); + + context('user has "Party Up" but not "Party On" achievement', function() { + beforeEach(function() { + user.achievements.partyUp = true; + }); + + it('awards "Party On" achievement', function() { + expect(User.set).to.be.calledOnce; + expect(User.set).to.be.calledWith( + { 'achievements.partyOn': true} + ); + expect(rootScope.openModal).to.be.calledOnce; + expect(rootScope.openModal).to.be.calledWith( + 'achievements/partyOn', + { controller: 'UserCtrl' } + ); + }); + }); + + context('user has neither "Party Up" nor "Party On" achievements', function() { + it('awards "Party Up" and "Party On" achievements', function() { + expect(User.set).to.be.calledTwice; + expect(User.set).to.be.calledWith( + { 'achievements.partyUp': true} + ); + expect(User.set).to.be.calledWith( + { 'achievements.partyOn': true} + ); + expect(rootScope.openModal).to.be.calledTwice; + expect(rootScope.openModal).to.be.calledWith( + 'achievements/partyUp', + { controller: 'UserCtrl' } + ); + expect(rootScope.openModal).to.be.calledWith( + 'achievements/partyOn', + { controller: 'UserCtrl' } + ); + }); + }); + + context('user has both "Party Up" and "Party On" achievements', function() { + beforeEach(function() { + user.achievements.partyUp = true; + user.achievements.partyOn = true; + }); + + it('awards no new achievements', function() { + expect(User.set).to.not.be.called; + expect(rootScope.openModal).to.not.be.called; + }); + }); + + }); + }); + describe('questAccept', function() { beforeEach(function() { scope.group = { From c0e636dba048f3460baf977946e3c7e84ac979aa Mon Sep 17 00:00:00 2001 From: Kaitlin Hipkin Date: Tue, 15 Mar 2016 18:36:36 -0400 Subject: [PATCH 2/6] fix spy? --- test/spec/controllers/partyCtrlSpec.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/spec/controllers/partyCtrlSpec.js b/test/spec/controllers/partyCtrlSpec.js index e39748a18f..eaca6d20a1 100644 --- a/test/spec/controllers/partyCtrlSpec.js +++ b/test/spec/controllers/partyCtrlSpec.js @@ -8,10 +8,11 @@ describe("Party Controller", function() { user._id = "unique-user-id"; User = { user: user, - sync: sandbox.spy, - set: sandbox.spy + sync: sandbox.spy } + sandbox.spy(User, "set"); + module(function($provide) { $provide.value('User', User); }); From 68c3c2973ba267e1123dd4274fb6be121278d511 Mon Sep 17 00:00:00 2001 From: Kaitlin Hipkin Date: Tue, 15 Mar 2016 19:48:23 -0400 Subject: [PATCH 3/6] cannot spy on an undefined function --- test/spec/controllers/partyCtrlSpec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/spec/controllers/partyCtrlSpec.js b/test/spec/controllers/partyCtrlSpec.js index eaca6d20a1..6a0278557e 100644 --- a/test/spec/controllers/partyCtrlSpec.js +++ b/test/spec/controllers/partyCtrlSpec.js @@ -8,7 +8,8 @@ describe("Party Controller", function() { user._id = "unique-user-id"; User = { user: user, - sync: sandbox.spy + sync: sandbox.spy, + set: function() {} } sandbox.spy(User, "set"); From ee09bee1e2ca914e39a00ccab0190742af406c38 Mon Sep 17 00:00:00 2001 From: Kaitlin Hipkin Date: Wed, 16 Mar 2016 00:55:20 -0400 Subject: [PATCH 4/6] use sandbox.stub instead of sandbox.spy --- test/spec/controllers/partyCtrlSpec.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/spec/controllers/partyCtrlSpec.js b/test/spec/controllers/partyCtrlSpec.js index 6a0278557e..53692c040b 100644 --- a/test/spec/controllers/partyCtrlSpec.js +++ b/test/spec/controllers/partyCtrlSpec.js @@ -9,10 +9,10 @@ describe("Party Controller", function() { User = { user: user, sync: sandbox.spy, - set: function() {} + set: function() {} // no-op } - sandbox.spy(User, "set"); + sandbox.stub(User, 'set'); module(function($provide) { $provide.value('User', User); @@ -37,9 +37,13 @@ describe("Party Controller", function() { }); describe('initialization', function() { + beforeEach(function() { + sandbox.stub(rootScope, 'openModal'); + }); + context('party has 1 member', function() { beforeEach(function() { - sinon.stub(groups.party.memberCount).returns(1); + scope.group = { memberCount: 1 }; }); it('awards no new achievements', function() { @@ -50,7 +54,7 @@ describe("Party Controller", function() { context('party has 2 members', function() { beforeEach(function() { - sinon.stub(groups.party.memberCount).returns(2); + scope.group = { memberCount: 2 }; }); context('user does not have "Party Up" achievement', function() { @@ -70,7 +74,7 @@ describe("Party Controller", function() { context('party has 4 members', function() { beforeEach(function() { - sinon.stub(groups.party.memberCount).returns(4); + scope.group = { memberCount: 4 }; }); context('user has "Party Up" but not "Party On" achievement', function() { From 469ece70c388db39fd766be13ba7bd57e8c5dacf Mon Sep 17 00:00:00 2001 From: Kaitlin Hipkin Date: Wed, 16 Mar 2016 22:32:07 -0400 Subject: [PATCH 5/6] clean up party {up,on} tests, add calls to $controller after setup --- test/spec/controllers/partyCtrlSpec.js | 74 ++++++++++++-------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/test/spec/controllers/partyCtrlSpec.js b/test/spec/controllers/partyCtrlSpec.js index 53692c040b..2c7cdf0c52 100644 --- a/test/spec/controllers/partyCtrlSpec.js +++ b/test/spec/controllers/partyCtrlSpec.js @@ -8,12 +8,10 @@ describe("Party Controller", function() { user._id = "unique-user-id"; User = { user: user, - sync: sandbox.spy, - set: function() {} // no-op + sync: sandbox.spy(), + set: sandbox.spy() } - sandbox.stub(User, 'set'); - module(function($provide) { $provide.value('User', User); }); @@ -42,61 +40,66 @@ describe("Party Controller", function() { }); context('party has 1 member', function() { - beforeEach(function() { - scope.group = { memberCount: 1 }; - }); - it('awards no new achievements', function() { + sandbox.stub(groups, 'party').returns({ + $syncParty: function() {}, + memberCount: 1 + }); + + $controller('PartyCtrl', { $scope: scope, User: User }); + expect(User.set).to.not.be.called; expect(rootScope.openModal).to.not.be.called; }); }); context('party has 2 members', function() { - beforeEach(function() { - scope.group = { memberCount: 2 }; - }); - context('user does not have "Party Up" achievement', function() { it('awards "Party Up" achievement', function() { + sandbox.stub(groups, 'party').returns({ + $syncParty: function() {}, + memberCount: 2 + }); + + $controller('PartyCtrl', { $scope: scope, User: User }); + expect(User.set).to.be.calledOnce; expect(User.set).to.be.calledWith( - { 'achievements.partyUp': true} + { 'achievements.partyUp': true } ); expect(rootScope.openModal).to.be.calledOnce; - expect(rootScope.openModal).to.be.calledWith( - 'achievements/partyUp', - { controller: 'UserCtrl' } - ); + expect(rootScope.openModal).to.be.calledWith('achievements/partyUp'); }); }); }); context('party has 4 members', function() { beforeEach(function() { - scope.group = { memberCount: 4 }; + sandbox.stub(groups, 'party').returns({ + $syncParty: function() {}, + memberCount: 4 + }); }); context('user has "Party Up" but not "Party On" achievement', function() { - beforeEach(function() { - user.achievements.partyUp = true; - }); - it('awards "Party On" achievement', function() { + user.achievements.partyUp = true; + + $controller('PartyCtrl', { $scope: scope, User: User }); + expect(User.set).to.be.calledOnce; expect(User.set).to.be.calledWith( - { 'achievements.partyOn': true} + { 'achievements.partyOn': true } ); expect(rootScope.openModal).to.be.calledOnce; - expect(rootScope.openModal).to.be.calledWith( - 'achievements/partyOn', - { controller: 'UserCtrl' } - ); + expect(rootScope.openModal).to.be.calledWith('achievements/partyOn'); }); }); context('user has neither "Party Up" nor "Party On" achievements', function() { it('awards "Party Up" and "Party On" achievements', function() { + $controller('PartyCtrl', { $scope: scope, User: User }); + expect(User.set).to.be.calledTwice; expect(User.set).to.be.calledWith( { 'achievements.partyUp': true} @@ -105,29 +108,22 @@ describe("Party Controller", function() { { 'achievements.partyOn': true} ); expect(rootScope.openModal).to.be.calledTwice; - expect(rootScope.openModal).to.be.calledWith( - 'achievements/partyUp', - { controller: 'UserCtrl' } - ); - expect(rootScope.openModal).to.be.calledWith( - 'achievements/partyOn', - { controller: 'UserCtrl' } - ); + expect(rootScope.openModal).to.be.calledWith('achievements/partyUp'); + expect(rootScope.openModal).to.be.calledWith('achievements/partyOn'); }); }); context('user has both "Party Up" and "Party On" achievements', function() { - beforeEach(function() { + it('awards no new achievements', function() { user.achievements.partyUp = true; user.achievements.partyOn = true; - }); - it('awards no new achievements', function() { + $controller('PartyCtrl', { $scope: scope, User: User }); + expect(User.set).to.not.be.called; expect(rootScope.openModal).to.not.be.called; }); }); - }); }); From 0ca949b1bb7bf6cd01c5dfefe6d05fabe002b91d Mon Sep 17 00:00:00 2001 From: Kaitlin Hipkin Date: Wed, 16 Mar 2016 23:50:16 -0400 Subject: [PATCH 6/6] pull initialization of controller into helper method and stub state --- test/spec/controllers/partyCtrlSpec.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/test/spec/controllers/partyCtrlSpec.js b/test/spec/controllers/partyCtrlSpec.js index 2c7cdf0c52..92401f1987 100644 --- a/test/spec/controllers/partyCtrlSpec.js +++ b/test/spec/controllers/partyCtrlSpec.js @@ -35,6 +35,15 @@ describe("Party Controller", function() { }); describe('initialization', function() { + function initializeControllerWithStubbedState() { + inject(function(_$state_) { + var state = _$state_; + sandbox.stub(state, 'is').returns(true); + $controller('PartyCtrl', { $scope: scope, $state: state }); + expect(state.is).to.be.calledOnce; // ensure initialization worked as desired + }); + }; + beforeEach(function() { sandbox.stub(rootScope, 'openModal'); }); @@ -46,7 +55,7 @@ describe("Party Controller", function() { memberCount: 1 }); - $controller('PartyCtrl', { $scope: scope, User: User }); + initializeControllerWithStubbedState(); expect(User.set).to.not.be.called; expect(rootScope.openModal).to.not.be.called; @@ -61,7 +70,7 @@ describe("Party Controller", function() { memberCount: 2 }); - $controller('PartyCtrl', { $scope: scope, User: User }); + initializeControllerWithStubbedState(); expect(User.set).to.be.calledOnce; expect(User.set).to.be.calledWith( @@ -85,7 +94,7 @@ describe("Party Controller", function() { it('awards "Party On" achievement', function() { user.achievements.partyUp = true; - $controller('PartyCtrl', { $scope: scope, User: User }); + initializeControllerWithStubbedState(); expect(User.set).to.be.calledOnce; expect(User.set).to.be.calledWith( @@ -98,7 +107,7 @@ describe("Party Controller", function() { context('user has neither "Party Up" nor "Party On" achievements', function() { it('awards "Party Up" and "Party On" achievements', function() { - $controller('PartyCtrl', { $scope: scope, User: User }); + initializeControllerWithStubbedState(); expect(User.set).to.be.calledTwice; expect(User.set).to.be.calledWith( @@ -118,7 +127,7 @@ describe("Party Controller", function() { user.achievements.partyUp = true; user.achievements.partyOn = true; - $controller('PartyCtrl', { $scope: scope, User: User }); + initializeControllerWithStubbedState(); expect(User.set).to.not.be.called; expect(rootScope.openModal).to.not.be.called;