Add tests for castEnd on root controller

This commit is contained in:
Blade Barringer
2015-06-01 17:38:18 -05:00
parent a8ca01ba62
commit 39f363ec29
2 changed files with 106 additions and 33 deletions

View File

@@ -1,38 +1,112 @@
'use strict'; 'use strict';
// @TODO: Something here is calling a full page reload
describe('Root Controller', function() { describe('Root Controller', function() {
var scope, user, ctrl; var scope, rootscope, user, User, notification, ctrl, $httpBackend;
beforeEach(function () { beforeEach(function () {
module(function($provide) { module(function($provide) {
$provide.value('User', {}); $provide.value('User', {});
}); });
inject(function($rootScope, $controller) { inject(function($rootScope, $controller, _$httpBackend_, Notification) {
scope = $rootScope.$new(); scope = $rootScope.$new();
scope.loginUsername = 'user' scope.loginUsername = 'user';
scope.loginPassword = 'pass' scope.loginPassword = 'pass';
user = specHelper.newUser();
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(){ afterEach(function() {
expect(scope.contribText()).to.eql(undefined); notification.text.reset();
expect(scope.contribText(null, {npc: 'NPC'})).to.eql('NPC'); User.save.reset();
expect(scope.contribText({level: 0, text: 'Blacksmith'})).to.eql(undefined); User.sync.reset();
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('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');
});
});
}); });

View File

@@ -259,7 +259,7 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
} }
$scope.castEnd = function(target, type, $event){ $scope.castEnd = function(target, type, $event){
if (!$rootScope.applyingAction) return; if (!$rootScope.applyingAction) return 'No applying action';
$event && ($event.stopPropagation(),$event.preventDefault()); $event && ($event.stopPropagation(),$event.preventDefault());
if ($scope.spell.target != type) return Notification.text(window.env.t('invalidTarget')); if ($scope.spell.target != type) return Notification.text(window.env.t('invalidTarget'));
$scope.spell.cast(User.user, target); $scope.spell.cast(User.user, target);
@@ -271,17 +271,16 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
$rootScope.applyingAction = false; $rootScope.applyingAction = false;
$http.post(ApiUrl.get() + '/api/v2/user/class/cast/'+spell.key+'?targetType='+type+'&targetId='+targetId) $http.post(ApiUrl.get() + '/api/v2/user/class/cast/'+spell.key+'?targetType='+type+'&targetId='+targetId)
.success(function(){ .success(function(){
var msg = window.env.t('youCast', {spell: spell.text()}); var msg = window.env.t('youCast', {spell: spell.text()});
switch (type) { switch (type) {
case 'task': msg = window.env.t('youCastTarget', {spell: spell.text(), target: target.text});break; 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 '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; case 'party': msg = window.env.t('youCastParty', {spell: spell.text()});break;
} }
Notification.text($filter("markdown")(msg)); Notification.text($filter("markdown")(msg));
User.sync(); User.sync();
}); });
} }
$rootScope.castCancel = function(){ $rootScope.castCancel = function(){