Refactored editTask into TaskService

This commit is contained in:
Blade Barringer
2015-06-16 23:11:04 -05:00
parent 18fe4b6166
commit 3ab27f32d2
10 changed files with 176 additions and 13 deletions

View File

@@ -49,6 +49,7 @@ module.exports = function(config) {
"website/public/js/services/memberServices.js",
"website/public/js/services/guideServices.js",
"website/public/js/services/challengeServices.js",
"website/public/js/services/taskServices.js",
"website/public/js/services/paymentServices.js",
"website/public/js/filters/money.js",

View File

@@ -174,4 +174,20 @@ describe('Challenges Controller', function() {
expect(scope.filterChallenges(notOwnNotMem)).to.eql(true);
});
});
describe('editTask', function() {
it('calls Tasks.editTask', function() {
inject(function(Tasks) {
sinon.stub(Tasks, 'editTask');
var task = {
id: 'task-id',
type: 'todo'
};
scope.editTask(task);
expect(Tasks.editTask).to.be.calledOnce;
expect(Tasks.editTask).to.be.calledWith(task);
});
});
});
});

View File

@@ -0,0 +1,39 @@
'use strict';
describe('Tasks Controller', function() {
var $rootScope, scope, user, ctrl;
beforeEach(function() {
user = specHelper.newUser();
module(function($provide) {
$provide.value('User', {user: user});
$provide.value('Guide', {});
});
inject(function($rootScope, $controller){
scope = $rootScope.$new();
$controller('RootCtrl', {$scope: scope, User: {user: user}});
ctrl = $controller('TasksCtrl', {$scope: scope, User: {user: user}});
});
});
describe('editTask', function() {
it('calls Tasks.editTask', function() {
inject(function(Tasks) {
sinon.stub(Tasks, 'editTask');
var task = {
id: 'task-id',
type: 'todo'
};
scope.editTask(task);
expect(Tasks.editTask).to.be.calledOnce;
expect(Tasks.editTask).to.be.calledWith(task);
});
});
});
});

View File

@@ -0,0 +1,82 @@
'use strict';
describe('Tasks Service', function() {
var rootScope, tasks, user;
beforeEach(function() {
module(function($provide) {
user = specHelper.newUser();
$provide.value('User', {user: user});
});
inject(function(_$rootScope_, Tasks, User) {
rootScope = _$rootScope_;
rootScope.charts = {};
tasks = Tasks;
});
});
describe('editTask', function() {
var task;
beforeEach(function(){
task = { id: 'task-id' }; // @TODO: replace with task factory
});
it('opens the edit menu if it is not already open', function() {
tasks.editTask(task);
expect(task._editing).to.eql(true);
});
it('closes the edit menu if it is not already open', function() {
task._editing = true;
tasks.editTask(task);
expect(task._editing).to.eql(false);
});
it('shows tags by default', function() {
tasks.editTask(task);
expect(task._tags).to.eql(true);
});
it('does not show tags if tagsCollapsed preference is enabled', function() {
user.preferences.tagsCollapsed = true;
tasks.editTask(task);
expect(task._tags).to.eql(false);
});
it('closes the edit menu if it is not already open', function() {
task._editing = true;
tasks.editTask(task);
expect(task._editing).to.eql(false);
});
it('does not open open advanced options automatically if user preference is set to collapsed', function(){
user.preferences.advancedCollapsed = true;
tasks.editTask(task);
expect(task._advanced).to.eql(false);
});
it('does open open advanced options automatically if user preference is set to not collapsed', function(){
user.preferences.advancedCollapsed = false;
tasks.editTask(task);
expect(task._advanced).to.eql(true);
});
it('closes task chart if it exists', function() {
rootScope.charts[task.id] = true;
tasks.editTask(task);
expect(rootScope.charts[task.id]).to.eql(false);
});
});
});

View File

@@ -1,7 +1,7 @@
"use strict";
habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state', '$stateParams',
function($rootScope, $scope, Shared, User, Challenges, Notification, $compile, Groups, $state, $stateParams) {
habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state', '$stateParams', 'Tasks',
function($rootScope, $scope, Shared, User, Challenges, Notification, $compile, Groups, $state, $stateParams, Tasks) {
// Use presence of cid to determine whether to show a list or a single
// challenge
@@ -49,6 +49,10 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
});
};
$scope.editTask = function(task) {
Tasks.editTask(task);
};
/**
* Create
*/

View File

@@ -1,7 +1,7 @@
"use strict";
habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','Notification', '$http', 'ApiUrl', '$timeout', 'Shared', 'Guide',
function($scope, $rootScope, $location, User, Notification, $http, ApiUrl, $timeout, Shared, Guide) {
habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','Notification', '$http', 'ApiUrl', '$timeout', 'Shared', 'Guide', 'Tasks',
function($scope, $rootScope, $location, User, Notification, $http, ApiUrl, $timeout, Shared, Guide, Tasks) {
$scope.obj = User.user; // used for task-lists
$scope.user = User.user;
@@ -65,6 +65,10 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
list.focus = true;
};
$scope.editTask = function(task) {
Tasks.editTask(task);
};
/**
* Add the new task to the actions log
*/

View File

@@ -18,14 +18,6 @@ function habitrpgTasks($rootScope, User) {
// main: '@', // true if it's the user's main list
// obj: '='
//},
controller: ['$scope', '$rootScope', function($scope, $rootScope){
$scope.editTask = function(task){
task._editing = !task._editing;
task._tags = User.user.preferences.tagsCollapsed;
task._advanced = !User.user.preferences.advancedCollapsed;
if($rootScope.charts[task.id]) $rootScope.charts[task.id] = false;
};
}],
link: function(scope, element, attrs) {
// $scope.obj needs to come from controllers, so we can pass by ref
scope.main = attrs.main;

View File

@@ -0,0 +1,24 @@
'use strict';
angular
.module('habitrpg')
.factory('Tasks', tasksFactory);
tasksFactory.$inject = [
'$rootScope',
'User'
];
function tasksFactory($rootScope, User) {
function editTask(task) {
task._editing = !task._editing;
task._tags = !User.user.preferences.tagsCollapsed;
task._advanced = !User.user.preferences.advancedCollapsed;
if($rootScope.charts[task.id]) $rootScope.charts[task.id] = false;
}
return {
editTask: editTask
};
}

View File

@@ -46,6 +46,7 @@
"js/services/groupServices.js",
"js/services/memberServices.js",
"js/services/guideServices.js",
"js/services/taskServices.js",
"js/services/challengeServices.js",
"js/services/paymentServices.js",

View File

@@ -1,5 +1,5 @@
fieldset.option-group(ng-if='!$state.includes("options.social.challenges")')
p.option-title.mega(ng-click='task._tags = !task._tags', tooltip=env.t('expandCollapse'))=env.t('tags')
label.checkbox(ng-repeat='tag in user.tags', ng-class="{visuallyhidden: task._tags}")
label.checkbox(ng-repeat='tag in user.tags', ng-if='task._tags')
input(type='checkbox', ng-model='task.tags[tag.id]')
markdown(text='tag.name')