-
Notifications
You must be signed in to change notification settings - Fork 10
/
entities-controller_spec.js
96 lines (83 loc) · 2.64 KB
/
entities-controller_spec.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
var request = require('../lib/dupertest'),
entities = require('./entities-controller'),
Entity = require('./entity'),
db = require('./mock-db');
describe('mockController', function() {
var entity;
beforeEach(function() {
// clear database before each test
db.clear();
// create new entity to test against
entity = db.create({name: 'New Cool Entity'});
});
describe('Basic examples using expect', function() {
it('should return an error if the entity does not exist', function(done) {
request(entities.show)
.params({id: 1000})
.expect(404, done);
});
it('should return an entity if one exists', function(done) {
request(entities.show)
.params({id: entity.id})
.expect(entity, done);
});
});
describe('Basic examples using end', function() {
it('should return an error if the entity does not exist', function(done) {
request(entities.show)
.params({id: 1000})
.end(function(response) {
expect(response).toBe(404);
done();
});
});
it('should return an entity if one exists', function(done) {
request(entities.show)
.params({id: entity.id})
.end(function(response) {
expect(response).toEqual(entity);
done();
});
});
});
describe('More complex example building up the request object', function() {
it('should return an entity with the original request url', function(done) {
entities.urlForSomeReason = 'http://localhost:3000/entities';
request(entities.somethingMoreComplex)
.body({entity: entity})
.extendReq({
protocol: 'http',
originalUrl: '/entities',
// very mocked out - we know it will only be used in the context of req.get('host')
get: function() {
return 'localhost:3000';
}
})
.extendRes({
set: function() {}
})
.end(function(response) {
expect(response).toEqual(entity);
done();
});
});
});
describe('Transforming the request before it is sent', function() {
it('should allow the request to be dynamically modified', function(done) {
function action(req, res) {
var incrementedId = req.params.incrementedId;
res.send(incrementedId);
}
request(action)
.params({id: entity.id})
.beforeSend(function() {
var params = this.req.params;
params.incrementedId = params.id + 1;
})
.end(function(response) {
expect(response).toBe(entity.id + 1);
done();
});
});
});
});