update POST-tasks test to async/await syntax

This commit is contained in:
Matteo Pagliazzi
2016-01-02 16:04:39 +01:00
parent 775766b30f
commit 7490bfae87

View File

@@ -6,14 +6,14 @@ import {
describe('POST /tasks', () => { describe('POST /tasks', () => {
let user; let user;
before(() => { before(async () => {
return generateUser().then((generatedUser) => { return generateUser().then((generatedUser) => {
user = generatedUser; user = generatedUser;
}); });
}); });
context('validates params', () => { context('validates params', () => {
it('returns an error if req.body.type is absent', () => { it('returns an error if req.body.type is absent', async () => {
return expect(user.post('/tasks', { return expect(user.post('/tasks', {
notType: 'habit', notType: 'habit',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
@@ -23,7 +23,7 @@ describe('POST /tasks', () => {
}); });
}); });
it('returns an error if req.body.type is not valid', () => { it('returns an error if req.body.type is not valid', async () => {
return expect(user.post('/tasks', { return expect(user.post('/tasks', {
type: 'habitF', type: 'habitF',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
@@ -33,7 +33,7 @@ describe('POST /tasks', () => {
}); });
}); });
it('returns an error if one object inside an array is invalid', () => { it('returns an error if one object inside an array is invalid', async () => {
return expect(user.post('/tasks', [ return expect(user.post('/tasks', [
{type: 'habitF'}, {type: 'habitF'},
{type: 'habit'}, {type: 'habit'},
@@ -44,7 +44,7 @@ describe('POST /tasks', () => {
}); });
}); });
it('returns an error if req.body.text is absent', () => { it('returns an error if req.body.text is absent', async () => {
return expect(user.post('/tasks', { return expect(user.post('/tasks', {
type: 'habit', type: 'habit',
})).to.eventually.be.rejected.and.eql({ })).to.eventually.be.rejected.and.eql({
@@ -54,19 +54,19 @@ describe('POST /tasks', () => {
}); });
}); });
it('automatically sets "task.userId" to user\'s uuid', () => { it('automatically sets "task.userId" to user\'s uuid', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
}).then((task) => {
expect(task.userId).to.equal(user._id);
}); });
expect(task.userId).to.equal(user._id);
}); });
it(`ignores setting userId, history, createdAt, it(`ignores setting userId, history, createdAt,
updatedAt, challenge, completed, streak, updatedAt, challenge, completed, streak,
dateCompleted fields`, () => { dateCompleted fields`, async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
userId: 123, userId: 123,
@@ -77,49 +77,49 @@ describe('POST /tasks', () => {
completed: true, completed: true,
streak: 25, streak: 25,
dateCompleted: 'never', dateCompleted: 'never',
}).then((task) => {
expect(task.userId).to.equal(user._id);
expect(task.history).to.eql([]);
expect(task.createdAt).not.to.equal('yesterday');
expect(task.updatedAt).not.to.equal('tomorrow');
expect(task.challenge).not.to.equal('no');
expect(task.completed).to.equal(false);
expect(task.streak).to.equal(0);
expect(task.streak).not.to.equal('never');
}); });
expect(task.userId).to.equal(user._id);
expect(task.history).to.eql([]);
expect(task.createdAt).not.to.equal('yesterday');
expect(task.updatedAt).not.to.equal('tomorrow');
expect(task.challenge).not.to.equal('no');
expect(task.completed).to.equal(false);
expect(task.streak).to.equal(0);
expect(task.streak).not.to.equal('never');
}); });
it('ignores invalid fields', () => { it('ignores invalid fields', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
notValid: true, notValid: true,
}).then((task) => {
expect(task).not.to.have.property('notValid');
}); });
expect(task).not.to.have.property('notValid');
}); });
}); });
context('habits', () => { context('habits', () => {
it('creates a habit', () => { it('creates a habit', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
up: false, up: false,
down: true, down: true,
notes: 1976, notes: 1976,
}).then((task) => {
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test habit');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('habit');
expect(task.up).to.eql(false);
expect(task.down).to.eql(true);
}); });
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test habit');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('habit');
expect(task.up).to.eql(false);
expect(task.down).to.eql(true);
}); });
it('creates multiple habits', () => { it('creates multiple habits', async () => {
return user.post('/tasks', [{ let [task, task2] = await user.post('/tasks', [{
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
up: false, up: false,
@@ -131,63 +131,63 @@ describe('POST /tasks', () => {
up: true, up: true,
down: false, down: false,
notes: 1977, notes: 1977,
}]).then(([task, task2]) => { }]);
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test habit');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('habit');
expect(task.up).to.eql(false);
expect(task.down).to.eql(true);
expect(task2.userId).to.equal(user._id); expect(task.userId).to.equal(user._id);
expect(task2.text).to.eql('test habit 2'); expect(task.text).to.eql('test habit');
expect(task2.notes).to.eql('1977'); expect(task.notes).to.eql('1976');
expect(task2.type).to.eql('habit'); expect(task.type).to.eql('habit');
expect(task2.up).to.eql(true); expect(task.up).to.eql(false);
expect(task2.down).to.eql(false); expect(task.down).to.eql(true);
});
expect(task2.userId).to.equal(user._id);
expect(task2.text).to.eql('test habit 2');
expect(task2.notes).to.eql('1977');
expect(task2.type).to.eql('habit');
expect(task2.up).to.eql(true);
expect(task2.down).to.eql(false);
}); });
it('defaults to setting up and down to true', () => { it('defaults to setting up and down to true', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
notes: 1976, notes: 1976,
}).then((task) => {
expect(task.up).to.eql(true);
expect(task.down).to.eql(true);
}); });
expect(task.up).to.eql(true);
expect(task.down).to.eql(true);
}); });
it('cannot create checklists', () => { it('cannot create checklists', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test habit', text: 'test habit',
type: 'habit', type: 'habit',
checklist: [ checklist: [
{_id: 123, completed: false, text: 'checklist'}, {_id: 123, completed: false, text: 'checklist'},
], ],
}).then((task) => {
expect(task).not.to.have.property('checklist');
}); });
expect(task).not.to.have.property('checklist');
}); });
}); });
context('todos', () => { context('todos', () => {
it('creates a todo', () => { it('creates a todo', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test todo', text: 'test todo',
type: 'todo', type: 'todo',
notes: 1976, notes: 1976,
}).then((task) => {
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test todo');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('todo');
}); });
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test todo');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('todo');
}); });
it('creates multiple todos', () => { it('creates multiple todos', async () => {
return user.post('/tasks', [{ let [task, task2] = await user.post('/tasks', [{
text: 'test todo', text: 'test todo',
type: 'todo', type: 'todo',
notes: 1976, notes: 1976,
@@ -195,61 +195,61 @@ describe('POST /tasks', () => {
text: 'test todo 2', text: 'test todo 2',
type: 'todo', type: 'todo',
notes: 1977, notes: 1977,
}]).then(([task, task2]) => { }]);
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test todo');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('todo');
expect(task2.userId).to.equal(user._id); expect(task.userId).to.equal(user._id);
expect(task2.text).to.eql('test todo 2'); expect(task.text).to.eql('test todo');
expect(task2.notes).to.eql('1977'); expect(task.notes).to.eql('1976');
expect(task2.type).to.eql('todo'); expect(task.type).to.eql('todo');
});
expect(task2.userId).to.equal(user._id);
expect(task2.text).to.eql('test todo 2');
expect(task2.notes).to.eql('1977');
expect(task2.type).to.eql('todo');
}); });
it('can create checklists', () => { it('can create checklists', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test todo', text: 'test todo',
type: 'todo', type: 'todo',
checklist: [ checklist: [
{completed: false, text: 'checklist'}, {completed: false, text: 'checklist'},
], ],
}).then((task) => {
expect(task.checklist).to.be.an('array');
expect(task.checklist.length).to.eql(1);
expect(task.checklist[0]).to.be.an('object');
expect(task.checklist[0].text).to.eql('checklist');
expect(task.checklist[0].completed).to.eql(false);
expect(task.checklist[0]._id).to.be.a('string');
}); });
expect(task.checklist).to.be.an('array');
expect(task.checklist.length).to.eql(1);
expect(task.checklist[0]).to.be.an('object');
expect(task.checklist[0].text).to.eql('checklist');
expect(task.checklist[0].completed).to.eql(false);
expect(task.checklist[0]._id).to.be.a('string');
}); });
}); });
context('dailys', () => { context('dailys', () => {
it('creates a daily', () => { it('creates a daily', async () => {
let now = new Date(); let now = new Date();
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
notes: 1976, notes: 1976,
frequency: 'daily', frequency: 'daily',
everyX: 5, everyX: 5,
startDate: now, startDate: now,
}).then((task) => {
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test daily');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('daily');
expect(task.frequency).to.eql('daily');
expect(task.everyX).to.eql(5);
expect(new Date(task.startDate)).to.eql(now);
}); });
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test daily');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('daily');
expect(task.frequency).to.eql('daily');
expect(task.everyX).to.eql(5);
expect(new Date(task.startDate)).to.eql(now);
}); });
it('creates multiple dailys', () => { it('creates multiple dailys', async () => {
return user.post('/tasks', [{ let [task, task2] = await user.post('/tasks', [{
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
notes: 1976, notes: 1976,
@@ -257,40 +257,40 @@ describe('POST /tasks', () => {
text: 'test daily 2', text: 'test daily 2',
type: 'daily', type: 'daily',
notes: 1977, notes: 1977,
}]).then(([task, task2]) => { }]);
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test daily');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('daily');
expect(task2.userId).to.equal(user._id); expect(task.userId).to.equal(user._id);
expect(task2.text).to.eql('test daily 2'); expect(task.text).to.eql('test daily');
expect(task2.notes).to.eql('1977'); expect(task.notes).to.eql('1976');
expect(task2.type).to.eql('daily'); expect(task.type).to.eql('daily');
});
expect(task2.userId).to.equal(user._id);
expect(task2.text).to.eql('test daily 2');
expect(task2.notes).to.eql('1977');
expect(task2.type).to.eql('daily');
}); });
it('defaults to a weekly frequency, with every day set', () => { it('defaults to a weekly frequency, with every day set', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
}).then((task) => { });
expect(task.frequency).to.eql('weekly');
expect(task.everyX).to.eql(1); expect(task.frequency).to.eql('weekly');
expect(task.repeat).to.eql({ expect(task.everyX).to.eql(1);
m: true, expect(task.repeat).to.eql({
t: true, m: true,
w: true, t: true,
th: true, w: true,
f: true, th: true,
s: true, f: true,
su: true, s: true,
}); su: true,
}); });
}); });
it('allows repeat field to be configured', () => { it('allows repeat field to be configured', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
repeat: { repeat: {
@@ -298,66 +298,66 @@ describe('POST /tasks', () => {
w: false, w: false,
su: false, su: false,
}, },
}).then((task) => { });
expect(task.repeat).to.eql({
m: false, expect(task.repeat).to.eql({
t: true, m: false,
w: false, t: true,
th: true, w: false,
f: true, th: true,
s: true, f: true,
su: false, s: true,
}); su: false,
}); });
}); });
it('defaults startDate to today', () => { it('defaults startDate to today', async () => {
let today = (new Date()).getDay(); let today = (new Date()).getDay();
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
}).then((task) => {
expect((new Date(task.startDate)).getDay()).to.eql(today);
}); });
expect((new Date(task.startDate)).getDay()).to.eql(today);
}); });
it('can create checklists', () => { it('can create checklists', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test daily', text: 'test daily',
type: 'daily', type: 'daily',
checklist: [ checklist: [
{completed: false, text: 'checklist'}, {completed: false, text: 'checklist'},
], ],
}).then((task) => {
expect(task.checklist).to.be.an('array');
expect(task.checklist.length).to.eql(1);
expect(task.checklist[0]).to.be.an('object');
expect(task.checklist[0].text).to.eql('checklist');
expect(task.checklist[0].completed).to.eql(false);
expect(task.checklist[0]._id).to.be.a('string');
}); });
expect(task.checklist).to.be.an('array');
expect(task.checklist.length).to.eql(1);
expect(task.checklist[0]).to.be.an('object');
expect(task.checklist[0].text).to.eql('checklist');
expect(task.checklist[0].completed).to.eql(false);
expect(task.checklist[0]._id).to.be.a('string');
}); });
}); });
context('rewards', () => { context('rewards', () => {
it('creates a reward', () => { it('creates a reward', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test reward', text: 'test reward',
type: 'reward', type: 'reward',
notes: 1976, notes: 1976,
value: 10, value: 10,
}).then((task) => {
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test reward');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('reward');
expect(task.value).to.eql(10);
}); });
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test reward');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('reward');
expect(task.value).to.eql(10);
}); });
it('creates multiple rewards', () => { it('creates multiple rewards', async () => {
return user.post('/tasks', [{ let [task, task2] = await user.post('/tasks', [{
text: 'test reward', text: 'test reward',
type: 'reward', type: 'reward',
notes: 1976, notes: 1976,
@@ -367,50 +367,50 @@ describe('POST /tasks', () => {
type: 'reward', type: 'reward',
notes: 1977, notes: 1977,
value: 12, value: 12,
}]).then(([task, task2]) => { }]);
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test reward');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('reward');
expect(task.value).to.eql(11);
expect(task2.userId).to.equal(user._id); expect(task.userId).to.equal(user._id);
expect(task2.text).to.eql('test reward 2'); expect(task.text).to.eql('test reward');
expect(task2.notes).to.eql('1977'); expect(task.notes).to.eql('1976');
expect(task2.type).to.eql('reward'); expect(task.type).to.eql('reward');
expect(task2.value).to.eql(12); expect(task.value).to.eql(11);
});
expect(task2.userId).to.equal(user._id);
expect(task2.text).to.eql('test reward 2');
expect(task2.notes).to.eql('1977');
expect(task2.type).to.eql('reward');
expect(task2.value).to.eql(12);
}); });
it('defaults to a 0 value', () => { it('defaults to a 0 value', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test reward', text: 'test reward',
type: 'reward', type: 'reward',
}).then((task) => {
expect(task.value).to.eql(0);
}); });
expect(task.value).to.eql(0);
}); });
it('requires value to be coerced into a number', () => { it('requires value to be coerced into a number', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test reward', text: 'test reward',
type: 'reward', type: 'reward',
value: '10', value: '10',
}).then((task) => {
expect(task.value).to.eql(10);
}); });
expect(task.value).to.eql(10);
}); });
it('cannot create checklists', () => { it('cannot create checklists', async () => {
return user.post('/tasks', { let task = await user.post('/tasks', {
text: 'test reward', text: 'test reward',
type: 'reward', type: 'reward',
checklist: [ checklist: [
{_id: 123, completed: false, text: 'checklist'}, {_id: 123, completed: false, text: 'checklist'},
], ],
}).then((task) => {
expect(task).not.to.have.property('checklist');
}); });
expect(task).not.to.have.property('checklist');
}); });
}); });
}); });