Better tests for transition.attr.

This commit is contained in:
Mike Bostock 2011-08-21 17:43:37 -07:00
Родитель f577542eff
Коммит 3f22d76fb3
3 изменённых файлов: 32 добавлений и 10 удалений

Просмотреть файл

@ -5,16 +5,29 @@ var assert = require("assert");
module.exports = {
topic: function() {
var callback = this.callback,
div = d3.select("body").append("div");
var cb = this.callback;
div
var s = d3.select("body").append("div")
.attr("width", 20)
.transition()
.attr("color", "red");
var t = s.transition()
.attr("width", 100)
.attr("width", 200)
.each("end", function() { callback(null, div); });
.attr("color", function() { return "green"; })
.each("end", function() { cb(null, {selection: s, transition: t}); });
},
"sets an attribute as a number": function(div) {
assert.equal(div.attr("width"), "200");
"defines the corresponding attr tween": function(result) {
assert.typeOf(result.transition.tween("attr.width"), "function");
assert.typeOf(result.transition.tween("attr.color"), "function");
},
"the last attr operator takes precedence": function(result) {
assert.equal(result.selection.attr("width"), "200");
},
"sets an attribute as a number": function(result) {
assert.equal(result.selection.attr("width"), "200");
},
"sets an attribute as a function": function(result) {
assert.equal(result.selection.attr("color"), "rgb(0,128,0)");
}
};

Просмотреть файл

@ -14,7 +14,7 @@ module.exports = {
var t = s.transition()
.style("background-color", "green")
.style("background-color", "red")
.style("color", "green", "important")
.style("color", function() { return "green"; }, "important")
.each("end", function() { cb(null, {selection: s, transition: t}); });
},
"defines the corresponding style tween": function(result) {
@ -25,6 +25,9 @@ module.exports = {
assert.equal(result.selection.style("background-color"), "rgb(255,0,0)");
},
"sets a property as a string": function(result) {
assert.equal(result.selection.style("background-color"), "rgb(255,0,0)");
},
"sets a property as a function": function(result) {
assert.equal(result.selection.style("color"), "rgb(0,128,0)");
},
"observes the specified priority": function(result) {

Просмотреть файл

@ -9,7 +9,8 @@ module.exports = {
dd = [],
ii = [],
tt = [],
vv = [];
vv = [],
fails = 0;
var s = d3.select("body").append("div").selectAll("div")
.data(["red", "green"])
@ -17,6 +18,7 @@ module.exports = {
.style("background-color", function(d) { return d3.rgb(d)+""; });
var t = s.transition()
.styleTween("background-color", function() { ++fails; })
.styleTween("background-color", tween);
function tween(d, i, v) {
@ -29,7 +31,8 @@ module.exports = {
data: dd,
index: ii,
value: vv,
context: tt
context: tt,
fails: fails
});
return i && d3.interpolateHsl(v, "blue");
}
@ -44,6 +47,9 @@ module.exports = {
"defines the corresponding style tween": function(result) {
assert.typeOf(result.transition.tween("style.background-color"), "function");
},
"the last style tween takes precedence": function(result) {
assert.equal(result.fails, 0);
},
"invokes the tween function": function(result) {
assert.deepEqual(result.data, ["red", "green"], "expected data, got {actual}");
assert.deepEqual(result.index, [0, 1], "expected data, got {actual}");