-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.test.js
107 lines (99 loc) · 3.09 KB
/
app.test.js
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
var superagent = require('superagent')
var expect = require('expect.js')
var PORT = process.env.PORT || 3000
var BASE = 'http://localhost:'
var COLLECTION = '/collections/test/'
var URL = BASE+PORT+COLLECTION
describe('express rest api server', function(){
var id
it('posts an object', function(done){
superagent.post(URL)
.send({ name: 'John'
, email: '[email protected]'
})
.end(function(e, res){
// console.log(res.body)
expect(e).to.eql(null)
expect(res.body.length).to.eql(1)
expect(res.body[0]._id.length).to.eql(24)
id = res.body[0]._id
expect(res.status).to.eql(201)
done()
})
})
it('retrieves an object', function(done){
superagent.get(URL+id)
.end(function(e, res){
// console.log(res.body)
expect(e).to.eql(null)
expect(typeof res.body).to.eql('object')
expect(res.body._id.length).to.eql(24)
expect(res.body._id).to.eql(id)
expect(res.status).to.eql(200)
done()
})
})
it('retrieves a collection', function(done){
superagent.get(URL)
.end(function(e, res){
// console.log(res.body)
expect(e).to.eql(null)
expect(res.body.length).to.be.above(0)
expect(res.body.map(function (item){return item._id})).to.contain(id)
expect(res.status).to.eql(200)
done()
})
})
it('updates an object', function(done){
superagent.put(URL+id)
.send({name: 'Peter'
, email: '[email protected]'})
.end(function(e, res){
// console.log(res.body)
expect(e).to.eql(null)
expect(typeof res.body).to.eql('object')
//expect(res.body.msg).to.eql('success')
expect(res.body._id.length).to.eql(24)
expect(res.body._id).to.eql(id)
expect(res.body.name).to.eql('Peter')
expect(res.status).to.eql(200)
done()
})
})
// it('checks an updated object', function(done){
// superagent.get(BASE+port+'/collections/test/'+id)
// .end(function(e, res){
// // console.log(res.body)
// expect(e).to.eql(null)
// expect(typeof res.body).to.eql('object')
// expect(res.body._id.length).to.eql(24)
// expect(res.body._id).to.eql(id)
// expect(res.body.name).to.eql('Peter')
// expect(res.status).to.eql(200)
// done()
// })
// })
it('removes an object', function(done){
superagent.del(URL+id)
.end(function(e, res){
expect(e).to.eql(null)
expect(typeof res.body).to.eql('object')
expect(res.status).to.eql(204)
expect(res.noContent).to.be(true)
expect(res.body).to.eql({})
//expect(res.body.msg).to.eql('success')
done()
})
})
it('tries to remove a non-existent object', function(done){
superagent.del(URL+id)
.end(function(e, res){
expect(e).to.eql(null)
expect(typeof res.body).to.eql('object')
expect(res.status).to.eql(404)
expect(res.noContent).to.be(false)
expect(res.body).to.eql({msg: 'Not found'})
done()
})
})
})