~bac/juju-gui/trunkcopy

« back to all changes in this revision

Viewing changes to lib/d3/test/core/transition-test-each.js

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require("../env");
2
 
 
3
 
var assert = require("assert");
4
 
 
5
 
module.exports = {
6
 
  "start": {
7
 
    topic: function() {
8
 
      var cb = this.callback,
9
 
          div = d3.select("body").html("").selectAll().data(["foo", "bar"]).enter().append("div").attr("class", String),
10
 
          transition = div.transition().delay(150),
11
 
          then = Date.now(),
12
 
          n = 0,
13
 
          calls = [],
14
 
          context = [],
15
 
          data = [],
16
 
          index = [],
17
 
          count = [],
18
 
          delay = [];
19
 
 
20
 
      // A callback to verify that multiple callbacks are allowed.
21
 
      transition.each("start.other", function() {
22
 
        ++n;
23
 
      });
24
 
 
25
 
      // A callback which captures arguments and context.
26
 
      transition.each("start", function(d, i) {
27
 
        context.push(this);
28
 
        data.push(d);
29
 
        index.push(i);
30
 
        count.push(++n);
31
 
        delay.push(Date.now() - then);
32
 
        if (n >= 4) cb(null, {
33
 
          selection: div,
34
 
          delay: delay,
35
 
          context: context,
36
 
          data: data,
37
 
          index: index,
38
 
          count: count,
39
 
          id: transition.id
40
 
        });
41
 
      });
42
 
    },
43
 
 
44
 
    "invokes the listener after the specified delay": function(result) {
45
 
      assert.inDelta(result.delay, [150, 150], 20);
46
 
    },
47
 
    "invokes each listener exactly once, in order": function(result) {
48
 
      assert.deepEqual(result.count, [2, 4]);
49
 
    },
50
 
 
51
 
    // For the same node, listeners will be called back in order, according to
52
 
    // the implementation of d3.dispatch. However, the order of callbacks across
53
 
    // nodes is not guaranteed; currently, callbacks are in reverse order if
54
 
    // they share the same delay, because of the timer queue. I suppose it'd be
55
 
    // slightly better if the callbacks were invoked in node order (consistent
56
 
    // with selections), but since these callbacks happen asynchronously I don't
57
 
    // think the API needs to guarantee the order of callbacks.
58
 
 
59
 
    "uses the node as the context": function(result) {
60
 
      assert.domEqual(result.context[1], result.selection[0][0]);
61
 
      assert.domEqual(result.context[0], result.selection[0][1]);
62
 
    },
63
 
    "passes the data and index to the function": function(result) {
64
 
      assert.deepEqual(result.data, ["bar", "foo"], "expected data, got {actual}");
65
 
      assert.deepEqual(result.index, [1, 0], "expected index, got {actual}");
66
 
    },
67
 
 
68
 
    "sets an exclusive lock on transitioning nodes": function(result) {
69
 
      var id = result.id;
70
 
      assert.isTrue(id > 0);
71
 
      assert.equal(result.selection[0][0].__transition__.count, 1);
72
 
      assert.equal(result.selection[0][1].__transition__.count, 1);
73
 
      assert.equal(result.selection[0][0].__transition__.active, id);
74
 
      assert.equal(result.selection[0][1].__transition__.active, id);
75
 
    }
76
 
  },
77
 
 
78
 
  "end": {
79
 
    topic: function() {
80
 
      var cb = this.callback,
81
 
          div = d3.select("body").html("").selectAll().data(["foo", "bar"]).enter().append("div").attr("class", String),
82
 
          transition = div.transition().duration(150),
83
 
          then = Date.now(),
84
 
          n = 0,
85
 
          calls = [],
86
 
          context = [],
87
 
          data = [],
88
 
          index = [],
89
 
          count = [],
90
 
          delay = [];
91
 
 
92
 
      // A callback to verify that multiple callbacks are allowed.
93
 
      transition.each("end.other", function() {
94
 
        ++n;
95
 
      });
96
 
 
97
 
      // A callback which captures arguments and context.
98
 
      transition.each("end", function(d, i) {
99
 
        context.push(this);
100
 
        data.push(d);
101
 
        index.push(i);
102
 
        count.push(++n);
103
 
        delay.push(Date.now() - then);
104
 
        if (n >= 4) cb(null, {
105
 
          selection: div,
106
 
          delay: delay,
107
 
          context: context,
108
 
          data: data,
109
 
          index: index,
110
 
          count: count,
111
 
          id: transition.id
112
 
        });
113
 
      });
114
 
    },
115
 
 
116
 
    "invokes the listener after the specified delay": function(result) {
117
 
      assert.inDelta(result.delay, [150, 150], 20);
118
 
    },
119
 
    "invokes each listener exactly once, in order": function(result) {
120
 
      assert.deepEqual(result.count, [2, 4]);
121
 
    },
122
 
 
123
 
    // For the same node, listeners will be called back in order, according to
124
 
    // the implementation of d3.dispatch. However, the order of callbacks across
125
 
    // nodes is not guaranteed; currently, callbacks are in reverse order if
126
 
    // they share the same delay, because of the timer queue. I suppose it'd be
127
 
    // slightly better if the callbacks were invoked in node order (consistent
128
 
    // with selections), but since these callbacks happen asynchronously I don't
129
 
    // think the API needs to guarantee the order of callbacks.
130
 
 
131
 
    "uses the node as the context": function(result) {
132
 
      assert.domEqual(result.context[1], result.selection[0][0]);
133
 
      assert.domEqual(result.context[0], result.selection[0][1]);
134
 
    },
135
 
    "passes the data and index to the function": function(result) {
136
 
      assert.deepEqual(result.data, ["bar", "foo"], "expected data, got {actual}");
137
 
      assert.deepEqual(result.index, [1, 0], "expected index, got {actual}");
138
 
    },
139
 
 
140
 
    "deletes the transition lock after end": function(result) {
141
 
      assert.isFalse("__transition__" in result.selection[0][0]);
142
 
      assert.isFalse("__transition__" in result.selection[0][1]);
143
 
    },
144
 
 
145
 
    // I'd like to test d3.timer.flush here, but unfortunately there's a bug in
146
 
    // Vows where it really doesn't like to receive multiple callbacks from
147
 
    // different tests at the same time!
148
 
 
149
 
    "sequenced": {
150
 
      topic: function(result) {
151
 
        var cb = this.callback,
152
 
            node = result.selection[0][0],
153
 
            id = result.id;
154
 
        d3.select(node).transition().delay(150).each("start", function() {
155
 
          cb(null, {id: id, node: this});
156
 
        });
157
 
      },
158
 
      "inherits the same transition id": function(result) {
159
 
        assert.isTrue(result.id > 0);
160
 
        assert.equal(result.node.__transition__.count, 1);
161
 
        assert.equal(result.node.__transition__.active, result.id);
162
 
      }
163
 
    }
164
 
  }
165
 
};