toolkit/javascript/d3/test/core/selection-call-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.call");
       
     8 
       
     9 suite.addBatch({
       
    10   "select(body)": {
       
    11     topic: function() {
       
    12       return d3.select("body").html("");
       
    13     },
       
    14     "calls the function once": function(body) {
       
    15       var count = 0;
       
    16       body.call(function() { ++count; });
       
    17       assert.equal(count, 1);
       
    18     },
       
    19     "passes any optional arguments": function(body) {
       
    20       var abc;
       
    21       body.call(function(selection, a, b, c) { abc = [a, b, c]; }, "a", "b", "c");
       
    22       assert.deepEqual(abc, ["a", "b", "c"]);
       
    23     },
       
    24     "passes the selection as the first argument": function(body) {
       
    25       var s;
       
    26       body.call(function(selection) { s = selection; });
       
    27       assert.isTrue(s === body);
       
    28     },
       
    29     "uses the selection as the context": function(body) {
       
    30       var s;
       
    31       body.call(function() { s = this; });
       
    32       assert.isTrue(s === body);
       
    33     },
       
    34     "returns the current selection": function(body) {
       
    35       assert.isTrue(body.call(function() {}) === body);
       
    36     }
       
    37   }
       
    38 });
       
    39 
       
    40 suite.addBatch({
       
    41   "selectAll(div)": {
       
    42     topic: function() {
       
    43       return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
       
    44     },
       
    45     "calls the function once": function(div) {
       
    46       var count = 0;
       
    47       div.call(function() { ++count; });
       
    48       assert.equal(count, 1);
       
    49     },
       
    50     "passes any optional arguments": function(div) {
       
    51       var abc;
       
    52       div.call(function(selection, a, b, c) { abc = [a, b, c]; }, "a", "b", "c");
       
    53       assert.deepEqual(abc, ["a", "b", "c"]);
       
    54     },
       
    55     "passes the selection as the first argument": function(div) {
       
    56       var s;
       
    57       div.call(function(selection) { s = selection; });
       
    58       assert.isTrue(s === div);
       
    59     },
       
    60     "uses the selection as the context": function(div) {
       
    61       var s;
       
    62       div.call(function() { s = this; });
       
    63       assert.isTrue(s === div);
       
    64     },
       
    65     "returns the current selection": function(div) {
       
    66       assert.isTrue(div.call(function() {}) === div);
       
    67     }
       
    68   }
       
    69 });
       
    70 
       
    71 suite.export(module);