Updated add and remove tests for challenges (#7155)

This commit is contained in:
Keith Holliday
2016-05-08 11:35:38 -05:00
committed by Matteo Pagliazzi
parent 94220c5483
commit e747bba669
3 changed files with 38 additions and 20 deletions

View File

@@ -210,6 +210,17 @@ describe('Challenges Controller', function() {
}); });
describe('addTask', function() { describe('addTask', function() {
var challenge;
beforeEach(function () {
challenge = specHelper.newChallenge({
description: 'You are the owner and member',
leader: user._id,
members: [user],
_isMember: true
});
});
it('adds default task to array', function() { it('adds default task to array', function() {
var taskArray = []; var taskArray = [];
var listDef = { var listDef = {
@@ -217,26 +228,27 @@ describe('Challenges Controller', function() {
type: 'todo' type: 'todo'
} }
scope.addTask(taskArray, listDef); scope.addTask(taskArray, listDef, challenge);
expect(taskArray.length).to.eql(1); expect(challenge['todos'].length).to.eql(1);
expect(taskArray[0].text).to.eql('new todo text'); expect(challenge['todos'][0].text).to.eql('new todo text');
expect(taskArray[0].type).to.eql('todo'); expect(challenge['todos'][0].type).to.eql('todo');
}); });
it('adds the task to the front of the array', function() { it('adds the task to the front of the array', function() {
var previousTask = specHelper.newTodo({ text: 'previous task' }); var previousTask = specHelper.newTodo({ text: 'previous task' });
var taskArray = [previousTask]; var taskArray = [];
challenge['todos'] = [previousTask];
var listDef = { var listDef = {
newTask: 'new todo', newTask: 'new todo',
type: 'todo' type: 'todo'
} }
scope.addTask(taskArray, listDef); scope.addTask(taskArray, listDef, challenge);
expect(taskArray.length).to.eql(2); expect(challenge['todos'].length).to.eql(2);
expect(taskArray[0].text).to.eql('new todo'); expect(challenge['todos'][0].text).to.eql('new todo');
expect(taskArray[1].text).to.eql('previous task'); expect(challenge['todos'][1].text).to.eql('previous task');
}); });
it('removes text from new task input box', function() { it('removes text from new task input box', function() {
@@ -246,7 +258,7 @@ describe('Challenges Controller', function() {
type: 'todo' type: 'todo'
} }
scope.addTask(taskArray, listDef); scope.addTask(taskArray, listDef, challenge);
expect(listDef.newTask).to.not.exist; expect(listDef.newTask).to.not.exist;
}); });
@@ -261,31 +273,37 @@ describe('Challenges Controller', function() {
}); });
describe('removeTask', function() { describe('removeTask', function() {
var task, list; var task, challenge;
beforeEach(function() { beforeEach(function() {
sandbox.stub(window, 'confirm'); sandbox.stub(window, 'confirm');
task = specHelper.newTodo(); task = specHelper.newTodo();
list = [task]; challenge = specHelper.newChallenge({
description: 'You are the owner and member',
leader: user._id,
members: [user],
_isMember: true
});
challenge['todos'] = [task];
}); });
it('asks user to confirm deletion', function() { it('asks user to confirm deletion', function() {
scope.removeTask(task, list); scope.removeTask(task, challenge);
expect(window.confirm).to.be.calledOnce; expect(window.confirm).to.be.calledOnce;
}); });
it('does not remove task from list if not confirmed', function() { it('does not remove task from list if not confirmed', function() {
window.confirm.returns(false); window.confirm.returns(false);
scope.removeTask(task, list); scope.removeTask(task, challenge);
expect(list).to.include(task); expect(challenge['todos']).to.include(task);
}); });
it('removes task from list', function() { it('removes task from list', function() {
window.confirm.returns(true); window.confirm.returns(true);
scope.removeTask(task, list); scope.removeTask(task, challenge);
expect(list).to.not.include(task); expect(challenge['todos']).to.not.include(task);
}); });
}); });

View File

@@ -234,11 +234,11 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
var task = Shared.taskDefaults({text: listDef.newTask, type: listDef.type}); var task = Shared.taskDefaults({text: listDef.newTask, type: listDef.type});
Tasks.createChallengeTasks(challenge._id, task); Tasks.createChallengeTasks(challenge._id, task);
if (!challenge[task.type + 's']) challenge[task.type + 's'] = []; if (!challenge[task.type + 's']) challenge[task.type + 's'] = [];
challenge[task.type + 's'].push(task); challenge[task.type + 's'].unshift(task);
delete listDef.newTask; delete listDef.newTask;
}; };
$scope.removeTask = function(task, list) { $scope.removeTask = function(task, challenge) {
if (!confirm(window.env.t('sureDelete', {taskType: window.env.t(task.type), taskText: task.text}))) return; if (!confirm(window.env.t('sureDelete', {taskType: window.env.t(task.type), taskText: task.text}))) return;
Tasks.deleteTask(task._id); Tasks.deleteTask(task._id);
var index = challenge[task.type + 's'].indexOf(task); var index = challenge[task.type + 's'].indexOf(task);

View File

@@ -8,7 +8,7 @@ div(ng-if='task._editing')
p p
a(ng-click='unlink(task, "keep")')=env.t('keepIt') a(ng-click='unlink(task, "keep")')=env.t('keepIt')
|    |   
a(ng-click="removeTask(task, obj[list.type+'s'])")=env.t('removeIt') a(ng-click="removeTask(task, obj")=env.t('removeIt')
div(ng-if='task.challenge.broken=="CHALLENGE_DELETED"') div(ng-if='task.challenge.broken=="CHALLENGE_DELETED"')
p p
|  |