summaryrefslogtreecommitdiffstats
path: root/test/fastdom-sandbox-test.js
blob: 43b6626d502f693bb19443c4010b860ec95d1489 (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
/*global suite, setup, test, assert, sinon, fastdomSandbox, fastdomPromised*/
/* jshint maxlen:false */

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

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

  test('It works as normal', function(done) {
    var sandbox = fastdom.sandbox();

    sandbox.measure(function() {
      sandbox.mutate(function() {
        done();
      });
    });
  });

  test('Its possible to clear all sandbox jobs', function(done) {
    var sandbox = fastdom.sandbox();
    var spy = sinon.spy();

    sandbox.measure(spy);
    sandbox.mutate(spy);

    fastdom.measure(function() {
      fastdom.mutate(function() {
        assert.isTrue(spy.notCalled);
        done();
      });
    });

    sandbox.clear();
  });

  test('It clears individual tasks', function(done) {
    var sandbox = fastdom.sandbox();
    var spy = sinon.spy();

    var task = sandbox.measure(spy);
    sandbox.clear(task);
    sandbox.measure(function() {
      assert.isTrue(spy.notCalled);
      done();
    });
  });

  test('it works with fastdom-promised', function(done) {
    var myFastdom = fastdom
      .extend(fastdomPromised)
      .extend(fastdomSandbox);

    var sandbox = myFastdom.sandbox();
    var spy1 = sinon.spy();
    var spy2 = sinon.spy();

    sandbox.measure(spy1)
      .then(function() {
        return sandbox.mutate(spy2);
      })

      .then(function() {
        sinon.assert.calledOnce(spy1);
        sinon.assert.calledOnce(spy2);

        spy1.reset();
        spy2.reset();

        sandbox.measure(spy1);
        sandbox.measure(spy2);
        sandbox.clear();

        raf(function() {
          console.log(3);
          sinon.assert.notCalled(spy1);
          sinon.assert.notCalled(spy2);
          done();
        });
      })

      .catch(done);
  });
});