toolkit/javascript/d3/test/core/transition-test-tween.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 require("../env");
       
     2 require("../../d3");
       
     3 
       
     4 var assert = require("assert");
       
     5 
       
     6 module.exports = {
       
     7   topic: function() {
       
     8     var cb = this.callback,
       
     9         dd = [],
       
    10         ii = [],
       
    11         tt = [],
       
    12         fails = 0;
       
    13 
       
    14     var s = d3.select("body").append("div").selectAll("div")
       
    15         .data(["red", "green"])
       
    16       .enter().append("div")
       
    17         .text(function(d) { return d3.rgb(d)+""; });
       
    18 
       
    19     var t = s.transition()
       
    20         .tween("text", function() { ++fails; })
       
    21         .tween("text", tween);
       
    22 
       
    23     function tween(d, i) {
       
    24       dd.push(d);
       
    25       ii.push(i);
       
    26       if (tt.push(this) >= 2) cb(null, {
       
    27         selection: s,
       
    28         transition: t,
       
    29         data: dd,
       
    30         index: ii,
       
    31         context: tt,
       
    32         fails: fails
       
    33       });
       
    34       return i && function(t) {
       
    35         this.textContent = d3.hsl(230, 0.5, t) + "";
       
    36       };
       
    37     }
       
    38   },
       
    39 
       
    40   // The order here is a bit brittle: because the transition has zero delay,
       
    41   // it's invoking the start event immediately for all nodes, rather than
       
    42   // pushing each node onto the timer queue (which would reverse the order of
       
    43   // callbacks). The order in which tweens are invoked is undefined, so perhaps
       
    44   // we should sort the expected and actual values before comparing.
       
    45 
       
    46   "defines the corresponding tween": function(result) {
       
    47     assert.typeOf(result.transition.tween("text"), "function");
       
    48   },
       
    49   "the last tween takes precedence": function(result) {
       
    50     assert.equal(result.fails, 0);
       
    51   },
       
    52   "invokes the tween function": function(result) {
       
    53     assert.deepEqual(result.data, ["red", "green"], "expected data, got {actual}");
       
    54     assert.deepEqual(result.index, [0, 1], "expected data, got {actual}");
       
    55     assert.domEqual(result.context[0], result.selection[0][0], "expected this, got {actual}");
       
    56     assert.domEqual(result.context[1], result.selection[0][1], "expected this, got {actual}");
       
    57   },
       
    58 
       
    59   "end": {
       
    60     topic: function(result) {
       
    61       var cb = this.callback;
       
    62       result.transition.each("end", function(d, i) { if (i >= 1) cb(null, result); });
       
    63     },
       
    64     "uses the returned tweener": function(result) {
       
    65       assert.equal(result.selection[0][1].textContent, "#ffffff");
       
    66     },
       
    67     "does nothing if the tweener is falsey": function(result) {
       
    68       assert.equal(result.selection[0][0].textContent, "#ff0000");
       
    69     }
       
    70   }
       
    71 };