summaryrefslogtreecommitdiffstats
path: root/test/fastdom-test.js
blob: 5dd4ceb64fbe6c9cf530174711a6ea346d765d5f (plain)
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
/*jshint maxlen:false*/
/*global suite, setup, teardown, test, assert, sinon, fastdomSandbox, fastdomPromised*/

suite('fastdom', function() {
  var raf = window.requestAnimationFrame;
  var fastdom;

  setup(function() {
    fastdom = new window.fastdom.constructor();
  });

  test('it runs reads before writes', function(done) {
    var read = sinon.spy(function() {
      assert(!write.called);
    });

    var write = sinon.spy(function() {
      assert(read.called);
      done();
    });

    fastdom.measure(read);
    fastdom.mutate(write);
  });

  test('it calls all reads together, followed by all writes', function(done) {
    var read1 = sinon.spy();
    var read2 = sinon.spy();
    var write1 = sinon.spy();
    var write2 = sinon.spy();

    // Assign unsorted
    fastdom.measure(read1);
    fastdom.mutate(write1);
    fastdom.measure(read2);
    fastdom.mutate(write2);

    // After the queue has been emptied
    // check the callbacks were called
    // in the correct order.
    raf(function() {
      assert(read1.calledBefore(read2));
      assert(read2.calledBefore(write1));
      assert(write1.calledBefore(write2));
      done();
    });
  });

  test('it calls a read in the same frame if scheduled inside a read callback', function(done) {
    var cb = sinon.spy();

    fastdom.measure(function() {

      // Schedule a callback for *next* frame
      raf(cb);

      // Schedule a read callback
      // that should be run in the
      // current frame checking that
      // the RAF callback has not
      // yet been fired.
      fastdom.measure(function() {
        assert(!cb.called);
        done();
      }, this);
    }, this);
  });

  test('it calls a write in the same frame if scheduled inside a read callback', function(done) {
    var cb = sinon.spy();

    fastdom.measure(function() {

      // Schedule a callback for *next* frame
      raf(cb);

      // Schedule a read callback
      // that should be run in the
      // current frame checking that
      // the RAF callback has not
      // yet been fired.
      fastdom.mutate(function() {
        assert(!cb.called);
        done();
      }, this);
    }, this);
  });

  test('it calls a read in the *next* frame if scheduled inside a write callback', function(done) {
    var cb = sinon.spy();

    fastdom.mutate(function() {

      // Schedule a callback for *next* frame
      raf(cb);

      // Schedule a read that should be
      // called in the next frame, meaning
      // the test callback should have already
      // been called.
      fastdom.measure(function() {
        assert(cb.called);
        done();
      }, this);
    }, this);
  });

  test('it does not request a new frame when a write is requested inside a nested read', function(done) {
    var callback = sinon.spy();

    fastdom.mutate(function() {
      fastdom.measure(function() {

        // Schedule a callback for *next* frame
        raf(callback);

        // Schedule a read callback
        // that should be run in the
        // current frame checking that
        // the RAF callback has not
        // yet been fired.
        fastdom.mutate(function() {
          assert(!callback.called);
          done();
        });
      });
    });
  });

  test('it schedules a new frame when a read is requested in a nested write', function(done) {
    fastdom.raf = sinon.spy(fastdom, 'raf');

    fastdom.measure(function() {
      fastdom.mutate(function() {
        fastdom.measure(function(){

          // Should have scheduled a new frame
          assert(fastdom.raf.calledTwice);
          done();
        });
      });
    });
  });

  test('it runs nested reads in the same frame', function(done) {
    sinon.spy(fastdom, 'raf');

    fastdom.measure(function() {
      fastdom.measure(function() {
        fastdom.measure(function() {
          fastdom.measure(function() {

            // Should not have scheduled a new frame
            sinon.assert.calledOnce(fastdom.raf);
            done();
          });
        });
      });
    });
  });

  test('it runs nested writes in the same frame', function(done) {
    fastdom.raf = sinon.spy(fastdom, 'raf');

    fastdom.mutate(function() {
      fastdom.mutate(function() {
        fastdom.mutate(function() {
          fastdom.mutate(function() {

            // Should not have scheduled a new frame
            sinon.assert.calledOnce(fastdom.raf);
            done();
          });
        });
      });
    });
  });

  test('it calls a "read" callback with the given context', function(done) {
    fastdom.measure(function() {
      assert.equal(this.foo, 'bar');
      done();
    }, { foo: 'bar' });
  });

  test('it calls a "write" callback with the given context', function(done) {
    fastdom.mutate(function() {
      assert.equal(this.foo, 'bar');
      done();
    }, { foo: 'bar' });
  });

  test('it has an empty job hash when batch complete', function(done) {
    var ran = 0;

    fastdom.measure(function(){ ran += 1; });
    fastdom.measure(function(){ ran += 2; });
    fastdom.mutate(function(){ ran += 4; });
    fastdom.mutate(function(){ ran += 8; });

    // Check there are four jobs stored
    assert.equal(ran, 0);

    raf(function() {
      assert.equal(ran, 15);
      done();
    });
  });

  test('it maintains correct context if single method is registered twice', function(done) {
    var ctx1 = { foo: 'bar' };
    var ctx2 = { bar: 'baz' };

    function shared() {}

    var spy1 = sinon.spy(shared);
    var spy2 = sinon.spy(shared);

    fastdom.measure(spy1, ctx1);
    fastdom.measure(spy2, ctx2);

    raf(function() {
      assert(spy1.calledOn(ctx1));
      assert(spy2.calledOn(ctx2));
      done();
    });
  });

  test('it runs .catch() handler on error if one has been registered', function(done) {
    fastdom.catch = sinon.spy();

    fastdom.measure(function() { throw 'err1'; });
    fastdom.mutate(function() { throw 'err2'; });

    raf(function() {
      raf(function() {
        assert(fastdom.catch.calledTwice, 'twice');
        assert(fastdom.catch.getCall(0).calledWith('err1'), 'bla');
        assert(fastdom.catch.getCall(1).calledWith('err2'), 'bl2');
        done();
      });
    });
  });

  suite('exceptions', function() {

    // temporarily disable mocha error detection
    setup(function() {
      this.onerror = window.onerror;
      window.onerror = null;
    });

    // re-enable mocha error detection
    teardown(function() {
      window.onerror = this.onerror;
    });

    test('it flushes remaining tasks in next frame if prior task throws', function(done) {
      var spy = sinon.spy();

      fastdom.measure(function() { throw new Error('error'); });
      fastdom.measure(spy);

      raf(function() {
        sinon.assert.notCalled(spy);
        raf(function() {
          sinon.assert.calledOnce(spy);
          done();
        });
      });
    });
  });

  test('it stops rAF loop once frame queue is empty', function(done) {
    var callback = sinon.spy();

    sinon.spy(fastdom, 'raf');
    fastdom.measure(callback);

    raf(function() {
      assert(callback.called);
      assert(fastdom.raf.calledOnce);
      done();
    });
  });

  suite('clear', function() {
    test('it does not run "read" job if cleared (sync)', function(done) {
      var read = sinon.spy();
      var id = fastdom.measure(read);
      fastdom.clear(id);

      raf(function() {
        raf(function() {
        assert(!read.called);
        done();
      });
      });
    });

    test('it fails silently if job not found in queue', function(done) {
      var read = sinon.spy();
      var read2 = sinon.spy();

      var id = fastdom.measure(read);
      fastdom.clear(id);

      raf(function() {
        assert(!read2.called);
        done();
      });
    });

    test('it does not run "write" job if cleared (async)', function(done) {
      var read = sinon.spy();
      var write = sinon.spy();

      var id = fastdom.mutate(write);
      fastdom.measure(function() {
        fastdom.clear(id);

        raf(function() {
          assert(!read.called);
          done();
        });
      });
    });

    test('it does not run "write" job if cleared', function(done) {
      var write = sinon.spy();
      var id = fastdom.mutate(write);

      fastdom.clear(id);

      raf(function() {
        assert(!write.called);
        done();
      });
    });

    test('it removes reference to the job if cleared', function(done) {
      var write = sinon.spy();
      var id = fastdom.mutate(write);

      fastdom.clear(id);

      raf(function() {
        raf(function() {
          raf(function() {
            assert(!write.called);
            done();
          });
        });
      });
    });
  });

  suite('FastDom#extend()', function() {
    test('it has the properties of given object', function() {
      var fastdom2 = fastdom.extend({ prop: 'foo' });
      assert.equal(fastdom2.prop, 'foo');
    });

    test('it can extend an extension', function() {
      var fastdom2 = fastdom.extend({ prop: 'foo' });
      var fastdom3 = fastdom2.extend({ prop: 'bar' });

      assert.equal(fastdom2.prop, 'foo');
      assert.equal(fastdom3.prop, 'bar');

      assert.equal(fastdom2.fastdom, fastdom);
      assert.equal(fastdom3.fastdom, fastdom2);
    });

    test('it throws if argument is not object', function() {
      assert.throws(function() {
        fastdom.extend();
      });

      assert.throws(function() {
        fastdom.extend('oopsie');
      });

      assert.throws(function() {
        fastdom.extend(999);
      });
    });

    test('it only mixes in own properties', function() {
      var proto = { foo: 'foo' };
      var extension = Object.create(proto);
      var extended = fastdom.extend(extension);

      assert.isUndefined(extended.foo);
    });
  });
});