toolkit/javascript/d3/test/core/transition-test-attrTween.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         vv = [];
       
    13 
       
    14     var s = d3.select("body").html("").append("div").selectAll("div")
       
    15         .data(["red", "green"])
       
    16       .enter().append("div")
       
    17         .attr("color", function(d) { return d3.rgb(d)+""; });
       
    18 
       
    19     var t = s.transition()
       
    20         .attrTween("color", tween);
       
    21 
       
    22     function tween(d, i, v) {
       
    23       dd.push(d);
       
    24       ii.push(i);
       
    25       vv.push(v);
       
    26       if (tt.push(this) >= 2) cb(null, {
       
    27         selection: s,
       
    28         transition: t,
       
    29         data: dd,
       
    30         index: ii,
       
    31         value: vv,
       
    32         context: tt
       
    33       });
       
    34       return i && d3.interpolateHsl(v, "blue");
       
    35     }
       
    36   },
       
    37 
       
    38   // The order here is a bit brittle: because the transition has zero delay,
       
    39   // it's invoking the start event immediately for all nodes, rather than
       
    40   // pushing each node onto the timer queue (which would reverse the order of
       
    41   // callbacks). The order in which tweens are invoked is undefined, so perhaps
       
    42   // we should sort the expected and actual values before comparing.
       
    43 
       
    44   "defines the corresponding attr tween": function(result) {
       
    45     assert.typeOf(result.transition.tween("attr.color"), "function");
       
    46   },
       
    47   "invokes the tween function": function(result) {
       
    48     assert.deepEqual(result.data, ["red", "green"], "expected data, got {actual}");
       
    49     assert.deepEqual(result.index, [0, 1], "expected data, got {actual}");
       
    50     assert.deepEqual(result.value, ["#ff0000", "#008000"], "expected value, got {actual}");
       
    51     assert.domEqual(result.context[0], result.selection[0][0], "expected this, got {actual}");
       
    52     assert.domEqual(result.context[1], result.selection[0][1], "expected this, got {actual}");
       
    53   },
       
    54 
       
    55   "end": {
       
    56     topic: function(result) {
       
    57       var cb = this.callback;
       
    58       result.transition.each("end", function(d, i) { if (i >= 1) cb(null, result); });
       
    59     },
       
    60     "uses the returned interpolator": function(result) {
       
    61       assert.equal(result.selection[0][1].getAttribute("color"), "#0000ff");
       
    62     },
       
    63     "does nothing if the interpolator is falsey": function(result) {
       
    64       assert.equal(result.selection[0][0].getAttribute("color"), "#ff0000");
       
    65     }
       
    66   }
       
    67 };