forked from JohanObrink/mongoose-mock
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (116 loc) · 2.88 KB
/
Copy pathindex.js
File metadata and controls
146 lines (116 loc) · 2.88 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
var sinon = require('sinon');
var events = require('events');
var mongoose = {};
module.exports = mongoose;
// Mongoose-mock emits events
// when Models or Documents are created.
// This allows for the mock injector to get notifications
// about use of the mock and get a chance to access
// the mocked models and document produced.
events.EventEmitter.call(mongoose);
mongoose.__proto__ = events.EventEmitter.prototype; // jshint ignore:line
// ## Schema
var Schema = function () {
function Model(properties) {
var self = this;
if (properties) {
Object.keys(properties).forEach(function (key) {
self[key] = properties[key];
});
}
this.save = sinon.stub();
this.increment = sinon.stub();
this.remove = sinon.stub();
this.save.returns(Promise.resolve(this));
this.increment.returns(Promise.resolve(this));
this.remove.returns(Promise.resolve(this));
mongoose.emit('document', this);
}
Model.statics = {};
Model.methods = {};
Model.static = sinon.stub();
Model.method = sinon.stub();
Model.pre = sinon.stub();
Model.path = sinon.stub();
Model.post = sinon.stub();
Model.exec = sinon.stub();
Model.path.returns({
validate: sinon.stub(),
});
Model.virtual = function () {
function SetterGetter() {
var _set = sinon.stub();
var _get = sinon.stub();
var _ret = { set: _set, get: _set };
_set.returns(_ret);
_get.returns(_ret);
return _ret;
}
return new SetterGetter();
};
[
'aggregate',
'allowDiskUse',
'count',
'create',
'distinct',
'ensureIndexes',
'find',
'findById',
'findByIdAndRemove',
'findByIdAndUpdate',
'findOne',
'findOneAndRemove',
'findOneAndUpdate',
'geoNear',
'geoSearch',
'index',
'limit',
'mapReduce',
'plugin',
'populate',
'remove',
'select',
'set',
'sort',
'update',
'where'
].forEach(function (fn) {
var stub = sinon.stub();
stub.returns(Model);
Model[fn] = stub;
});
mongoose.emit('model', Model);
return Model;
};
// compiled models are stored in models_
// and may be retrieved by name.
var models_ = {};
function createModelFromSchema(name, Type) {
if (!Type) {
return models_[name];
}
if (Type.statics) {
Object.keys(Type.statics).forEach(function (key) {
Type[key] = Type.statics[key];
});
}
if (Type.methods) {
Object.keys(Type.methods).forEach(function (key) {
Type.prototype[key] = Type.methods[key];
});
}
models_[name] = Type;
return models_[name];
}
mongoose.Schema = Schema;
mongoose.Schema.Types = { ObjectId: '' }; // Defining mongoose types as dummies.
mongoose.Types = mongoose.Schema.Types;
mongoose.model = createModelFromSchema;
mongoose.set = sinon.stub();
mongoose.connect = sinon.stub();
mongoose.connection = {
once: sinon.spy(),
on: sinon.spy()
};