Blame view

node_modules/loopback-datasource-juggler/test/hooks.test.js 11.5 KB
f7563de62   Palak Handa   first commit
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  // Copyright IBM Corp. 2013,2016. All Rights Reserved.
  // Node module: loopback-datasource-juggler
  // This file is licensed under the MIT License.
  // License text available at https://opensource.org/licenses/MIT
  'use strict';
  
  // This test written in mocha+should.js
  var should = require('./init.js');
  
  var j = require('../'),
    Schema = j.Schema,
    AbstractClass = j.AbstractClass,
    Hookable = j.Hookable,
  
    db, User;
  
  describe('hooks', function() {
    before(function(done) {
      db = getSchema();
  
      User = db.define('User', {
        email: {type: String, index: true},
        name: String,
        password: String,
        state: String,
      });
  
      db.automigrate('User', done);
    });
  
    describe('initialize', function() {
      afterEach(function() {
        User.afterInitialize = null;
      });
  
      it('should be triggered on new', function(done) {
        User.afterInitialize = function() {
          done();
        };
        new User;
      });
  
      it('should be triggered on create', function(done) {
        var user;
        User.afterInitialize = function() {
          if (this.name === 'Nickolay') {
            this.name += ' Rozental';
          }
        };
        User.create({name: 'Nickolay'}, function(err, u) {
          u.id.should.be.ok;
          u.name.should.equal('Nickolay Rozental');
          done();
        });
      });
    });
  
    describe('create', function() {
      afterEach(removeHooks('Create'));
  
      it('should be triggered on create', function(done) {
        addHooks('Create', done);
        User.create();
      });
  
      it('should not be triggered on new', function() {
        User.beforeCreate = function(next) {
          should.fail('This should not be called');
          next();
        };
        var u = new User;
      });
  
      it('should be triggered on new+save', function(done) {
        addHooks('Create', done);
        (new User).save();
      });
  
      it('afterCreate should not be triggered on failed create', function(done) {
        var old = User.dataSource.connector.create;
        User.dataSource.connector.create = function(modelName, id, cb) {
          cb(new Error('error'));
        };
  
        User.afterCreate = function() {
          throw new Error('shouldn\'t be called');
        };
        User.create(function(err, user) {
          User.dataSource.connector.create = old;
          done();
        });
      });
  
      it('afterCreate should not be triggered on failed beforeCreate', function(done) {
        User.beforeCreate = function(next, data) {
          // Skip next()
          next(new Error('fail in beforeCreate'));
        };
  
        var old = User.dataSource.connector.create;
        User.dataSource.connector.create = function(modelName, id, cb) {
          throw new Error('shouldn\'t be called');
        };
  
        User.afterCreate = function() {
          throw new Error('shouldn\'t be called');
        };
        User.create(function(err, user) {
          User.dataSource.connector.create = old;
          done();
        });
      });
    });
  
    describe('save', function() {
      afterEach(removeHooks('Save'));
  
      it('should be triggered on create', function(done) {
        addHooks('Save', done);
        User.create();
      });
  
      it('should be triggered on new+save', function(done) {
        addHooks('Save', done);
        (new User).save();
      });
  
      it('should be triggered on updateAttributes', function(done) {
        User.create(function(err, user) {
          addHooks('Save', done);
          user.updateAttributes({name: 'Anatoliy'});
        });
      });
  
      it('should be triggered on save', function(done) {
        User.create(function(err, user) {
          addHooks('Save', done);
          user.name = 'Hamburger';
          user.save();
        });
      });
  
      it('should save full object', function(done) {
        User.create(function(err, user) {
          User.beforeSave = function(next, data) {
            data.should.have.keys('id', 'name', 'email',
              'password', 'state');
            done();
          };
          user.save();
        });
      });
  
      it('should save actual modifications to database', function(done) {
        User.beforeSave = function(next, data) {
          data.password = 'hash';
          next();
        };
        User.destroyAll(function() {
          User.create({
            email: 'james.bond@example.com',
            password: '53cr3t',
          }, function() {
            User.findOne({
              where: {email: 'james.bond@example.com'},
            }, function(err, jb) {
              jb.password.should.equal('hash');
              done();
            });
          });
        });
      });
  
      it('should save actual modifications on updateAttributes', function(done) {
        User.beforeSave = function(next, data) {
          data.password = 'hash';
          next();
        };
        User.destroyAll(function() {
          User.create({
            email: 'james.bond@example.com',
          }, function(err, u) {
            u.updateAttribute('password', 'new password', function(e, u) {
              should.not.exist(e);
              should.exist(u);
              u.password.should.equal('hash');
              User.findOne({
                where: {email: 'james.bond@example.com'},
              }, function(err, jb) {
                jb.password.should.equal('hash');
                done();
              });
            });
          });
        });
      });
  
      it('beforeSave should be able to skip next', function(done) {
        User.create(function(err, user) {
          User.beforeSave = function(next, data) {
            next(null, 'XYZ');
          };
          user.save(function(err, result) {
            result.should.be.eql('XYZ');
            done();
          });
        });
      });
    });
  
    describe('update', function() {
      afterEach(removeHooks('Update'));
  
      it('should not be triggered on create', function() {
        User.beforeUpdate = function(next) {
          should.fail('This should not be called');
          next();
        };
        User.create();
      });
  
      it('should not be triggered on new+save', function() {
        User.beforeUpdate = function(next) {
          should.fail('This should not be called');
          next();
        };
        (new User).save();
      });
  
      it('should be triggered on updateAttributes', function(done) {
        User.create(function(err, user) {
          addHooks('Update', done);
          user.updateAttributes({name: 'Anatoliy'});
        });
      });
  
      it('should be triggered on save', function(done) {
        User.create(function(err, user) {
          addHooks('Update', done);
          user.name = 'Hamburger';
          user.save();
        });
      });
  
      it('should update limited set of fields', function(done) {
        User.create(function(err, user) {
          User.beforeUpdate = function(next, data) {
            data.should.have.keys('name', 'email');
            done();
          };
          user.updateAttributes({name: 1, email: 2});
        });
      });
  
      it('should not trigger after-hook on failed save', function(done) {
        User.afterUpdate = function() {
          should.fail('afterUpdate shouldn\'t be called');
        };
        User.create(function(err, user) {
          var save = User.dataSource.connector.save;
          User.dataSource.connector.save = function(modelName, id, cb) {
            User.dataSource.connector.save = save;
            cb(new Error('Error'));
          };
  
          user.save(function(err) {
            done();
          });
        });
      });
    });
  
    describe('destroy', function() {
      afterEach(removeHooks('Destroy'));
  
      it('should be triggered on destroy', function(done) {
        var hook = 'not called';
        User.beforeDestroy = function(next) {
          hook = 'called';
          next();
        };
        User.afterDestroy = function(next) {
          hook.should.eql('called');
          next();
        };
        User.create(function(err, user) {
          user.destroy(done);
        });
      });
  
      it('should not trigger after-hook on failed destroy', function(done) {
        var destroy = User.dataSource.connector.destroy;
        User.dataSource.connector.destroy = function(modelName, id, cb) {
          cb(new Error('error'));
        };
        User.afterDestroy = function() {
          should.fail('afterDestroy shouldn\'t be called');
        };
        User.create(function(err, user) {
          user.destroy(function(err) {
            User.dataSource.connector.destroy = destroy;
            done();
          });
        });
      });
    });
  
    describe('lifecycle', function() {
      var life = [], user;
      before(function(done) {
        User.beforeSave = function(d) {
          life.push('beforeSave');
          d();
        };
        User.beforeCreate = function(d) {
          life.push('beforeCreate');
          d();
        };
        User.beforeUpdate = function(d) {
          life.push('beforeUpdate');
          d();
        };
        User.beforeDestroy = function(d) {
          life.push('beforeDestroy');
          d();
        };
        User.beforeValidate = function(d) {
          life.push('beforeValidate');
          d();
        };
        User.afterInitialize = function() {
          life.push('afterInitialize');
        };
        User.afterSave = function(d) {
          life.push('afterSave');
          d();
        };
        User.afterCreate = function(d) {
          life.push('afterCreate');
          d();
        };
        User.afterUpdate = function(d) {
          life.push('afterUpdate');
          d();
        };
        User.afterDestroy = function(d) {
          life.push('afterDestroy');
          d();
        };
        User.afterValidate = function(d) {
          life.push('afterValidate');
          d();
        };
        User.create(function(e, u) {
          user = u;
          life = [];
          done();
        });
      });
      beforeEach(function() {
        life = [];
      });
  
      it('should describe create sequence', function(done) {
        User.create(function() {
          life.should.eql([
            'afterInitialize',
            'beforeValidate',
            'afterValidate',
            'beforeCreate',
            'beforeSave',
            'afterSave',
            'afterCreate',
          ]);
          done();
        });
      });
  
      it('should describe new+save sequence', function(done) {
        var u = new User;
        u.save(function() {
          life.should.eql([
            'afterInitialize',
            'beforeValidate',
            'afterValidate',
            'beforeCreate',
            'beforeSave',
            'afterSave',
            'afterCreate',
          ]);
          done();
        });
      });
  
      it('should describe updateAttributes sequence', function(done) {
        user.updateAttributes({name: 'Antony'}, function() {
          life.should.eql([
            'beforeValidate',
            'afterValidate',
            'beforeSave',
            'beforeUpdate',
            'afterUpdate',
            'afterSave',
          ]);
          done();
        });
      });
  
      it('should describe isValid sequence', function(done) {
        should.not.exist(
          user.constructor._validations,
          'Expected user to have no validations, but she have');
        user.isValid(function(valid) {
          valid.should.be.true;
          life.should.eql([
            'beforeValidate',
            'afterValidate',
          ]);
          done();
        });
      });
  
      it('should describe destroy sequence', function(done) {
        user.destroy(function() {
          life.should.eql([
            'beforeDestroy',
            'afterDestroy',
          ]);
          done();
        });
      });
    });
  });
  
  function addHooks(name, done) {
    var called = false, random = String(Math.floor(Math.random() * 1000));
    User['before' + name] = function(next, data) {
      called = true;
      data.email = random;
      next();
    };
    User['after' + name] = function(next) {
      (new Boolean(called)).should.equal(true);
      this.should.have.property('email', random);
      done();
    };
  }
  
  function removeHooks(name) {
    return function() {
      User['after' + name] = null;
      User['before' + name] = null;
    };
  }