Blame view

node_modules/loopback-datasource-juggler/test/async-observer.test.js 9.63 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
  // Copyright IBM Corp. 2015,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';
  
  var ModelBuilder = require('../').ModelBuilder;
  var should = require('./init');
  
  describe('async observer', function() {
    var TestModel;
    beforeEach(function defineTestModel() {
      var modelBuilder = new ModelBuilder();
      TestModel = modelBuilder.define('TestModel', {name: String});
    });
  
    it('calls registered async observers', function(done) {
      var notifications = [];
      TestModel.observe('before', pushAndNext(notifications, 'before'));
      TestModel.observe('after', pushAndNext(notifications, 'after'));
  
      TestModel.notifyObserversOf('before', {}, function(err) {
        if (err) return done(err);
        notifications.push('call');
        TestModel.notifyObserversOf('after', {}, function(err) {
          if (err) return done(err);
  
          notifications.should.eql(['before', 'call', 'after']);
          done();
        });
      });
    });
  
    it('allows multiple observers for the same operation', function(done) {
      var notifications = [];
      TestModel.observe('event', pushAndNext(notifications, 'one'));
      TestModel.observe('event', pushAndNext(notifications, 'two'));
  
      TestModel.notifyObserversOf('event', {}, function(err) {
        if (err) return done(err);
        notifications.should.eql(['one', 'two']);
        done();
      });
    });
  
    it('allows multiple operations to be notified in one call', function(done) {
      var notifications = [];
      TestModel.observe('event1', pushAndNext(notifications, 'one'));
      TestModel.observe('event2', pushAndNext(notifications, 'two'));
  
      TestModel.notifyObserversOf(['event1', 'event2'], {}, function(err) {
        if (err) return done(err);
        notifications.should.eql(['one', 'two']);
        done();
      });
    });
  
    it('inherits observers from base model', function(done) {
      var notifications = [];
      TestModel.observe('event', pushAndNext(notifications, 'base'));
  
      var Child = TestModel.extend('Child');
      Child.observe('event', pushAndNext(notifications, 'child'));
  
      Child.notifyObserversOf('event', {}, function(err) {
        if (err) return done(err);
        notifications.should.eql(['base', 'child']);
        done();
      });
    });
  
    it('allow multiple operations to be notified with base models', function(done) {
      var notifications = [];
      TestModel.observe('event1', pushAndNext(notifications, 'base1'));
      TestModel.observe('event2', pushAndNext(notifications, 'base2'));
  
      var Child = TestModel.extend('Child');
      Child.observe('event1', pushAndNext(notifications, 'child1'));
      Child.observe('event2', pushAndNext(notifications, 'child2'));
  
      Child.notifyObserversOf(['event1', 'event2'], {}, function(err) {
        if (err) return done(err);
        notifications.should.eql(['base1', 'child1', 'base2', 'child2']);
        done();
      });
    });
  
    it('does not modify observers in the base model', function(done) {
      var notifications = [];
      TestModel.observe('event', pushAndNext(notifications, 'base'));
  
      var Child = TestModel.extend('Child');
      Child.observe('event', pushAndNext(notifications, 'child'));
  
      TestModel.notifyObserversOf('event', {}, function(err) {
        if (err) return done(err);
        notifications.should.eql(['base']);
        done();
      });
    });
  
    it('always calls inherited observers', function(done) {
      var notifications = [];
      TestModel.observe('event', pushAndNext(notifications, 'base'));
  
      var Child = TestModel.extend('Child');
      // Important: there are no observers on the Child model
  
      Child.notifyObserversOf('event', {}, function(err) {
        if (err) return done(err);
        notifications.should.eql(['base']);
        done();
      });
    });
  
    it('can remove observers', function(done) {
      var notifications = [];
  
      function call(ctx, next) {
        notifications.push('call');
        process.nextTick(next);
      };
  
      TestModel.observe('event', call);
      TestModel.removeObserver('event', call);
  
      TestModel.notifyObserversOf('event', {}, function(err) {
        if (err) return done(err);
        notifications.should.eql([]);
        done();
      });
    });
  
    it('can clear all observers', function(done) {
      var notifications = [];
  
      function call(ctx, next) {
        notifications.push('call');
        process.nextTick(next);
      };
  
      TestModel.observe('event', call);
      TestModel.observe('event', call);
      TestModel.observe('event', call);
      TestModel.clearObservers('event');
  
      TestModel.notifyObserversOf('event', {}, function(err) {
        if (err) return done(err);
        notifications.should.eql([]);
        done();
      });
    });
  
    it('handles no observers', function(done) {
      TestModel.notifyObserversOf('no-observers', {}, function(err) {
        // the test passes when no error was raised
        done(err);
      });
    });
  
    it('passes context to final callback', function(done) {
      var context = {};
      TestModel.notifyObserversOf('event', context, function(err, ctx) {
        (ctx || 'null').should.equal(context);
        done();
      });
    });
  
    describe('notifyObserversAround', function() {
      var notifications;
      beforeEach(function() {
        notifications = [];
        TestModel.observe('before execute',
          pushAndNext(notifications, 'before execute'));
        TestModel.observe('after execute',
          pushAndNext(notifications, 'after execute'));
      });
  
      it('should notify before/after observers', function(done) {
        var context = {};
  
        function work(done) {
          process.nextTick(function() {
            done(null, 1);
          });
        }
  
        TestModel.notifyObserversAround('execute', context, work,
          function(err, result) {
            notifications.should.eql(['before execute', 'after execute']);
            result.should.eql(1);
            done();
          });
      });
  
      it('should allow work with context', function(done) {
        var context = {};
  
        function work(context, done) {
          process.nextTick(function() {
            done(null, 1);
          });
        }
  
        TestModel.notifyObserversAround('execute', context, work,
          function(err, result) {
            notifications.should.eql(['before execute', 'after execute']);
            result.should.eql(1);
            done();
          });
      });
  
      it('should notify before/after observers with multiple results',
        function(done) {
          var context = {};
  
          function work(done) {
            process.nextTick(function() {
              done(null, 1, 2);
            });
          }
  
          TestModel.notifyObserversAround('execute', context, work,
            function(err, r1, r2) {
              r1.should.eql(1);
              r2.should.eql(2);
              notifications.should.eql(['before execute', 'after execute']);
              done();
            });
        });
  
      it('should allow observers to skip other ones',
        function(done) {
          TestModel.observe('before invoke',
            function(context, next) {
              notifications.push('before invoke');
              context.end(null, 0);
            });
          TestModel.observe('after invoke',
            pushAndNext(notifications, 'after invoke'));
  
          var context = {};
  
          function work(done) {
            process.nextTick(function() {
              done(null, 1, 2);
            });
          }
  
          TestModel.notifyObserversAround('invoke', context, work,
            function(err, r1) {
              r1.should.eql(0);
              notifications.should.eql(['before invoke']);
              done();
            });
        });
  
      it('should allow observers to tweak results',
        function(done) {
          TestModel.observe('after invoke',
            function(context, next) {
              notifications.push('after invoke');
              context.results = [3];
              next();
            });
  
          var context = {};
  
          function work(done) {
            process.nextTick(function() {
              done(null, 1, 2);
            });
          }
  
          TestModel.notifyObserversAround('invoke', context, work,
            function(err, r1) {
              r1.should.eql(3);
              notifications.should.eql(['after invoke']);
              done();
            });
        });
    });
  
    it('resolves promises returned by observers', function(done) {
      TestModel.observe('event', function(ctx) {
        return Promise.resolve('value-to-ignore');
      });
      TestModel.notifyObserversOf('event', {}, function(err, ctx) {
        // the test times out when the promises are not supported
        done();
      });
    });
  
    it('handles rejected promise returned by an observer', function(done) {
      var testError = new Error('expected test error');
      TestModel.observe('event', function(ctx) {
        return Promise.reject(testError);
      });
      TestModel.notifyObserversOf('event', {}, function(err, ctx) {
        err.should.eql(testError);
        done();
      });
    });
  
    it('returns a promise when no callback is provided', function() {
      var context = {value: 'a-test-context'};
      var p = TestModel.notifyObserversOf('event', context);
      (p !== undefined).should.be.true;
      return p.then(function(result) {
        result.should.eql(context);
      });
    });
  
    it('returns a rejected promise when no callback is provided', function() {
      var testError = new Error('expected test error');
      TestModel.observe('event', function(ctx, next) { next(testError); });
      var p = TestModel.notifyObserversOf('event', context);
      return p.then(
        function(result) {
          throw new Error('The promise should have been rejected.');
        },
        function(err) {
          err.should.eql(testError);
        });
    });
  });
  
  function pushAndNext(array, value) {
    return function(ctx, next) {
      array.push(value);
      process.nextTick(next);
    };
  }