toolkit/javascript/d3/test/core/selection-each-test.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 require("../env");
       
     2 require("../../d3");
       
     3 
       
     4 var vows = require("vows"),
       
     5     assert = require("assert");
       
     6 
       
     7 var suite = vows.describe("selection.each");
       
     8 
       
     9 suite.addBatch({
       
    10   "select(body)": {
       
    11     topic: function() {
       
    12       return d3.select("body").html("");
       
    13     },
       
    14     "calls the function once per element": function(body) {
       
    15       var count = 0;
       
    16       body.each(function() { ++count; });
       
    17       assert.equal(count, 1);
       
    18     },
       
    19     "passes the data and index to the function": function(body) {
       
    20       var data = new Object(), dd, ii;
       
    21       body.data([data]).each(function(d, i) { dd = d; ii = i; });
       
    22       assert.isTrue(dd === data);
       
    23       assert.isTrue(ii === 0);
       
    24     },
       
    25     "uses the node as the context": function(body) {
       
    26       var node;
       
    27       body.each(function() { node = this; });
       
    28       assert.isTrue(node === document.body);
       
    29     },
       
    30     "returns the same selection": function(body) {
       
    31       assert.isTrue(body.each(function() {}) === body);
       
    32     },
       
    33     "ignores null nodes": function() {
       
    34       var count = 0, body = d3.select("body");
       
    35       body[0][0] = null;
       
    36       body.each(function() { ++count; });
       
    37       assert.equal(count, 0);
       
    38     },
       
    39     "returns the current selection": function(body) {
       
    40       assert.isTrue(body.each(function() {}) === body);
       
    41     }
       
    42   }
       
    43 });
       
    44 
       
    45 suite.addBatch({
       
    46   "selectAll(div)": {
       
    47     topic: function() {
       
    48       return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
       
    49     },
       
    50     "calls the function once per element": function(div) {
       
    51       var count = 0;
       
    52       div.each(function() { ++count; });
       
    53       assert.equal(count, 2);
       
    54     },
       
    55     "passes the data and index to the function": function(div) {
       
    56       var data = [new Object(), new Object()], dd = [], ii = [];
       
    57       div.data(data).each(function(d, i) { dd.push(d); ii.push(i); });
       
    58       assert.deepEqual(dd, data);
       
    59       assert.deepEqual(ii, [0, 1]);
       
    60     },
       
    61     "uses the node as the context": function(div) {
       
    62       var nodes = [];
       
    63       div.each(function() { nodes.push(this); });
       
    64       assert.equal(nodes.length, 2);
       
    65       assert.isTrue(div[0][0] == nodes[0]);
       
    66       assert.isTrue(div[0][1] == nodes[1]);
       
    67     },
       
    68     "returns the same selection": function(div) {
       
    69       assert.isTrue(div.each(function() {}) === div);
       
    70     },
       
    71     "ignores null nodes": function(div) {
       
    72       var count = 0, some = d3.selectAll("div");
       
    73       some[0][0] = null;
       
    74       some.each(function() { ++count; });
       
    75       assert.equal(count, 1);
       
    76     },
       
    77     "returns the current selection": function(div) {
       
    78       assert.isTrue(div.each(function() {}) === div);
       
    79     }
       
    80   }
       
    81 });
       
    82 
       
    83 suite.export(module);