|
| 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 | + } |
| 40 | +}); |
| 41 | + |
| 42 | +suite.addBatch({ |
| 43 | + "selectAll(div)": { |
| 44 | + topic: function() { |
| 45 | + return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div"); |
| 46 | + }, |
| 47 | + "calls the function once per element": function(div) { |
| 48 | + var count = 0; |
| 49 | + div.each(function() { ++count; }); |
| 50 | + assert.equal(count, 2); |
| 51 | + }, |
| 52 | + "passes the data and index to the function": function(div) { |
| 53 | + var data = [new Object(), new Object()], dd = [], ii = []; |
| 54 | + div.data(data).each(function(d, i) { dd.push(d); ii.push(i); }); |
| 55 | + assert.deepEqual(dd, data); |
| 56 | + assert.deepEqual(ii, [0, 1]); |
| 57 | + }, |
| 58 | + "uses the node as the context": function(div) { |
| 59 | + var nodes = []; |
| 60 | + div.each(function() { nodes.push(this); }); |
| 61 | + assert.equal(nodes.length, 2); |
| 62 | + assert.isTrue(div[0][0] == nodes[0]); |
| 63 | + assert.isTrue(div[0][1] == nodes[1]); |
| 64 | + }, |
| 65 | + "returns the same selection": function(div) { |
| 66 | + assert.isTrue(div.each(function() {}) === div); |
| 67 | + }, |
| 68 | + "ignores null nodes": function(div) { |
| 69 | + var count = 0, some = d3.selectAll("div"); |
| 70 | + some[0][0] = null; |
| 71 | + some.each(function() { ++count; }); |
| 72 | + assert.equal(count, 1); |
| 73 | + } |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +suite.export(module); |
0 commit comments