Refactor Tasks tests to use await syntax

This commit is contained in:
Georgi Gardev
2016-01-12 19:11:59 +02:00
parent 2e3bee08d8
commit ac6a0276ab
12 changed files with 507 additions and 630 deletions

View File

@@ -6,61 +6,53 @@ import {
describe('DELETE /tasks/:id', () => { describe('DELETE /tasks/:id', () => {
let user; let user;
before(() => { before(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser;
});
}); });
context('task can be deleted', () => { context('task can be deleted', () => {
let task; let task;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { task = await user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
}).then((createdTask) => {
task = createdTask;
}); });
}); });
it('deletes a user\'s task', () => { it('deletes a user\'s task', async () => {
return user.del(`/tasks/${task._id}`) await user.del(`/tasks/${task._id}`);
.then(() => {
return expect(user.get(`/tasks/${task._id}`)).to.eventually.be.rejected.and.eql({ await expect(user.get(`/tasks/${task._id}`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('taskNotFound'), message: t('taskNotFound'),
}); });
}); });
}); });
});
context('task cannot be deleted', () => { context('task cannot be deleted', () => {
it('cannot delete a non-existant task', () => { it('cannot delete a non-existant task', async () => {
return expect(user.del('/tasks/550e8400-e29b-41d4-a716-446655440000')).to.eventually.be.rejected.and.eql({ await expect(user.del('/tasks/550e8400-e29b-41d4-a716-446655440000')).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('taskNotFound'), message: t('taskNotFound'),
}); });
}); });
it('cannot delete a task owned by someone else', () => { it('cannot delete a task owned by someone else', async () => {
return generateUser() let anotherUser = await generateUser();
.then((anotherUser) => { let task2 = await anotherUser.post('/tasks', {
return anotherUser.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
}); });
})
.then((task2) => { await expect(user.del(`/tasks/${task2._id}`)).to.eventually.be.rejected.and.eql({
return expect(user.del(`/tasks/${task2._id}`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('taskNotFound'), message: t('taskNotFound'),
}); });
}); });
});
it('cannot delete active challenge tasks'); // TODO after challenges are implemented it('cannot delete active challenge tasks'); // TODO after challenges are implemented
it('remove a task from user.tasksOrder'); // TODO it('remove a task from user.tasksOrder'); // TODO

View File

@@ -6,37 +6,27 @@ import Q from 'q';
describe('GET /tasks', () => { describe('GET /tasks', () => {
let user; let user;
before(() => { beforeEach(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser;
});
}); });
it('returns all user\'s tasks', () => { it('returns all user\'s tasks', async () => {
let length; let createdTasks = await Q.all([
return Q.all([
user.post('/tasks', {text: 'test habit', type: 'habit'}), user.post('/tasks', {text: 'test habit', type: 'habit'}),
]) ]);
.then((createdTasks) => {
length = createdTasks.length; let length = createdTasks.length;
return user.get('/tasks'); let tasks = await user.get('/tasks');
})
.then((tasks) => {
expect(tasks.length).to.equal(length + 1); // + 1 because 1 is a default task expect(tasks.length).to.equal(length + 1); // + 1 because 1 is a default task
}); });
});
it('returns only a type of user\'s tasks if req.query.type is specified', () => { it('returns only a type of user\'s tasks if req.query.type is specified', async () => {
let habitId; let task = await user.post('/tasks', {text: 'test habit', type: 'habit'});
user.post('/tasks', {text: 'test habit', type: 'habit'}) let tasks = await user.get('/tasks?type=habit');
.then((task) => {
habitId = task._id;
return user.get('/tasks?type=habit');
})
.then((tasks) => {
expect(tasks.length).to.equal(1); expect(tasks.length).to.equal(1);
expect(tasks[0]._id).to.equal(habitId); expect(tasks[0]._id).to.equal(task._id);
});
}); });
// TODO complete after task scoring is done // TODO complete after task scoring is done

View File

@@ -7,64 +7,52 @@ import { v4 as generateUUID } from 'uuid';
describe('GET /tasks/:id', () => { describe('GET /tasks/:id', () => {
let user; let user;
before(() => { before(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser;
});
}); });
context('task can be accessed', () => { context('task can be accessed', async () => {
let task; let task;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { task = await user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
}).then((createdTask) => {
task = createdTask;
}); });
}); });
it('gets specified task', () => { it('gets specified task', async () => {
return user.get(`/tasks/${task._id}`) let getTask = await user.get(`/tasks/${task._id}`);
.then((getTask) => {
expect(getTask).to.eql(task); expect(getTask).to.eql(task);
}); });
});
// TODO after challenges are implemented // TODO after challenges are implemented
it('can get active challenge task that user does not own'); // Yes? it('can get active challenge task that user does not own'); // Yes?
}); });
context('task cannot be accessed', () => { context('task cannot be accessed', () => {
it('cannot get a non-existant task', () => { it('cannot get a non-existant task', async () => {
let dummyId = generateUUID(); let dummyId = generateUUID();
return expect(user.get(`/tasks/${dummyId}`)).to.eventually.be.rejected.and.eql({ await expect(user.get(`/tasks/${dummyId}`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('taskNotFound'), message: t('taskNotFound'),
}); });
}); });
it('cannot get a task owned by someone else', () => { it('cannot get a task owned by someone else', async () => {
let anotherUser; let anotherUser = await generateUser();
let task = await user.post('/tasks', {
return generateUser()
.then((user2) => {
anotherUser = user2;
return user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
}); });
}).then((task) => {
return expect(anotherUser.get(`/tasks/${task._id}`)).to.eventually.be.rejected.and.eql({ await expect(anotherUser.get(`/tasks/${task._id}`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('taskNotFound'), message: t('taskNotFound'),
}); });
}); });
}); });
});
}); });

View File

