@@ -4,7 +4,8 @@ var chai = require('chai'),
4
4
async = require ( 'async' ) ,
5
5
mongoose = require ( 'mongoose' ) ,
6
6
Schema = mongoose . Schema ,
7
- AutoIncrement = require ( '../index' ) ;
7
+ AutoIncrement = require ( '../index' ) ,
8
+ sinon = require ( 'sinon' ) ;
8
9
9
10
describe ( 'Basic => ' , function ( ) {
10
11
@@ -191,6 +192,76 @@ describe('Basic => ', function() {
191
192
192
193
} ) ;
193
194
195
+ describe ( 'hook' , function ( ) {
196
+ before ( function ( done ) {
197
+ var SimpleFieldSchema = new Schema ( {
198
+ id : Number ,
199
+ val : String ,
200
+ tag : String
201
+ } ) ;
202
+ var wrapper = function ( schema , options ) {
203
+ var instance = AutoIncrement ( schema , options ) ;
204
+ this . setNextCounterSpy = sinon . spy ( instance , '_setNextCounter' ) ;
205
+ return instance ;
206
+ } . bind ( this ) ;
207
+ SimpleFieldSchema . plugin ( wrapper , { id : 'id_hook_test' , inc_field : 'id' } ) ;
208
+ this . SimpleField = mongoose . model ( 'SimpleFieldHookTest' , SimpleFieldSchema ) ;
209
+ this . SimpleField . create ( { val : 'existing' } , function ( err ) {
210
+ this . setNextCounterSpy . reset ( ) ;
211
+ done ( err ) ;
212
+ } . bind ( this ) ) ;
213
+ } ) ;
214
+
215
+ afterEach ( function ( ) {
216
+ this . setNextCounterSpy . reset ( ) ;
217
+ } ) ;
218
+
219
+ it ( 'is called when saving a new document' , function ( done ) {
220
+ var t = new this . SimpleField ( { val : 'a' } ) ;
221
+ t . save ( function ( err ) {
222
+ sinon . assert . calledOnce ( this . setNextCounterSpy ) ;
223
+ done ( err ) ;
224
+ } . bind ( this ) ) ;
225
+ } ) ;
226
+
227
+ it ( 'is not called when saving an existing document' , function ( done ) {
228
+ var t = new this . SimpleField ( { val : 'a' } ) ;
229
+ t . isNew = false ;
230
+ t . save ( function ( err ) {
231
+ sinon . assert . notCalled ( this . setNextCounterSpy ) ;
232
+ done ( err ) ;
233
+ } . bind ( this ) ) ;
234
+ } ) ;
235
+
236
+ it ( 'is called when upserting in an update and result in an insert' , function ( done ) {
237
+ this . SimpleField . update ( { val : '1234' } , { tag : 'nothing' } , { upsert : true } , function ( err , doc ) {
238
+ sinon . assert . calledOnce ( this . setNextCounterSpy ) ;
239
+ done ( err ) ;
240
+ } . bind ( this ) ) ;
241
+ } ) ;
242
+
243
+ it ( 'is not called when upserting in an update and not result in an insert' , function ( done ) {
244
+ this . SimpleField . update ( { val : 'existing' } , { tag : 'update' } , { upsert : true } , function ( err , doc ) {
245
+ sinon . assert . notCalled ( this . setNextCounterSpy ) ;
246
+ done ( err ) ;
247
+ } . bind ( this ) ) ;
248
+ } ) ;
249
+
250
+ it ( 'is called when upserting in an findOneAndUpdate and result in an insert' , function ( done ) {
251
+ this . SimpleField . findOneAndUpdate ( { val : '4567' } , { tag : 'nothing' } , { upsert : true } , function ( err , doc ) {
252
+ sinon . assert . calledOnce ( this . setNextCounterSpy ) ;
253
+ done ( err ) ;
254
+ } . bind ( this ) ) ;
255
+ } ) ;
256
+
257
+ it ( 'is not called when upserting in an findOneAndUpdate and not result in an insert' , function ( done ) {
258
+ this . SimpleField . findOneAndUpdate ( { val : '1234' } , { tag : 'findOneAndUpdate' } , { upsert : true } , function ( err , doc ) {
259
+ sinon . assert . notCalled ( this . setNextCounterSpy ) ;
260
+ done ( err ) ;
261
+ } . bind ( this ) ) ;
262
+ } ) ;
263
+ } ) ;
264
+
194
265
describe ( 'a manual increment field => ' , function ( ) {
195
266
196
267
before ( function ( done ) {
@@ -305,7 +376,7 @@ describe('Basic => ', function() {
305
376
assert . throws ( function ( ) {
306
377
UnusedSchema . plugin ( AutoIncrement , { inc_field : 'inhabitant' , reference_fields : [ 'country' , 'city' ] , disable_hooks : true } ) ;
307
378
} , Error ) ;
308
-
379
+
309
380
} ) ;
310
381
} ) ;
311
382
@@ -342,7 +413,7 @@ describe('Basic => ', function() {
342
413
343
414
} ) ;
344
415
345
- } ) ;
416
+ } ) ;
346
417
347
418
} ) ;
348
419
} ) ;
0 commit comments