udemy-node-todo-app/server/tests/server.test.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-11-30 04:15:25 +00:00
const expect = require('expect');
const request = require('supertest');
const {app} = require('./../server');
const {Todo} = require('./../models/todo');
beforeEach((done) => {
Todo.remove({}).then(() => done());
});
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) => {
expect(todos.length).toBe(1);
expect(todos[0].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) => {
expect(todos.length).toBe(0);
done();
}).catch((e) => done(e));
})
2017-11-30 04:15:25 +00:00
});
});