-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathstatics.js
More file actions
126 lines (105 loc) · 2.87 KB
/
statics.js
File metadata and controls
126 lines (105 loc) · 2.87 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
try {
var model = require('model');
} catch (e) {
var model = require('..');
}
var assert = require('assert');
var User = model('User')
.attr('id', { type: 'number' })
.attr('name', { type: 'string' })
.attr('age', { type: 'number' })
.headers({'X-API-TOKEN': 'token string'})
describe('Model.url()', function(){
it('should return the base url', function(){
assert('/users' == User.url());
})
})
describe('Model.url(string)', function(){
it('should join', function(){
assert('/users/edit' == User.url('edit'));
})
})
describe('Model.attrs', function(){
it('should hold the defined attrs', function(){
assert('string' == User.attrs.name.type);
assert('number' == User.attrs.age.type);
})
})
describe('Model.all(fn)', function(){
beforeEach(function(done){
User.destroyAll(done);
});
beforeEach(function(done){
var tobi = new User({ name: 'tobi', age: 2 });
var loki = new User({ name: 'loki', age: 1 });
var jane = new User({ name: 'jane', age: 8 });
tobi.save(function(){
loki.save(function(){
jane.save(done);
});
});
})
it('should respond with a collection of all', function(done){
User.all(function(err, users, res){
assert(!err);
assert(res);
assert(res.req.header['X-API-TOKEN'] == 'token string')
assert(3 == users.length());
assert('tobi' == users.at(0).name());
assert('loki' == users.at(1).name());
assert('jane' == users.at(2).name());
done();
});
})
})
describe('Model.route(string)', function(){
it('should set the base path for url', function(){
User.route('/api/u');
assert('/api/u/edit' == User.url('edit'));
})
})
describe('Model.toError()', function() {
var Car = model('Car')
.attr('id')
.attr('color')
.toError(function(err, res) {
return {error: err, response: res};
});
var _request_get = Car.request.get;
before( function(done) {
Car.request.get = function(url, data, fn) {
var req = _request_get(url, data, fn);
req.timeout(1);
return req;
};
done();
});
after( function(done) {
Car.request.get = _request_get;
done();
});
it('should be called on errors (e.g. request timeout)', function(done) {
Car.all(function(err, res) {
assert(err.error instanceof Error);
assert(-1 != err.error.message.indexOf('timeout'));
done();
});
});
it('should be inherited by model instances', function(done) {
var car = new Car({color: 'silver'});
car.save(function(err) {
assert(500 == err.response.status);
done();
});
});
it('should use the default error handler if not called', function(done) {
var Car = model('Car')
.attr('id')
.attr('color');
Car.all(function(err) {
assert(err instanceof Error);
assert(-1 != err.message.indexOf('timeout'));
done();
});
});
});