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,24 +1,41 @@
'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'
scope.loginUsername = 'user';
scope.loginPassword = 'pass';
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: user}});
ctrl = $controller('RootCtrl', {$scope: scope, User: User});
});
});
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');
@@ -34,5 +51,62 @@ describe('Root Controller', function() {
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){
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);
@@ -281,7 +281,6 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
Notification.text($filter("markdown")(msg));
User.sync();
});
}
$rootScope.castCancel = function(){