diff --git a/test/spec/rootCtrlSpec.js b/test/spec/rootCtrlSpec.js index 5f4b61b1d2..8a68896a7b 100644 --- a/test/spec/rootCtrlSpec.js +++ b/test/spec/rootCtrlSpec.js @@ -1,38 +1,112 @@ 'use strict'; -// @TODO: Something here is calling a full page reload describe('Root Controller', function() { - var scope, user, ctrl; + var scope, rootscope, user, User, notification, ctrl, $httpBackend; beforeEach(function () { module(function($provide) { $provide.value('User', {}); }); - inject(function($rootScope, $controller) { + inject(function($rootScope, $controller, _$httpBackend_, Notification) { scope = $rootScope.$new(); - scope.loginUsername = 'user' - scope.loginPassword = 'pass' - user = specHelper.newUser(); + scope.loginUsername = 'user'; + scope.loginPassword = 'pass'; - ctrl = $controller('RootCtrl', {$scope: scope, User: {user: user}}); + rootscope = $rootScope; + + $httpBackend = _$httpBackend_; + + notification = Notification; + sinon.stub(notification, 'text') + + user = specHelper.newUser(); + User = {user: user}; + User.save = sinon.spy(); + User.sync = sinon.spy(); + + ctrl = $controller('RootCtrl', {$scope: scope, User: User}); }); }); - it('shows contributor level text', function(){ - expect(scope.contribText()).to.eql(undefined); - expect(scope.contribText(null, {npc: 'NPC'})).to.eql('NPC'); - expect(scope.contribText({level: 0, text: 'Blacksmith'})).to.eql(undefined); - expect(scope.contribText({level: 1, text: 'Blacksmith'})).to.eql('Friend Blacksmith'); - expect(scope.contribText({level: 2, text: 'Blacksmith'})).to.eql('Friend Blacksmith'); - expect(scope.contribText({level: 3, text: 'Blacksmith'})).to.eql('Elite Blacksmith'); - expect(scope.contribText({level: 4, text: 'Blacksmith'})).to.eql('Elite Blacksmith'); - expect(scope.contribText({level: 5, text: 'Blacksmith'})).to.eql('Champion Blacksmith'); - expect(scope.contribText({level: 6, text: 'Blacksmith'})).to.eql('Champion Blacksmith'); - expect(scope.contribText({level: 7, text: 'Blacksmith'})).to.eql('Legendary Blacksmith'); - expect(scope.contribText({level: 8, text: 'Blacksmith'})).to.eql('Guardian Blacksmith'); - expect(scope.contribText({level: 9, text: 'Blacksmith'})).to.eql('Heroic Blacksmith'); - expect(scope.contribText({level: 9, text: 'Blacksmith'}, {npc: 'NPC'})).to.eql('NPC'); + afterEach(function() { + notification.text.reset(); + User.save.reset(); + User.sync.reset(); }); + describe('contribText', function(){ + it('shows contributor level text', function(){ + expect(scope.contribText()).to.eql(undefined); + expect(scope.contribText(null, {npc: 'NPC'})).to.eql('NPC'); + expect(scope.contribText({level: 0, text: 'Blacksmith'})).to.eql(undefined); + expect(scope.contribText({level: 1, text: 'Blacksmith'})).to.eql('Friend Blacksmith'); + expect(scope.contribText({level: 2, text: 'Blacksmith'})).to.eql('Friend Blacksmith'); + expect(scope.contribText({level: 3, text: 'Blacksmith'})).to.eql('Elite Blacksmith'); + expect(scope.contribText({level: 4, text: 'Blacksmith'})).to.eql('Elite Blacksmith'); + expect(scope.contribText({level: 5, text: 'Blacksmith'})).to.eql('Champion Blacksmith'); + expect(scope.contribText({level: 6, text: 'Blacksmith'})).to.eql('Champion Blacksmith'); + expect(scope.contribText({level: 7, text: 'Blacksmith'})).to.eql('Legendary Blacksmith'); + expect(scope.contribText({level: 8, text: 'Blacksmith'})).to.eql('Guardian Blacksmith'); + expect(scope.contribText({level: 9, text: 'Blacksmith'})).to.eql('Heroic Blacksmith'); + expect(scope.contribText({level: 9, text: 'Blacksmith'}, {npc: 'NPC'})).to.eql('NPC'); + }); + }); + + describe('castEnd', function(){ + var task_target, type; + + beforeEach(function(){ + task_target = { id: 'task-id' }; + type = 'task'; + scope.spell = { + target: 'task', + key: 'fireball', + mana: 10, + text: env.t('spellWizardFireballText'), + cast: function(){} + }; + rootscope.applyingAction = true; + }); + + context('fails', function(){ + it('exits early if there is no applying action', function(){ + rootscope.applyingAction = null; + expect(scope.castEnd(task_target, type)).to.be.eql('No applying action'); + }); + + it('sends notification if target is invalid', function(){ + scope.spell.target = 'not_the_same_target'; + + scope.castEnd(task_target, type) + + notification.text.should.have.been.calledWith(window.env.t('invalidTarget')); + }); + }); + + context('succeeds', function(){ + it('sets scope.spell and rootScope.applyingAction to falsy values', function(){ + + scope.castEnd(task_target, type) + + expect(rootscope.applyingAction).to.eql(false); + expect(scope.spell).to.eql(null); + }); + + it('calls $scope.spell.cast', function(){ + // Kind of a hack, would prefer to use sinon.spy, + // but scope.spell gets turned to null in scope.castEnd + var spellWasCast = false; + scope.spell.cast = function(){ spellWasCast = true }; + + scope.castEnd(task_target, type) + + expect(spellWasCast).to.eql(true); + }); + + it('calls cast endpoint'); + + it('sends notification that spell was cast'); + }); + }); }); diff --git a/website/public/js/controllers/rootCtrl.js b/website/public/js/controllers/rootCtrl.js index 6ac31b9585..a8eb87641d 100644 --- a/website/public/js/controllers/rootCtrl.js +++ b/website/public/js/controllers/rootCtrl.js @@ -259,7 +259,7 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$ } $scope.castEnd = function(target, type, $event){ - if (!$rootScope.applyingAction) return; + if (!$rootScope.applyingAction) return 'No applying action'; $event && ($event.stopPropagation(),$event.preventDefault()); if ($scope.spell.target != type) return Notification.text(window.env.t('invalidTarget')); $scope.spell.cast(User.user, target); @@ -271,17 +271,16 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$ $rootScope.applyingAction = false; $http.post(ApiUrl.get() + '/api/v2/user/class/cast/'+spell.key+'?targetType='+type+'&targetId='+targetId) - .success(function(){ - var msg = window.env.t('youCast', {spell: spell.text()}); - switch (type) { - case 'task': msg = window.env.t('youCastTarget', {spell: spell.text(), target: target.text});break; - case 'user': msg = window.env.t('youCastTarget', {spell: spell.text(), target: target.profile.name});break; - case 'party': msg = window.env.t('youCastParty', {spell: spell.text()});break; - } - Notification.text($filter("markdown")(msg)); - User.sync(); - }); - + .success(function(){ + var msg = window.env.t('youCast', {spell: spell.text()}); + switch (type) { + case 'task': msg = window.env.t('youCastTarget', {spell: spell.text(), target: target.text});break; + case 'user': msg = window.env.t('youCastTarget', {spell: spell.text(), target: target.profile.name});break; + case 'party': msg = window.env.t('youCastParty', {spell: spell.text()});break; + } + Notification.text($filter("markdown")(msg)); + User.sync(); + }); } $rootScope.castCancel = function(){