2017-11-30 04:15:25 +00:00
|
|
|
const expect = require('expect');
|
|
|
|
const request = require('supertest');
|
2017-12-01 03:33:22 +00:00
|
|
|
const {ObjectID} = require('mongodb');
|
2017-11-30 04:15:25 +00:00
|
|
|
|
|
|
|
const {app} = require('./../server');
|
|
|
|
const {Todo} = require('./../models/todo');
|
|
|
|
|
2017-12-01 03:33:22 +00:00
|
|
|
var dummy = [
|
|
|
|
{_id: new ObjectID(), text: 'One'},
|
2017-12-01 05:07:56 +00:00
|
|
|
{_id: new ObjectID(), text: 'Two', completed: true, completedAt: 333},
|
2017-12-01 03:33:22 +00:00
|
|
|
{_id: new ObjectID(), text: 'Three'} ];
|
2017-11-30 04:39:28 +00:00
|
|
|
|
|
|
|
|
2017-11-30 04:15:25 +00:00
|
|
|
beforeEach((done) => {
|
2017-11-30 04:39:28 +00:00
|
|
|
Todo.remove({}).then(() => {
|
|
|
|
return Todo.insertMany(dummy);
|
|
|
|
}).then(() => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2017-11-30 04:15:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /todos', () => {
|
|
|
|
it('should create a new todo', (done) => {
|
2017-11-30 04:18:47 +00:00
|
|
|
var text = 'Test todo text';
|
|
|
|
|
2017-11-30 04:15:25 +00:00
|
|
|
request(app)
|
|
|
|
.post('/todos')
|
|
|
|
.send({text})
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.text).toBe(text);
|
|
|
|
})
|
|
|
|
.end((err, res) => {
|
2017-11-30 04:18:47 +00:00
|
|
|
if (err) {
|
2017-11-30 04:15:25 +00:00
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
Todo.find().then((todos) => {
|
2017-12-01 05:07:56 +00:00
|
|
|
expect(todos.length).toBe(dummy.length + 1);
|
|
|
|
expect(todos[dummy.length].text).toBe(text);
|
2017-11-30 04:18:47 +00:00
|
|
|
done();
|
2017-11-30 04:24:51 +00:00
|
|
|
}).catch((e) => done(e));
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT create a new todo', (done) => {
|
|
|
|
request(app)
|
|
|
|
.post('/todos')
|
|
|
|
.send()
|
|
|
|
.expect(400)
|
|
|
|
.end((err, res) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
Todo.find().then((todos) => {
|
2017-11-30 04:39:28 +00:00
|
|
|
expect(todos.length).toBe(3);
|
2017-11-30 04:24:51 +00:00
|
|
|
done();
|
|
|
|
}).catch((e) => done(e));
|
|
|
|
})
|
2017-11-30 04:15:25 +00:00
|
|
|
});
|
|
|
|
});
|
2017-11-30 04:39:28 +00:00
|
|
|
|
|
|
|
describe("GET /todos", () => {
|
|
|
|
it('should get all TODOS', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/todos')
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.todos.length).toBe(3)
|
|
|
|
|
|
|
|
})
|
|
|
|
.end(done);
|
|
|
|
})
|
|
|
|
})
|
2017-12-01 03:33:22 +00:00
|
|
|
|
|
|
|
describe("GET /todos/:id", () => {
|
|
|
|
it("should get a single todo", (done) => {
|
|
|
|
request(app)
|
|
|
|
.get(`/todos/${dummy[0]._id.toHexString()}`)
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.todo.text).toBe(dummy[0].text)
|
|
|
|
})
|
|
|
|
.end(done)
|
|
|
|
})
|
|
|
|
it("should return a 404 if todo not found", (done) => {
|
|
|
|
request(app)
|
|
|
|
.get(`/todos/${new ObjectID()}`)
|
|
|
|
.expect(404)
|
|
|
|
.end(done)
|
|
|
|
})
|
|
|
|
it("should return a 404 if id is bad", (done) => {
|
|
|
|
request(app)
|
|
|
|
.get(`/todos/${dummy[0]._id.toHexString()}11111`)
|
|
|
|
.expect(404)
|
|
|
|
.end(done)
|
|
|
|
})
|
|
|
|
});
|
2017-12-01 04:33:22 +00:00
|
|
|
|
|
|
|
describe("DELETE /todos/:id", () => {
|
|
|
|
it("should delete a single todo", (done) => {
|
|
|
|
request(app)
|
|
|
|
.delete(`/todos/${dummy[0]._id.toHexString()}`)
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.todo.text).toBe(dummy[0].text)
|
|
|
|
})
|
2017-12-01 04:40:34 +00:00
|
|
|
.end((err, res) => {
|
|
|
|
if(err){
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
Todo.findById(dummy[0]._id.toHexString()).then((todo) => {
|
|
|
|
expect(todo).toNotExist();
|
|
|
|
done();
|
|
|
|
}).catch((e) => done(e));
|
|
|
|
});
|
2017-12-01 04:33:22 +00:00
|
|
|
});
|
|
|
|
it("should return a 404 if todo not found", (done) => {
|
|
|
|
request(app)
|
|
|
|
.delete(`/todos/${new ObjectID()}`)
|
|
|
|
.expect(404)
|
|
|
|
.end(done)
|
|
|
|
});
|
|
|
|
it("should return a 404 if id is bad", (done) => {
|
|
|
|
request(app)
|
2017-12-01 04:40:34 +00:00
|
|
|
.delete(`/todos/123`)
|
2017-12-01 04:33:22 +00:00
|
|
|
.expect(404)
|
|
|
|
.end(done)
|
|
|
|
});
|
|
|
|
});
|
2017-12-01 05:07:56 +00:00
|
|
|
|
|
|
|
describe("PATCH /todos/:id", () => {
|
|
|
|
it("should update a single todo", (done) => {
|
|
|
|
var newText = "butts";
|
|
|
|
request(app)
|
|
|
|
.patch(`/todos/${dummy[0]._id.toHexString()}`)
|
|
|
|
.send({text: newText , completed: true})
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.todo.text).toBe(newText);
|
|
|
|
expect(res.body.todo.completedAt).toBeA('number');
|
|
|
|
})
|
|
|
|
.end(done)
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should clear completed at when todo is not completed", (done) => {
|
|
|
|
request(app)
|
|
|
|
.patch(`/todos/${dummy[1]._id.toHexString()}`)
|
|
|
|
.send({completed: false})
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.todo.completedAt).toBe(null);
|
|
|
|
expect(res.body.todo.completed).toBe(false);
|
|
|
|
})
|
|
|
|
.end(done)
|
|
|
|
});
|
|
|
|
});
|