1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| const {app,Todo} = require('../postman') const {ObjectID} = require('mongodb'); const expect = require('expect') const request = require('supertest')
const todos = [{ _id: new ObjectID(), text: 'First test todo' }, { _id: new ObjectID(), text: 'Second test todo' }];
beforeEach((done) => { Todo.remove({}).then(() => { return Todo.insertMany(todos); }).then(() => done()); });
describe('POST /todos', () => { it('should create a new todo', (done) => { var text = 'Test todo text';
request(app) .post('/todos') .send({text}) .expect(200) .expect((res) => { expect(res.body.text).toBe(text); }) .end((err, res) => { if (err) { return done(err); }
Todo.find({text}).then((todos) => { expect(todos.length).toBe(1); expect(todos[0].text).toBe(text); done(); }).catch((e) => done(e)); }); });
it('should not create todo with invalid body data', (done) => { request(app) .post('/todos') .send({}) .expect(400) .end((err, res) => { if (err) { return done(err); }
Todo.find().then((todos) => { expect(todos.length).toBe(2); done(); }).catch((e) => done(e)); }); }); });
describe('GET /todos', () => { it('should get all todos', (done) => { request(app) .get('/todos') .expect(200) .expect((res) => { expect(res.body.todos.length).toBe(2); }) .end(done); }); });
describe('GET /todos/:id', () => { it('should return todo doc', (done) => { request(app) .get(`/todos/${todos[0]._id.toHexString()}`) .expect(200) .expect((res) => { expect(res.body.todo.text).toBe(todos[0].text); }) .end(done); });
it('should return 404 if todo not found', (done) => { var hexId = new ObjectID().toHexString();
request(app) .get(`/todos/${hexId}`) .expect(404) .end(done); });
it('should return 404 for non-object ids', (done) => { request(app) .get('/todos/123abc') .expect(404) .end(done); }); });
describe('DELETE /todos/:id', () => { it('should remove a todo', (done) => { var hexId = todos[1]._id.toHexString();
request(app) .delete(`/todos/${hexId}`) .expect(200) .expect((res) => { expect(res.body.todo._id).toBe(hexId); }) .end((err, res) => { if (err) { return done(err); }
Todo.findById(hexId).then((todo) => { expect(todo).toBeFalsy(); done(); }).catch((e) => done(e)); }); });
it('should return 404 if todo not found', (done) => { var hexId = new ObjectID().toHexString();
request(app) .delete(`/todos/${hexId}`) .expect(404) .end(done); });
it('should return 404 if object id is invalid', (done) => { request(app) .delete('/todos/123abc') .expect(404) .end(done); }); });
|