This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
97 lines (84 loc) · 3.13 KB
/
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
'use strict';
var should = require('should'),
sinon = require('sinon'),
reflekt = require('reflekt'),
Service = require('./service');
describe('MongoService', function() {
beforeEach(function() {
var self = this;
this.toArray = sinon.spy(function(callback) {
callback(null, { dummy: 'yup' });
});
this.find = sinon.spy(function() {
return {
toArray: self.toArray
};
});
this.collection = { find: this.find };
this.$mongo = {
collection: sinon.spy(function(name) {
self.name = name;
return self.collection;
})
};
this.$resolver = sinon.spy(new reflekt.ObjectResolver({ bar: 'bar', $body: '$body' }));
this.$$resolver = sinon.spy(new reflekt.ObjectResolver({ $mongo: this.$mongo }));
});
it('should use the defined collection', function(done) {
var self = this;
handler({ collection: 'foo' })(null, this.$caller, this.$$resolver, this.$resolver, function() {
self.name.should.equal('foo');
done();
});
});
it('should not run the query if an error is defined in the resolver', function(done) {
var self = this;
handler({ collection: 'foo' })({}, this.$caller, this.$$resolver, this.$resolver, function() {
self.$mongo.collection.called.should.equal(false);
done();
});
});
it('should use the defined resource', function(done) {
var toArray = sinon.spy(function(callback) {
callback();
}),
collection = {
find: sinon.spy(function() {
return {
toArray: toArray
};
})
},
$mongo = {
collection: sinon.spy(function() {
return collection;
})
};
handler({ resource: $mongo })(null, this.$caller, this.$$resolver, this.$resolver, function() {
$mongo.collection.called.should.equal(true);
done();
});
});
it('should use `$mongo` if no resource is defined', function(done) {
var self = this;
handler({})(null, this.$caller, this.$$resolver, this.$resolver, function() {
self.$mongo.collection.called.should.equal(true);
done();
});
});
it('should resolve the resource if it is defined as a string', function(done) {
var self = this;
handler({ resource: '$mongo' })(null, this.$caller, this.$$resolver, this.$resolver, function() {
self.$$resolver.called.should.equal(true);
self.$$resolver.calledWith('$mongo').should.equal(true);
done();
});
});
it('should inject the results if no error occurred and inject is defined', function(done) {
var self = this;
handler({ inject: 'ducks' })(null, this.$caller, this.$$resolver, this.$resolver, function() {
should(self.$resolver('ducks')).eql({ dummy: 'yup' });
done();
});
});
});