From 35316ebeb6432f3038f59b3a784de4fe28f42849 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Wed, 16 Dec 2015 12:57:19 +0100 Subject: [PATCH] move completed todos outside of tasksOrder (and back) with tests --- .../POST-tasks_id_score_direction.test.js | 33 +++++++++++++++++++ .../v3/integration/tasks/PUT-tasks_id.test.js | 8 ++--- website/src/controllers/api-v3/tasks.js | 17 ++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/test/api/v3/integration/tasks/POST-tasks_id_score_direction.test.js b/test/api/v3/integration/tasks/POST-tasks_id_score_direction.test.js index 98d34bcbd5..622d63dc4f 100644 --- a/test/api/v3/integration/tasks/POST-tasks_id_score_direction.test.js +++ b/test/api/v3/integration/tasks/POST-tasks_id_score_direction.test.js @@ -53,6 +53,39 @@ describe('POST /tasks/:id/score/:direction', () => { .then((task) => expect(task.completed).to.equal(true)); }); + it('moves completed todos out of user.tasksOrder.todos', () => { + return api.get('/user') + .then(user => { + expect(user.tasksOrder.todos.indexOf(todo._id)).to.not.equal(-1) + }).then(() => api.post(`/tasks/${todo._id}/score/up`)) + .then(() => api.get(`/tasks/${todo._id}`)) + .then((updatedTask) => { + expect(updatedTask.completed).to.equal(true); + return api.get('/user'); + }) + .then((user) => { + expect(user.tasksOrder.todos.indexOf(todo._id)).to.equal(-1) + }); + }); + + it('moves un-completed todos back into user.tasksOrder.todos', () => { + return api.get('/user') + .then(user => { + expect(user.tasksOrder.todos.indexOf(todo._id)).to.not.equal(-1) + }).then(() => api.post(`/tasks/${todo._id}/score/up`)) + .then(() => api.post(`/tasks/${todo._id}/score/down`)) + .then(() => api.get(`/tasks/${todo._id}`)) + .then((updatedTask) => { + expect(updatedTask.completed).to.equal(false); + return api.get('/user'); + }) + .then((user) => { + let l = user.tasksOrder.todos.length; + expect(user.tasksOrder.todos.indexOf(todo._id)).not.to.equal(-1); + expect(user.tasksOrder.todos.indexOf(todo._id)).to.equal(l - 1); // Check that it was pushed at the bottom + }); + }); + it('uncompletes todo when direction is down', () => { return api.post(`/tasks/${todo._id}/score/down`) .then((res) => api.get(`/tasks/${todo._id}`)) diff --git a/test/api/v3/integration/tasks/PUT-tasks_id.test.js b/test/api/v3/integration/tasks/PUT-tasks_id.test.js index 7ea2440beb..f78f7ca35e 100644 --- a/test/api/v3/integration/tasks/PUT-tasks_id.test.js +++ b/test/api/v3/integration/tasks/PUT-tasks_id.test.js @@ -15,7 +15,7 @@ describe('PUT /tasks/:id', () => { }); }); - xcontext('validates params', () => { + context('validates params', () => { let task; beforeEach(() => { @@ -64,7 +64,7 @@ describe('PUT /tasks/:id', () => { }); }); - xcontext('habits', () => { + context('habits', () => { let habit; beforeEach(() => { @@ -92,7 +92,7 @@ describe('PUT /tasks/:id', () => { }); }); - xcontext('todos', () => { + context('todos', () => { let todo; beforeEach(() => { @@ -255,7 +255,7 @@ describe('PUT /tasks/:id', () => { }); }); - xcontext('rewards', () => { + context('rewards', () => { let reward; beforeEach(() => { diff --git a/website/src/controllers/api-v3/tasks.js b/website/src/controllers/api-v3/tasks.js index 9221787a63..ef7180088e 100644 --- a/website/src/controllers/api-v3/tasks.js +++ b/website/src/controllers/api-v3/tasks.js @@ -255,6 +255,7 @@ api.scoreTask = { .then((task) => { if (!task) throw new NotFound(res.t('taskNotFound')); + let wasCompleted = task.completed; if (task.type === 'daily' || task.type === 'todo') { task.completed = direction === 'up'; // TODO move into scoreTask } @@ -263,6 +264,22 @@ api.scoreTask = { // Drop system (don't run on the client, as it would only be discarded since ops are sent to the API, not the results) if (direction === 'up') user.fns.randomDrop({task, delta}, req); + // If a todo was completed or uncompleted move it in or out of the user.tasksOrder.todos list + if (task.type === 'todo') { + if (!wasCompleted && task.completed) { + let i = user.tasksOrder.todos.indexOf(task._id); + if (i !== -1) user.tasksOrder.todos.splice(i, 1); + } else if (wasCompleted && !task.completed) { + let i = user.tasksOrder.todos.indexOf(task._id); + if (i === -1) { + user.tasksOrder.todos.push(task._id); // TODO push at the top? + } else { // If for some reason it hadn't been removed TODO ok? + user.tasksOrder.todos.splice(i, 1); + user.tasksOrder.push(task._id); + } + } + } + return Q.all([ user.save(), task.save(),