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
import * as chai from 'chai';
import * as chaiHttp from 'chai-http';
process.env.NODE_ENV = 'test';
import { app } from '../app';
import Cat from '../models/cat';
const should = chai.use(chaiHttp).should();
describe('Cats', () => {
beforeEach(done => {
Cat.remove({}, err => {
done();
});
});
describe('Backend tests for cats', () => {
it('should get all the cats', done => {
chai.request(app)
.get('/api/cats')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
res.body.length.should.be.eql(0);
done();
});
});
it('should get cats count', done => {
chai.request(app)
.get('/api/cats/count')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('number');
res.body.should.be.eql(0);
done();
});
});
it('should create new cat', done => {
const cat = new Cat({ name: 'Fluffy', weight: 4, age: 2 });
chai.request(app)
.post('/api/cat')
.send(cat)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.a.property('name');
res.body.should.have.a.property('weight');
res.body.should.have.a.property('age');
done();
});
});
it('should get a cat by its id', done => {
const cat = new Cat({ name: 'Cat', weight: 2, age: 4 });
cat.save((error, newCat) => {
chai.request(app)
.get(`/api/cat/${newCat.id}`)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('name');
res.body.should.have.property('weight');
res.body.should.have.property('age');
res.body.should.have.property('_id').eql(newCat.id);
done();
});
});
});
it('should update a cat by its id', done => {
const cat = new Cat({ name: 'Cat', weight: 2, age: 4 });
cat.save((error, newCat) => {
chai.request(app)
.put(`/api/cat/${newCat.id}`)
.send({ weight: 5 })
.end((err, res) => {
res.should.have.status(200);
done();
});
});
});
it('should delete a cat by its id', done => {
const cat = new Cat({ name: 'Cat', weight: 2, age: 4 });
cat.save((error, newCat) => {
chai.request(app)
.delete(`/api/cat/${newCat.id}`)
.end((err, res) => {
res.should.have.status(200);
done();
});
});
});
});
});