udemy-node-todo-app/server/tests/server.test.js
2017-11-29 23:39:28 -05:00

73 lines
1.4 KiB
JavaScript

const expect = require('expect');
const request = require('supertest');
const {app} = require('./../server');
const {Todo} = require('./../models/todo');
var dummy = [ {text: 'One'}, {text: 'Two'}, {text: 'Three'} ];
beforeEach((done) => {
Todo.remove({}).then(() => {
return Todo.insertMany(dummy);
}).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().then((todos) => {
expect(todos.length).toBe(4);
expect(todos[3].text).toBe(text);
done();
}).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(3);
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(3)
})
.end(done);
})
})