3
var vows = require("vows"),
4
assert = require("assert");
6
var suite = vows.describe("selection.datum");
11
return d3.select("body").html("");
13
"updates the data according to the specified function": function(body) {
14
body.data([42]).datum(function(d, i) { return d + i; });
15
assert.equal(document.body.__data__, 42);
17
"updates the data to the specified constant": function(body) {
19
assert.equal(document.body.__data__, 43);
21
"deletes the data if the function returns null": function(body) {
22
body.data([42]).datum(function() { return null; });
23
assert.isFalse("__data__" in document.body);
25
"deletes the data if the constant is null": function(body) {
26
body.data([42]).datum(null);
27
assert.isFalse("__data__" in document.body);
29
"returns the current selection": function(body) {
30
assert.isTrue(body.datum(function() { return 1; }) === body);
31
assert.isTrue(body.datum(2) === body);
33
"with no arguments, returns the first node's datum": function(body) {
35
assert.equal(body.datum(), 42);
43
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
45
"updates the data according to the specified function": function(div) {
46
div.data([42, 43]).datum(function(d, i) { return d + i; });
47
assert.equal(div[0][0].__data__, 42);
48
assert.equal(div[0][1].__data__, 44);
50
"updates the data to the specified constant": function(div) {
52
assert.equal(div[0][0].__data__, 44);
53
assert.equal(div[0][1].__data__, 44);
55
"deletes the data if the function returns null": function(div) {
56
div.datum(function() { return null; });
57
assert.isFalse("__data__" in div[0][0]);
58
assert.isFalse("__data__" in div[0][1]);
60
"deletes the data if the constant is null": function(div) {
62
assert.isFalse("__data__" in div[0][0]);
63
assert.isFalse("__data__" in div[0][1]);
65
"returns the current selection": function(div) {
66
assert.isTrue(div.datum(function() { return 1; }) === div);
67
assert.isTrue(div.datum(2) === div);
69
"ignores null nodes": function(div) {
70
var some = d3.selectAll("div").data([42, 43]);
72
some.datum(function() { return 1; });
73
assert.equal(div[0][0].__data__, 1);
74
assert.equal(div[0][1].__data__, 43);
76
assert.equal(div[0][0].__data__, 2);
77
assert.equal(div[0][1].__data__, 43);