add tests for tags ops on tasks

This commit is contained in:
Matteo Pagliazzi
2015-12-14 22:44:50 +01:00
parent da154d3ea3
commit e547eb2dde
4 changed files with 127 additions and 4 deletions

View File

@@ -27,5 +27,5 @@
"positionRequired": "\"position\" is required and must be a number.",
"cantMoveCompletedTodo": "Can't move a completed todo.",
"directionUpDown": "\"direction\" is required and must be 'up' or 'down'",
"alreadyTagged": "The task is already tagged with give tag."
"alreadyTagged": "The task is already tagged with given tag."
}

View File

@@ -0,0 +1,53 @@
import {
generateUser,
requester,
translate as t,
} from '../../../../../helpers/api-integration.helper';
import { v4 as generateUUID } from 'uuid';
describe('DELETE /tasks/:taskId/tags/:tagId', () => {
let user, api;
before(() => {
return generateUser().then((generatedUser) => {
user = generatedUser;
api = requester(user);
});
});
it('removes a tag from a task', () => {
let tag;
let task;
return api.post('/tasks', {
type: 'habit',
text: 'Task with tag',
}).then(createdTask => {
task = createdTask;
return api.post('/tags', {name: 'Tag 1'});
}).then(createdTag => {
tag = createdTag;
return api.post(`/tasks/${task._id}/tags/${tag._id}`);
}).then(savedTask => {
return api.del(`/tasks/${task._id}/tags/${tag._id}`);
}).then(() => api.get(`/tasks/${task._id}`))
.then(updatedTask => {
expect(updatedTask.tags.length).to.equal(0);
});
});
it('only deletes existing tags', () => {
let task;
return expect(api.post('/tasks', {
type: 'habit',
text: 'Task with tag',
}).then(createdTask => {
return api.del(`/tasks/${createdTask._id}/tags/${generateUUID()}`);
})).to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('tagNotFound'),
});
});
});

View File

@@ -0,0 +1,70 @@
import {
generateUser,
requester,
translate as t,
} from '../../../../../helpers/api-integration.helper';
import { v4 as generateUUID } from 'uuid';
describe('POST /tasks/:taskId/tags/:tagId', () => {
let user, api;
before(() => {
return generateUser().then((generatedUser) => {
user = generatedUser;
api = requester(user);
});
});
it('adds a tag to a task', () => {
let tag;
let task;
return api.post('/tasks', {
type: 'habit',
text: 'Task with tag',
}).then(createdTask => {
task = createdTask;
return api.post('/tags', {name: 'Tag 1'});
}).then(createdTag => {
tag = createdTag;
return api.post(`/tasks/${task._id}/tags/${tag._id}`);
}).then(savedTask => {
expect(savedTask.tags[0]).to.equal(tag._id);
});
});
it('does not add a tag to a task twice', () => {
let tag;
let task;
return expect(api.post('/tasks', {
type: 'habit',
text: 'Task with tag',
}).then(createdTask => {
task = createdTask;
return api.post('/tags', {name: 'Tag 1'});
}).then(createdTag => {
tag = createdTag;
return api.post(`/tasks/${task._id}/tags/${tag._id}`);
}).then(() => {
return api.post(`/tasks/${task._id}/tags/${tag._id}`);
})).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('alreadyTagged'),
});
});
it('does not add a non existing tag to a task', () => {
return expect(api.post('/tasks', {
type: 'habit',
text: 'Task with tag',
}).then((task) => {
return api.post(`/tasks/${task._id}/tags/${generateUUID()}`);
})).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('invalidReqParams'),
});
});
});

View File

@@ -518,7 +518,7 @@ api.removeChecklistItem = {
*/
api.addTagToTask = {
method: 'POST',
url: '/tasks/:taskId/tags',
url: '/tasks/:taskId/tags/:tagId',
middlewares: [authWithHeaders()],
handler (req, res, next) {
let user = res.locals.user;
@@ -538,7 +538,7 @@ api.addTagToTask = {
if (!task) throw new NotFound(res.t('taskNotFound'));
let tagId = req.params.tagId;
let alreadyTagged = task.tags.indexOf(tagId) === -1;
let alreadyTagged = task.tags.indexOf(tagId) !== -1;
if (alreadyTagged) throw new BadRequest(res.t('alreadyTagged'));
task.tags.push(tagId);
@@ -580,7 +580,7 @@ api.removeTagFromTask = {
.then((task) => {
if (!task) throw new NotFound(res.t('taskNotFound'));
let tagI = _.findIndex(task.tags, {_id: req.params.tagId});
let tagI = task.tags.indexOf(req.params.tagId);
if (tagI === -1) throw new NotFound(res.t('tagNotFound'));
task.tags.splice(tagI, 1);