@@ -7,14 +7,12 @@ describe('POST /tasks', () => {
let user; let user;
before(async () => { before(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser;
});
}); });
context('validates params', () => { context('validates params', async () => {
it('returns an error if req.body.type is absent', async () => { it('returns an error if req.body.type is absent', async () => {
return expect(user.post('/tasks', { await expect(user.post('/tasks', {
notType: 'habit', notType: 'habit',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
code: 400, code: 400,
@@ -24,7 +22,7 @@ describe('POST /tasks', () => {
}); });
it('returns an error if req.body.type is not valid', async () => { it('returns an error if req.body.type is not valid', async () => {
return expect(user.post('/tasks', { await expect(user.post('/tasks', {
type: 'habitF', type: 'habitF',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
code: 400, code: 400,
@@ -34,7 +32,7 @@ describe('POST /tasks', () => {
}); });
it('returns an error if one object inside an array is invalid', async () => { it('returns an error if one object inside an array is invalid', async () => {
return expect(user.post('/tasks', [ await expect(user.post('/tasks', [
{type: 'habitF'}, {type: 'habitF'},
{type: 'habit'}, {type: 'habit'},
])).to.eventually.be.rejected.and.eql({ ])).to.eventually.be.rejected.and.eql({
@@ -45,7 +43,7 @@ describe('POST /tasks', () => {
}); });
it('returns an error if req.body.text is absent', async () => { it('returns an error if req.body.text is absent', async () => {
return expect(user.post('/tasks', { await expect(user.post('/tasks', {
type: 'habit', type: 'habit',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
code: 400, code: 400,
@@ -56,49 +54,46 @@ describe('POST /tasks', () => {
it('does not update user.tasksOrder.{taskType} when the task is not saved because invalid', async () => { it('does not update user.tasksOrder.{taskType} when the task is not saved because invalid', async () => {
let originalHabitsOrder = (await user.get('/user')).tasksOrder.habits; let originalHabitsOrder = (await user.get('/user')).tasksOrder.habits;
return expect(user.post('/tasks', { await expect(user.post('/tasks', {
type: 'habit', type: 'habit',
})).to.eventually.be.rejected.and.eql({ // this block is necessary })).to.eventually.be.rejected.and.eql({ // this block is necessary
code: 400, code: 400,
error: 'BadRequest', error: 'BadRequest',
message: 'habit validation failed', message: 'habit validation failed',
}).then(async () => {
let updatedHabitsOrder = (await user.get('/user')).tasksOrder.habits;
expect(updatedHabitsOrder).to.eql(originalHabitsOrder);
}); });
let updatedHabitsOrder = (await user.get('/user')).tasksOrder.habits;
expect(updatedHabitsOrder).to.eql(originalHabitsOrder);
}); });
it('does not update user.tasksOrder.{taskType} when a task inside an array is not saved because invalid', async () => { it('does not update user.tasksOrder.{taskType} when a task inside an array is not saved because invalid', async () => {
let originalHabitsOrder = (await user.get('/user')).tasksOrder.habits; let originalHabitsOrder = (await user.get('/user')).tasksOrder.habits;
return expect(user.post('/tasks', [ await expect(user.post('/tasks', [
{type: 'habit'}, // Missing text {type: 'habit'}, // Missing text
{type: 'habit', text: 'valid'}, // Valid {type: 'habit', text: 'valid'}, // Valid
])).to.eventually.be.rejected.and.eql({ // this block is necessary ])).to.eventually.be.rejected.and.eql({ // this block is necessary
code: 400, code: 400,
error: 'BadRequest', error: 'BadRequest',
message: 'habit validation failed', message: 'habit validation failed',
}).then(async () => {
let updatedHabitsOrder = (await user.get('/user')).tasksOrder.habits;
expect(updatedHabitsOrder).to.eql(originalHabitsOrder);
}); });
let updatedHabitsOrder = (await user.get('/user')).tasksOrder.habits;
expect(updatedHabitsOrder).to.eql(originalHabitsOrder);
}); });
it('does not save any task sent in an array when 1 is invalid', async () => { it('does not save any task sent in an array when 1 is invalid', async () => {
let originalTasks = await user.get('/tasks'); let originalTasks = await user.get('/tasks');
return expect(user.post('/tasks', [ await expect(user.post('/tasks', [
{type: 'habit'}, // Missing text {type: 'habit'}, // Missing text
{type: 'habit', text: 'valid'}, // Valid {type: 'habit', text: 'valid'}, // Valid
])).to.eventually.be.rejected.and.eql({ // this block is necessary ])).to.eventually.be.rejected.and.eql({ // this block is necessary
code: 400, code: 400,
error: 'BadRequest', error: 'BadRequest',
message: 'habit validation failed', message: 'habit validation failed',
}).then(async () => {
let updatedTasks = await user.get('/tasks');
expect(updatedTasks).to.eql(originalTasks);
}); });
let updatedTasks = await user.get('/tasks');
expect(updatedTasks).to.eql(originalTasks);
}); });
it('automatically sets "task.userId" to user\'s uuid', async () => { it('automatically sets "task.userId" to user\'s uuid', async () => {

View File

@@ -7,25 +7,23 @@ import { v4 as generateUUID } from 'uuid';
describe('POST /tasks/:id/score/:direction', () => { describe('POST /tasks/:id/score/:direction', () => {
let user; let user;
beforeEach(() => { beforeEach(async () => {
return generateUser({ user = await generateUser({
'stats.gp': 100, 'stats.gp': 100,
}).then((generatedUser) => {
user = generatedUser;
}); });
}); });
context('all', () => { context('all', () => {
it('requires a task id', () => { it('requires a task id', async () => {
return expect(user.post('/tasks/123/score/up')).to.eventually.be.rejected.and.eql({ await expect(user.post('/tasks/123/score/up')).to.eventually.be.rejected.and.eql({
code: 400, code: 400,
error: 'BadRequest', error: 'BadRequest',
message: t('invalidReqParams'), message: t('invalidReqParams'),
}); });
}); });
it('requires a task direction', () => { it('requires a task direction', async () => {
return expect(user.post(`/tasks/${generateUUID()}/score/tt`)).to.eventually.be.rejected.and.eql({ await expect(user.post(`/tasks/${generateUUID()}/score/tt`)).to.eventually.be.rejected.and.eql({
code: 400, code: 400,
error: 'BadRequest', error: 'BadRequest',
message: t('invalidReqParams'), message: t('invalidReqParams'),
@@ -36,110 +34,97 @@ describe('POST /tasks/:id/score/:direction', () => {
context('todos', () => { context('todos', () => {
let todo; let todo;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { todo = await user.post('/tasks', {
text: 'test todo', text: 'test todo',
type: 'todo', type: 'todo',
}).then((task) => {
todo = task;
}); });
}); });
it('completes todo when direction is up', () => { it('completes todo when direction is up', async () => {
return user.post(`/tasks/${todo._id}/score/up`) await user.post(`/tasks/${todo._id}/score/up`);
.then(() => user.get(`/tasks/${todo._id}`)) let task = await user.get(`/tasks/${todo._id}`);
.then((task) => expect(task.completed).to.equal(true));
expect(task.completed).to.equal(true);
}); });
it('moves completed todos out of user.tasksOrder.todos', () => { it('moves completed todos out of user.tasksOrder.todos', async () => {
return user.get('/user') let getUser = await user.get('/user');
.then(usr => { expect(getUser.tasksOrder.todos.indexOf(todo._id)).to.not.equal(-1);
expect(usr.tasksOrder.todos.indexOf(todo._id)).to.not.equal(-1);
}).then(() => user.post(`/tasks/${todo._id}/score/up`)) await user.post(`/tasks/${todo._id}/score/up`);
.then(() => user.get(`/tasks/${todo._id}`)) let updatedTask = await user.get(`/tasks/${todo._id}`);
.then((updatedTask) => {
expect(updatedTask.completed).to.equal(true); expect(updatedTask.completed).to.equal(true);
return user.get('/user');
}) let updatedUser = await user.get('/user');
.then((usr) => { expect(updatedUser.tasksOrder.todos.indexOf(todo._id)).to.equal(-1);
expect(usr.tasksOrder.todos.indexOf(todo._id)).to.equal(-1);
});
}); });
it('moves un-completed todos back into user.tasksOrder.todos', () => { it('moves un-completed todos back into user.tasksOrder.todos', async () => {
return user.get('/user') let getUser = await user.get('/user');
.then(usr => { expect(getUser.tasksOrder.todos.indexOf(todo._id)).to.not.equal(-1);
expect(usr.tasksOrder.todos.indexOf(todo._id)).to.not.equal(-1);
}).then(() => user.post(`/tasks/${todo._id}/score/up`)) await user.post(`/tasks/${todo._id}/score/up`);
.then(() => user.post(`/tasks/${todo._id}/score/down`)) await user.post(`/tasks/${todo._id}/score/down`);
.then(() => user.get(`/tasks/${todo._id}`))
.then((updatedTask) => { let updatedTask = await user.get(`/tasks/${todo._id}`);
expect(updatedTask.completed).to.equal(false); expect(updatedTask.completed).to.equal(false);
return user.get('/user');
}) let updatedUser = await user.get('/user');
.then((usr) => { let l = updatedUser.tasksOrder.todos.length;
let l = usr.tasksOrder.todos.length; expect(updatedUser.tasksOrder.todos.indexOf(todo._id)).not.to.equal(-1);
expect(usr.tasksOrder.todos.indexOf(todo._id)).not.to.equal(-1); expect(updatedUser.tasksOrder.todos.indexOf(todo._id)).to.equal(l - 1); // Check that it was pushed at the bottom
expect(usr.tasksOrder.todos.indexOf(todo._id)).to.equal(l - 1); // Check that it was pushed at the bottom
});
}); });
it('uncompletes todo when direction is down', () => { it('uncompletes todo when direction is down', async () => {
return user.post(`/tasks/${todo._id}/score/down`) await user.post(`/tasks/${todo._id}/score/down`);
.then(() => user.get(`/tasks/${todo._id}`)) let updatedTask = await user.get(`/tasks/${todo._id}`);
.then((updatedTask) => {
expect(updatedTask.completed).to.equal(false); expect(updatedTask.completed).to.equal(false);
}); });
});
it('scores up todo even if it is already completed'); // Yes? it('scores up todo even if it is already completed'); // Yes?
it('scores down todo even if it is already uncompleted'); // Yes? it('scores down todo even if it is already uncompleted'); // Yes?
it('increases user\'s mp when direction is up', () => { context('user stats when direction is up', () => {
return user.post(`/tasks/${todo._id}/score/up`) let updatedUser;
.then(() => user.get(`/user`))
.then((updatedUser) => { beforeEach(async () => {
await user.post(`/tasks/${todo._id}/score/up`);
updatedUser = await user.get(`/user`);
});
it('increases user\'s mp', () => {
expect(updatedUser.stats.mp).to.be.greaterThan(user.stats.mp); expect(updatedUser.stats.mp).to.be.greaterThan(user.stats.mp);
}); });
});
it('decreases user\'s mp when direction is down', () => { it('increases user\'s exp', () => {
return user.post(`/tasks/${todo._id}/score/down`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(updatedUser.stats.mp).to.be.lessThan(user.stats.mp);
});
});
it('increases user\'s exp when direction is up', () => {
return user.post(`/tasks/${todo._id}/score/up`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(updatedUser.stats.exp).to.be.greaterThan(user.stats.exp); expect(updatedUser.stats.exp).to.be.greaterThan(user.stats.exp);
}); });
});
it('decreases user\'s exp when direction is down', () => { it('increases user\'s gold', () => {
return user.post(`/tasks/${todo._id}/score/down`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(updatedUser.stats.exp).to.be.lessThan(user.stats.exp);
});
});
it('increases user\'s gold when direction is up', () => {
return user.post(`/tasks/${todo._id}/score/up`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(updatedUser.stats.gp).to.be.greaterThan(user.stats.gp); expect(updatedUser.stats.gp).to.be.greaterThan(user.stats.gp);
}); });
}); });
it('decreases user\'s gold when direction is down', () => { context('user stats when direction is down', () => {
return user.post(`/tasks/${todo._id}/score/down`) let updatedUser;
.then(() => user.get(`/user`))
.then((updatedUser) => { beforeEach(async () => {
await user.post(`/tasks/${todo._id}/score/down`);
updatedUser = await user.get(`/user`);
});
it('decreases user\'s mp', () => {
expect(updatedUser.stats.mp).to.be.lessThan(user.stats.mp);
});
it('decreases user\'s exp', () => {
expect(updatedUser.stats.exp).to.be.lessThan(user.stats.exp);
});
it('decreases user\'s gold', () => {
expect(updatedUser.stats.gp).to.be.lessThan(user.stats.gp); expect(updatedUser.stats.gp).to.be.lessThan(user.stats.gp);
}); });
}); });
@@ -148,75 +133,69 @@ describe('POST /tasks/:id/score/:direction', () => {
context('dailys', () => { context('dailys', () => {
let daily; let daily;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { daily = await user.post('/tasks', {
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
}).then((task) => {
daily = task;
}); });
}); });
it('completes daily when direction is up', () => { it('completes daily when direction is up', async () => {
return user.post(`/tasks/${daily._id}/score/up`) await user.post(`/tasks/${daily._id}/score/up`);
.then(() => user.get(`/tasks/${daily._id}`)) let task = await user.get(`/tasks/${daily._id}`);
.then((task) => expect(task.completed).to.equal(true));
expect(task.completed).to.equal(true);
}); });
it('uncompletes daily when direction is down', () => { it('uncompletes daily when direction is down', async () => {
return user.post(`/tasks/${daily._id}/score/down`) await user.post(`/tasks/${daily._id}/score/down`);
.then(() => user.get(`/tasks/${daily._id}`)) let task = await user.get(`/tasks/${daily._id}`);
.then((task) => expect(task.completed).to.equal(false));
expect(task.completed).to.equal(false);
}); });
it('scores up daily even if it is already completed'); // Yes? it('scores up daily even if it is already completed'); // Yes?
it('scores down daily even if it is already uncompleted'); // Yes? it('scores down daily even if it is already uncompleted'); // Yes?
it('increases user\'s mp when direction is up', () => { context('user stats when direction is up', () => {
return user.post(`/tasks/${daily._id}/score/up`) let updatedUser;
.then(() => user.get(`/user`))
.then((updatedUser) => { beforeEach(async () => {
await user.post(`/tasks/${daily._id}/score/up`);
updatedUser = await user.get(`/user`);
});
it('increases user\'s mp', () => {
expect(updatedUser.stats.mp).to.be.greaterThan(user.stats.mp); expect(updatedUser.stats.mp).to.be.greaterThan(user.stats.mp);
}); });
});
it('decreases user\'s mp when direction is down', () => { it('increases user\'s exp', () => {
return user.post(`/tasks/${daily._id}/score/down`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(updatedUser.stats.mp).to.be.lessThan(user.stats.mp);
});
});
it('increases user\'s exp when direction is up', () => {
return user.post(`/tasks/${daily._id}/score/up`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(updatedUser.stats.exp).to.be.greaterThan(user.stats.exp); expect(updatedUser.stats.exp).to.be.greaterThan(user.stats.exp);
}); });
});
it('decreases user\'s exp when direction is down', () => { it('increases user\'s gold', () => {
return user.post(`/tasks/${daily._id}/score/down`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(updatedUser.stats.exp).to.be.lessThan(user.stats.exp);
});
});
it('increases user\'s gold when direction is up', () => {
return user.post(`/tasks/${daily._id}/score/up`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(updatedUser.stats.gp).to.be.greaterThan(user.stats.gp); expect(updatedUser.stats.gp).to.be.greaterThan(user.stats.gp);
}); });
}); });
it('decreases user\'s gold when direction is down', () => { context('user stats when direction is down', () => {
return user.post(`/tasks/${daily._id}/score/down`) let updatedUser;
.then(() => user.get(`/user`))
.then((updatedUser) => { beforeEach(async () => {
await user.post(`/tasks/${daily._id}/score/down`);
updatedUser = await user.get(`/user`);
});
it('decreases user\'s mp', () => {
expect(updatedUser.stats.mp).to.be.lessThan(user.stats.mp);
});
it('decreases user\'s exp', () => {
expect(updatedUser.stats.exp).to.be.lessThan(user.stats.exp);
});
it('decreases user\'s gold', () => {
expect(updatedUser.stats.gp).to.be.lessThan(user.stats.gp); expect(updatedUser.stats.gp).to.be.lessThan(user.stats.gp);
}); });
}); });
@@ -225,117 +204,93 @@ describe('POST /tasks/:id/score/:direction', () => {
context('habits', () => { context('habits', () => {
let habit, minusHabit, plusHabit, neitherHabit; // eslint-disable-line no-unused-vars let habit, minusHabit, plusHabit, neitherHabit; // eslint-disable-line no-unused-vars
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { habit = await user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
}).then((task) => { });
habit = task;
return user.post('/tasks', { minusHabit = await user.post('/tasks', {
text: 'test min habit', text: 'test min habit',
type: 'habit', type: 'habit',
up: false, up: false,
}); });
}).then((task) => {
minusHabit = task; plusHabit = await user.post('/tasks', {
return user.post('/tasks', {
text: 'test plus habit', text: 'test plus habit',
type: 'habit', type: 'habit',
down: false, down: false,
}); });
}).then((task) => {
plusHabit = task; neitherHabit = await user.post('/tasks', {
user.post('/tasks', {
text: 'test neither habit', text: 'test neither habit',
type: 'habit', type: 'habit',
up: false, up: false,
down: false, down: false,
}); });
}).then((task) => {
neitherHabit = task;
});
}); });
it('prevents plus only habit from scoring down'); // Yes? it('prevents plus only habit from scoring down'); // Yes?
it('prevents minus only habit from scoring up'); // Yes? it('prevents minus only habit from scoring up'); // Yes?
it('increases user\'s mp when direction is up', () => { it('increases user\'s mp when direction is up', async () => {
return user.post(`/tasks/${habit._id}/score/up`) await user.post(`/tasks/${habit._id}/score/up`);
.then(() => user.get(`/user`)) let updatedUser = await user.get(`/user`);
.then((updatedUser) => {
expect(updatedUser.stats.mp).to.be.greaterThan(user.stats.mp); expect(updatedUser.stats.mp).to.be.greaterThan(user.stats.mp);
}); });
});
it('decreases user\'s mp when direction is down', () => { it('decreases user\'s mp when direction is down', async () => {
return user.post(`/tasks/${habit._id}/score/down`) await user.post(`/tasks/${habit._id}/score/down`);
.then(() => user.get(`/user`)) let updatedUser = await user.get(`/user`);
.then((updatedUser) => {
expect(updatedUser.stats.mp).to.be.lessThan(user.stats.mp); expect(updatedUser.stats.mp).to.be.lessThan(user.stats.mp);
}); });
});
it('increases user\'s exp when direction is up', () => { it('increases user\'s exp when direction is up', async () => {
return user.post(`/tasks/${habit._id}/score/up`) await user.post(`/tasks/${habit._id}/score/up`);
.then(() => user.get(`/user`)) let updatedUser = await user.get(`/user`);
.then((updatedUser) => {
expect(updatedUser.stats.exp).to.be.greaterThan(user.stats.exp); expect(updatedUser.stats.exp).to.be.greaterThan(user.stats.exp);
}); });
});
it('increases user\'s gold when direction is up', () => { it('increases user\'s gold when direction is up', async () => {
return user.post(`/tasks/${habit._id}/score/up`) await user.post(`/tasks/${habit._id}/score/up`);
.then(() => user.get(`/user`)) let updatedUser = await user.get(`/user`);
.then((updatedUser) => {
expect(updatedUser.stats.gp).to.be.greaterThan(user.stats.gp); expect(updatedUser.stats.gp).to.be.greaterThan(user.stats.gp);
}); });
}); });
});
context('reward', () => { context('reward', () => {
let reward; let reward, updatedUser;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { reward = await user.post('/tasks', {
text: 'test reward', text: 'test reward',
type: 'reward', type: 'reward',
value: 5, value: 5,
}).then((task) => {
reward = task;
}); });
await user.post(`/tasks/${reward._id}/score/up`);
updatedUser = await user.get(`/user`);
}); });
it('purchases reward', () => { it('purchases reward', () => {
return user.post(`/tasks/${reward._id}/score/up`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(user.stats.gp).to.equal(updatedUser.stats.gp + 5); expect(user.stats.gp).to.equal(updatedUser.stats.gp + 5);
}); });
});
it('does not change user\'s mp', () => { it('does not change user\'s mp', () => {
return user.post(`/tasks/${reward._id}/score/up`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(user.stats.mp).to.equal(updatedUser.stats.mp); expect(user.stats.mp).to.equal(updatedUser.stats.mp);
}); });
});
it('does not change user\'s exp', () => { it('does not change user\'s exp', () => {
return user.post(`/tasks/${reward._id}/score/up`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(user.stats.exp).to.equal(updatedUser.stats.exp); expect(user.stats.exp).to.equal(updatedUser.stats.exp);
}); });
});
it('does not allow a down direction', () => { it('does not allow a down direction', () => {
return user.post(`/tasks/${reward._id}/score/up`)
.then(() => user.get(`/user`))
.then((updatedUser) => {
expect(user.stats.mp).to.equal(updatedUser.stats.mp); expect(user.stats.mp).to.equal(updatedUser.stats.mp);
}); });
}); });
});
}); });

View File

@@ -6,21 +6,17 @@ import { v4 as generateUUID } from 'uuid';
describe('PUT /tasks/:id', () => { describe('PUT /tasks/:id', () => {
let user; let user;
before(() => { before(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser;
});
}); });
context('validates params', () => { context('validates params', () => {
let task; let task;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { task = await user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
}).then((createdTask) => {
task = createdTask;
}); });
}); });
@@ -52,172 +48,167 @@ describe('PUT /tasks/:id', () => {
}); });
}); });
it('ignores invalid fields', () => { it('ignores invalid fields', async () => {
user.put(`/tasks/${task._id}`, { let savedTask = await user.put(`/tasks/${task._id}`, {
notValid: true, notValid: true,
}).then((savedTask) => {
expect(savedTask.notValid).to.be.a('undefined');
}); });
expect(savedTask.notValid).to.be.undefined;
}); });
}); });
context('habits', () => { context('habits', () => {
let habit; let habit;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { habit = await user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
notes: 1976, notes: 1976,
}).then((createdHabit) => {
habit = createdHabit;
}); });
}); });
it('updates a habit', () => { it('updates a habit', async () => {
return user.put(`/tasks/${habit._id}`, { let savedHabit = await user.put(`/tasks/${habit._id}`, {
text: 'some new text', text: 'some new text',
up: false, up: false,
down: false, down: false,
notes: 'some new notes', notes: 'some new notes',
}).then((task) => {
expect(task.text).to.eql('some new text');
expect(task.notes).to.eql('some new notes');
expect(task.up).to.eql(false);
expect(task.down).to.eql(false);
}); });
expect(savedHabit.text).to.eql('some new text');
expect(savedHabit.notes).to.eql('some new notes');
expect(savedHabit.up).to.eql(false);
expect(savedHabit.down).to.eql(false);
}); });
}); });
context('todos', () => { context('todos', () => {
let todo; let todo;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { todo = await user.post('/tasks', {
text: 'test todo', text: 'test todo',
type: 'todo', type: 'todo',
notes: 1976, notes: 1976,
}).then((createdTodo) => {
todo = createdTodo;
}); });
}); });
it('updates a todo', () => { it('updates a todo', async () => {
return user.put(`/tasks/${todo._id}`, { let savedTodo = await user.put(`/tasks/${todo._id}`, {
text: 'some new text', text: 'some new text',
notes: 'some new notes', notes: 'some new notes',
}).then((task) => {
expect(task.text).to.eql('some new text');
expect(task.notes).to.eql('some new notes');
});
}); });
it('can update checklists (replace it)', () => { expect(savedTodo.text).to.eql('some new text');
return user.put(`/tasks/${todo._id}`, { expect(savedTodo.notes).to.eql('some new notes');
});
it('can update checklists (replace it)', async () => {
await user.put(`/tasks/${todo._id}`, {
checklist: [ checklist: [
{text: 123, completed: false}, {text: 123, completed: false},
{text: 456, completed: true}, {text: 456, completed: true},
], ],
}).then(() => { });
return user.put(`/tasks/${todo._id}`, {
let savedTodo = await user.put(`/tasks/${todo._id}`, {
checklist: [ checklist: [
{text: 789, completed: false}, {text: 789, completed: false},
], ],
}); });
}).then((savedTodo2) => {
expect(savedTodo2.checklist.length).to.equal(1); expect(savedTodo.checklist.length).to.equal(1);
expect(savedTodo2.checklist[0].text).to.equal('789'); expect(savedTodo.checklist[0].text).to.equal('789');
expect(savedTodo2.checklist[0].completed).to.equal(false); expect(savedTodo.checklist[0].completed).to.equal(false);
});
}); });
it('can update tags (replace them)', () => { it('can update tags (replace them)', async () => {
let finalUUID = generateUUID(); let finalUUID = generateUUID();
return user.put(`/tasks/${todo._id}`, { await user.put(`/tasks/${todo._id}`, {
tags: [generateUUID(), generateUUID()], tags: [generateUUID(), generateUUID()],
}).then(() => { });
return user.put(`/tasks/${todo._id}`, {
let savedTodo = await user.put(`/tasks/${todo._id}`, {
tags: [finalUUID], tags: [finalUUID],
}); });
}).then((savedTodo2) => {
expect(savedTodo2.tags.length).to.equal(1); expect(savedTodo.tags.length).to.equal(1);
expect(savedTodo2.tags[0]).to.equal(finalUUID); expect(savedTodo.tags[0]).to.equal(finalUUID);
});
}); });
}); });
context('dailys', () => { context('dailys', () => {
let daily; let daily;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { daily = await user.post('/tasks', {
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
notes: 1976, notes: 1976,
}).then((createdDaily) => {
daily = createdDaily;
}); });
}); });
it('updates a daily', () => { it('updates a daily', async () => {
return user.put(`/tasks/${daily._id}`, { let savedDaily = await user.put(`/tasks/${daily._id}`, {
text: 'some new text', text: 'some new text',
notes: 'some new notes', notes: 'some new notes',
frequency: 'daily', frequency: 'daily',
everyX: 5, everyX: 5,
}).then((task) => {
expect(task.text).to.eql('some new text');
expect(task.notes).to.eql('some new notes');
expect(task.frequency).to.eql('daily');
expect(task.everyX).to.eql(5);
});
}); });
it('can update checklists (replace it)', () => { expect(savedDaily.text).to.eql('some new text');
return user.put(`/tasks/${daily._id}`, { expect(savedDaily.notes).to.eql('some new notes');
expect(savedDaily.frequency).to.eql('daily');
expect(savedDaily.everyX).to.eql(5);
});
it('can update checklists (replace it)', async () => {
await user.put(`/tasks/${daily._id}`, {
checklist: [ checklist: [
{text: 123, completed: false}, {text: 123, completed: false},
{text: 456, completed: true}, {text: 456, completed: true},
], ],
}).then(() => { });
return user.put(`/tasks/${daily._id}`, {
let savedDaily = await user.put(`/tasks/${daily._id}`, {
checklist: [ checklist: [
{text: 789, completed: false}, {text: 789, completed: false},
], ],
}); });
}).then((savedDaily2) => {
expect(savedDaily2.checklist.length).to.equal(1); expect(savedDaily.checklist.length).to.equal(1);
expect(savedDaily2.checklist[0].text).to.equal('789'); expect(savedDaily.checklist[0].text).to.equal('789');
expect(savedDaily2.checklist[0].completed).to.equal(false); expect(savedDaily.checklist[0].completed).to.equal(false);
});
}); });
it('can update tags (replace them)', () => { it('can update tags (replace them)', async () => {
let finalUUID = generateUUID(); let finalUUID = generateUUID();
return user.put(`/tasks/${daily._id}`, { await user.put(`/tasks/${daily._id}`, {
tags: [generateUUID(), generateUUID()], tags: [generateUUID(), generateUUID()],
}).then(() => { });
return user.put(`/tasks/${daily._id}`, {
let savedDaily = await user.put(`/tasks/${daily._id}`, {
tags: [finalUUID], tags: [finalUUID],
}); });
}).then((savedDaily2) => {
expect(savedDaily2.tags.length).to.equal(1); expect(savedDaily.tags.length).to.equal(1);
expect(savedDaily2.tags[0]).to.equal(finalUUID); expect(savedDaily.tags[0]).to.equal(finalUUID);
});
}); });
it('updates repeat, even if frequency is set to daily', () => { it('updates repeat, even if frequency is set to daily', async () => {
return user.put(`/tasks/${daily._id}`, { await user.put(`/tasks/${daily._id}`, {
frequency: 'daily', frequency: 'daily',
}).then(() => { });
return user.put(`/tasks/${daily._id}`, {
let savedDaily = await user.put(`/tasks/${daily._id}`, {
repeat: { repeat: {
m: false, m: false,
su: false, su: false,
}, },
}); });
}).then((savedDaily2) => {
expect(savedDaily2.repeat).to.eql({ expect(savedDaily.repeat).to.eql({
m: false, m: false,
t: true, t: true,
w: true, w: true,
@@ -227,61 +218,58 @@ describe('PUT /tasks/:id', () => {
su: false, su: false,
}); });
}); });
it('updates everyX, even if frequency is set to weekly', async () => {
await user.put(`/tasks/${daily._id}`, {
frequency: 'weekly',
}); });
it('updates everyX, even if frequency is set to weekly', () => { let savedDaily = await user.put(`/tasks/${daily._id}`, {
return user.put(`/tasks/${daily._id}`, {
frequency: 'weekly',
}).then(() => {
return user.put(`/tasks/${daily._id}`, {
everyX: 5, everyX: 5,
}); });
}).then((savedDaily2) => {
expect(savedDaily2.everyX).to.eql(5); expect(savedDaily.everyX).to.eql(5);
});
}); });
it('defaults startDate to today if none date object is passed in', () => { it('defaults startDate to today if none date object is passed in', async () => {
return user.put(`/tasks/${daily._id}`, { let savedDaily = await user.put(`/tasks/${daily._id}`, {
frequency: 'weekly', frequency: 'weekly',
}).then((savedDaily2) => {
expect((new Date(savedDaily2.startDate)).getDay()).to.eql((new Date()).getDay());
}); });
expect((new Date(savedDaily.startDate)).getDay()).to.eql((new Date()).getDay());
}); });
}); });
context('rewards', () => { context('rewards', () => {
let reward; let reward;
beforeEach(() => { beforeEach(async () => {
return user.post('/tasks', { reward = await user.post('/tasks', {
text: 'test reward', text: 'test reward',
type: 'reward', type: 'reward',
notes: 1976, notes: 1976,
value: 10, value: 10,
}).then((createdReward) => {
reward = createdReward;
}); });
}); });
it('updates a reward', () => { it('updates a reward', async () => {
return user.put(`/tasks/${reward._id}`, { let savedReward = await user.put(`/tasks/${reward._id}`, {
text: 'some new text', text: 'some new text',
notes: 'some new notes', notes: 'some new notes',
value: 10, value: 10,
}).then((task) => {
expect(task.text).to.eql('some new text');
expect(task.notes).to.eql('some new notes');
expect(task.value).to.eql(10);
});
}); });
it('requires value to be coerced into a number', () => { expect(savedReward.text).to.eql('some new text');
return user.put(`/tasks/${reward._id}`, { expect(savedReward.notes).to.eql('some new notes');
value: '100', expect(savedReward.value).to.eql(10);
}).then((task) => {
expect(task.value).to.eql(100);
}); });
it('requires value to be coerced into a number', async () => {
let savedReward = await user.put(`/tasks/${reward._id}`, {
value: '100',
});
expect(savedReward.value).to.eql(100);
}); });
}); });
}); });

View File

@@ -7,39 +7,31 @@ import { v4 as generateUUID } from 'uuid';
describe('DELETE /tasks/:taskId/checklist/:itemId', () => { describe('DELETE /tasks/:taskId/checklist/:itemId', () => {
let user; let user;
before(() => { before(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser;
});
}); });
it('deletes a checklist item', () => { it('deletes a checklist item', async () => {
let task; let task = await user.post('/tasks', {
return user.post('/tasks', {
type: 'daily', type: 'daily',
text: 'Daily with checklist', text: 'Daily with checklist',
}).then(createdTask => {
task = createdTask;
return user.post(`/tasks/${task._id}/checklist`, {text: 'Checklist Item 1', completed: false});
}).then((savedTask) => {
return user.del(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}`);
}).then(() => {
return user.get(`/tasks/${task._id}`);
}).then((savedTask) => {
expect(savedTask.checklist.length).to.equal(0);
});
}); });
it('does not work with habits', () => { let savedTask = await user.post(`/tasks/${task._id}/checklist`, {text: 'Checklist Item 1', completed: false});
let habit;
return expect(user.post('/tasks', { await user.del(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}`);
savedTask = await user.get(`/tasks/${task._id}`);
expect(savedTask.checklist.length).to.equal(0);
});
it('does not work with habits', async () => {
let habit = await user.post('/tasks', {
type: 'habit', type: 'habit',
text: 'habit with checklist', text: 'habit with checklist',
}).then(createdTask => { });
habit = createdTask;
return user.del(`/tasks/${habit._id}/checklist/${generateUUID()}`); await expect(user.del(`/tasks/${habit._id}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
})).to.eventually.be.rejected.and.eql({
code: 400, code: 400,
error: 'BadRequest', error: 'BadRequest',
message: t('checklistOnlyDailyTodo'), message: t('checklistOnlyDailyTodo'),
@@ -59,21 +51,21 @@ describe('DELETE /tasks/:taskId/checklist/:itemId', () => {
}); });
}); });
it('fails on task not found', () => { it('fails on task not found', async () => {
return expect(user.del(`/tasks/${generateUUID()}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({ await expect(user.del(`/tasks/${generateUUID()}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('taskNotFound'), message: t('taskNotFound'),
}); });
}); });
it('fails on checklist item not found', () => { it('fails on checklist item not found', async () => {
return expect(user.post('/tasks', { let createdTask = await user.post('/tasks', {
type: 'daily', type: 'daily',
text: 'daily with checklist', text: 'daily with checklist',
}).then(createdTask => { });
return user.del(`/tasks/${createdTask._id}/checklist/${generateUUID()}`);
})).to.eventually.be.rejected.and.eql({ await expect(user.del(`/tasks/${createdTask._id}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('checklistItemNotFound'), message: t('checklistItemNotFound'),

View File

@@ -7,23 +7,22 @@ import { v4 as generateUUID } from 'uuid';
describe('POST /tasks/:taskId/checklist/', () => { describe('POST /tasks/:taskId/checklist/', () => {
let user; let user;
before(() => { before(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser;
});
}); });
it('adds a checklist item to a task', () => { it('adds a checklist item to a task', async () => {
let task; let task = await user.post('/tasks', {
return user.post('/tasks', {
type: 'daily', type: 'daily',
text: 'Daily with checklist', text: 'Daily with checklist',
}).then(createdTask => { });
task = createdTask;
let savedTask = await user.post(`/tasks/${task._id}/checklist`, {
text: 'Checklist Item 1',
ignored: false,
_id: 123,
});
return user.post(`/tasks/${task._id}/checklist`, {text: 'Checklist Item 1', ignored: false, _id: 123});
}).then((savedTask) => {
expect(savedTask.checklist.length).to.equal(1); expect(savedTask.checklist.length).to.equal(1);
expect(savedTask.checklist[0].text).to.equal('Checklist Item 1'); expect(savedTask.checklist[0].text).to.equal('Checklist Item 1');
expect(savedTask.checklist[0].completed).to.equal(false); expect(savedTask.checklist[0].completed).to.equal(false);
@@ -31,17 +30,15 @@ describe('POST /tasks/:taskId/checklist/', () => {
expect(savedTask.checklist[0]._id).to.not.equal('123'); expect(savedTask.checklist[0]._id).to.not.equal('123');
expect(savedTask.checklist[0].ignored).to.be.an('undefined'); expect(savedTask.checklist[0].ignored).to.be.an('undefined');
}); });
});
it('does not add a checklist to habits', () => { it('does not add a checklist to habits', async () => {
let habit; let habit = await user.post('/tasks', {
return expect(user.post('/tasks', {
type: 'habit', type: 'habit',
text: 'habit with checklist', text: 'habit with checklist',
}).then(createdTask => { });
habit = createdTask;
return user.post(`/tasks/${habit._id}/checklist`, {text: 'Checklist Item 1'}); await expect(user.post(`/tasks/${habit._id}/checklist`, {
text: 'Checklist Item 1',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
code: 400, code: 400,
error: 'BadRequest', error: 'BadRequest',
@@ -49,14 +46,14 @@ describe('POST /tasks/:taskId/checklist/', () => {
}); });
}); });
it('does not add a checklist to rewards', () => { it('does not add a checklist to rewards', async () => {
let reward; let reward = await user.post('/tasks', {
return expect(user.post('/tasks', {
type: 'reward', type: 'reward',
text: 'reward with checklist', text: 'reward with checklist',
}).then(createdTask => { });
reward = createdTask;
return user.post(`/tasks/${reward._id}/checklist`, {text: 'Checklist Item 1'}); await expect(user.post(`/tasks/${reward._id}/checklist`, {
text: 'Checklist Item 1',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
code: 400, code: 400,
error: 'BadRequest', error: 'BadRequest',
@@ -64,8 +61,8 @@ describe('POST /tasks/:taskId/checklist/', () => {
}); });
}); });
it('fails on task not found', () => { it('fails on task not found', async () => {
return expect(user.post(`/tasks/${generateUUID()}/checklist`, { await expect(user.post(`/tasks/${generateUUID()}/checklist`, {
text: 'Checklist Item 1', text: 'Checklist Item 1',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
code: 404, code: 404,

View File

@@ -7,37 +7,35 @@ import { v4 as generateUUID } from 'uuid';
describe('POST /tasks/:taskId/checklist/:itemId/score', () => { describe('POST /tasks/:taskId/checklist/:itemId/score', () => {
let user; let user;
before(() => { before(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser;
});
}); });
it('scores a checklist item', () => { it('scores a checklist item', async () => {
let task; let task = await user.post('/tasks', {
return user.post('/tasks', {
type: 'daily', type: 'daily',
text: 'Daily with checklist', text: 'Daily with checklist',
}).then(createdTask => { });
task = createdTask;
return user.post(`/tasks/${task._id}/checklist`, {text: 'Checklist Item 1', completed: false}); let savedTask = await user.post(`/tasks/${task._id}/checklist`, {
}).then((savedTask) => { text: 'Checklist Item 1',
return user.post(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}/score`); completed: false,
}).then((savedTask) => { });
savedTask = await user.post(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}/score`);
expect(savedTask.checklist.length).to.equal(1); expect(savedTask.checklist.length).to.equal(1);
expect(savedTask.checklist[0].completed).to.equal(true); expect(savedTask.checklist[0].completed).to.equal(true);
}); });
});
it('fails on habits', () => { it('fails on habits', async () => {
let habit; let habit = await user.post('/tasks', {
return expect(user.post('/tasks', {
type: 'habit', type: 'habit',
text: 'habit with checklist', text: 'habit with checklist',
}).then(createdTask => { });
habit = createdTask;
return user.post(`/tasks/${habit._id}/checklist/${generateUUID()}/score`, {text: 'Checklist Item 1'}); await expect(user.post(`/tasks/${habit._id}/checklist/${generateUUID()}/score`, {
text: 'Checklist Item 1',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
code: 400, code: 400,
error: 'BadRequest', error: 'BadRequest',
@@ -58,21 +56,21 @@ describe('POST /tasks/:taskId/checklist/:itemId/score', () => {
}); });
}); });
it('fails on task not found', () => { it('fails on task not found', async () => {
return expect(user.post(`/tasks/${generateUUID()}/checklist/${generateUUID()}/score`)).to.eventually.be.rejected.and.eql({ await expect(user.post(`/tasks/${generateUUID()}/checklist/${generateUUID()}/score`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('taskNotFound'), message: t('taskNotFound'),
}); });
}); });
it('fails on checklist item not found', () => { it('fails on checklist item not found', async () => {
return expect(user.post('/tasks', { let createdTask = await user.post('/tasks', {
type: 'daily', type: 'daily',
text: 'daily with checklist', text: 'daily with checklist',
}).then(createdTask => { });
return user.post(`/tasks/${createdTask._id}/checklist/${generateUUID()}/score`);
})).to.eventually.be.rejected.and.eql({ await expect(user.post(`/tasks/${createdTask._id}/checklist/${generateUUID()}/score`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('checklistItemNotFound'), message: t('checklistItemNotFound'),

View File

@@ -7,30 +7,32 @@ import { v4 as generateUUID } from 'uuid';
describe('PUT /tasks/:taskId/checklist/:itemId', () => { describe('PUT /tasks/:taskId/checklist/:itemId', () => {
let user; let user;
before(() => { before(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser;
});
}); });
it('updates a checklist item', () => { it('updates a checklist item', async () => {
let task; let task = await user.post('/tasks', {
return user.post('/tasks', {
type: 'daily', type: 'daily',
text: 'Daily with checklist', text: 'Daily with checklist',
}).then(createdTask => { });
task = createdTask;
return user.post(`/tasks/${task._id}/checklist`, {text: 'Checklist Item 1', completed: false}); let savedTask = await user.post(`/tasks/${task._id}/checklist`, {
}).then((savedTask) => { text: 'Checklist Item 1',
return user.put(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}`, {text: 'updated', completed: true, _id: 123}); completed: false,
}).then((savedTask) => { });
savedTask = await user.put(`/tasks/${task._id}/checklist/${savedTask.checklist[0]._id}`, {
text: 'updated',
completed: true,
_id: 123,
});
expect(savedTask.checklist.length).to.equal(1); expect(savedTask.checklist.length).to.equal(1);
expect(savedTask.checklist[0].text).to.equal('updated'); expect(savedTask.checklist[0].text).to.equal('updated');
expect(savedTask.checklist[0].completed).to.equal(true); expect(savedTask.checklist[0].completed).to.equal(true);
expect(savedTask.checklist[0]._id).to.not.equal('123'); expect(savedTask.checklist[0]._id).to.not.equal('123');
}); });
});
it('fails on habits', async () => { it('fails on habits', async () => {
let habit = await user.post('/tasks', { let habit = await user.post('/tasks', {
@@ -58,21 +60,21 @@ describe('PUT /tasks/:taskId/checklist/:itemId', () => {
}); });
}); });
it('fails on task not found', () => { it('fails on task not found', async () => {
return expect(user.put(`/tasks/${generateUUID()}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({ await expect(user.put(`/tasks/${generateUUID()}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('taskNotFound'), message: t('taskNotFound'),
}); });
}); });
it('fails on checklist item not found', () => { it('fails on checklist item not found', async () => {
return expect(user.post('/tasks', { let createdTask = await user.post('/tasks', {
type: 'daily', type: 'daily',
text: 'daily with checklist', text: 'daily with checklist',
}).then(createdTask => { });
return user.put(`/tasks/${createdTask._id}/checklist/${generateUUID()}`);
})).to.eventually.be.rejected.and.eql({ await expect(user.put(`/tasks/${createdTask._id}/checklist/${generateUUID()}`)).to.eventually.be.rejected.and.eql({
code: 404, code: 404,
error: 'NotFound', error: 'NotFound',
message: t('checklistItemNotFound'), message: t('checklistItemNotFound'),

View File

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

View File

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