toolkit/javascript/d3/test/core/selection-html-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.html");
       
     8 
       
     9 suite.addBatch({
       
    10   "select(body)": {
       
    11     topic: function() {
       
    12       return d3.select("body").html("");
       
    13     },
       
    14     "sets the inner HTML as a string": function(body) {
       
    15       body.html("<h1>Hello, world!</h1>");
       
    16       assert.equal(document.body.firstChild.tagName, "H1");
       
    17       assert.equal(document.body.firstChild.textContent, "Hello, world!");
       
    18     },
       
    19     "sets the inner HTML as a number": function(body) {
       
    20       body.html(42);
       
    21       assert.equal(document.body.innerHTML, "42");
       
    22       assert.equal(document.body.firstChild.nodeType, document.TEXT_NODE);
       
    23     },
       
    24     "sets the inner HTML as a function": function(body) {
       
    25       body.data(["Subject"]).html(function(d, i) { return "<b>" + d + "</b><i>" + i + "</i>"; });
       
    26       assert.equal(document.body.firstChild.tagName, "B");
       
    27       assert.equal(document.body.firstChild.textContent, "Subject");
       
    28       assert.equal(document.body.lastChild.tagName, "I");
       
    29       assert.equal(document.body.lastChild.textContent, "0");
       
    30     },
       
    31     /*
       
    32     https://github.com/tmpvar/jsdom/issues/276
       
    33     "clears the inner HTML as null": function(body) {
       
    34       body.html(null);
       
    35       assert.equal(document.body.innerHTML, "");
       
    36       assert.isNull(document.body.firstChild);
       
    37     },
       
    38     */
       
    39     "clears the inner HTML as the empty string": function(body) {
       
    40       body.html("");
       
    41       assert.equal(document.body.innerHTML, "");
       
    42       assert.isNull(document.body.firstChild);
       
    43     },
       
    44     "clears the inner HTML as a function": function(body) {
       
    45       body.text(function() { return ""; });
       
    46       assert.equal(document.body.innerHTML, "");
       
    47       assert.isNull(document.body.firstChild);
       
    48     },
       
    49     "ignores null nodes": function() {
       
    50       var body = d3.select("body");
       
    51       body[0][0] = null;
       
    52       document.body.innerHTML = "<h1>foo</h1>";
       
    53       body.html("bar");
       
    54       assert.equal(document.body.textContent, "foo");
       
    55     },
       
    56     "returns the current selection": function(body) {
       
    57       assert.isTrue(body.html("foo") === body);
       
    58     }
       
    59   }
       
    60 });
       
    61 
       
    62 suite.addBatch({
       
    63   "selectAll(div)": {
       
    64     topic: function() {
       
    65       return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
       
    66     },
       
    67     "sets the inner HTML as a string": function(div) {
       
    68       div.html("<h1>Hello, world!</h1>");
       
    69       assert.equal(div[0][0].firstChild.tagName, "H1");
       
    70       assert.equal(div[0][0].firstChild.textContent, "Hello, world!");
       
    71       assert.equal(div[0][1].firstChild.tagName, "H1");
       
    72       assert.equal(div[0][1].firstChild.textContent, "Hello, world!");
       
    73     },
       
    74     "sets the inner HTML as a number": function(div) {
       
    75       div.html(42);
       
    76       assert.equal(div[0][0].innerHTML, "42");
       
    77       assert.equal(div[0][0].firstChild.nodeType, document.TEXT_NODE);
       
    78     },
       
    79     "sets the inner HTML as a function": function(div) {
       
    80       div.data(["foo", "bar"]).html(function(d, i) { return "<b>" + d + "</b><i>" + i + "</i>"; });
       
    81       assert.equal(div[0][0].firstChild.tagName, "B");
       
    82       assert.equal(div[0][0].firstChild.textContent, "foo");
       
    83       assert.equal(div[0][0].lastChild.tagName, "I");
       
    84       assert.equal(div[0][0].lastChild.textContent, "0");
       
    85       assert.equal(div[0][1].firstChild.tagName, "B");
       
    86       assert.equal(div[0][1].firstChild.textContent, "bar");
       
    87       assert.equal(div[0][1].lastChild.tagName, "I");
       
    88       assert.equal(div[0][1].lastChild.textContent, "1");
       
    89     },
       
    90     /*
       
    91     https://github.com/tmpvar/jsdom/issues/276
       
    92     "clears the inner HTML as null": function(div) {
       
    93       div.html(null);
       
    94       assert.equal(div[0][0].innerHTML, "");
       
    95       assert.isNull(div[0][0].firstChild);
       
    96       assert.equal(div[0][1].innerHTML, "");
       
    97       assert.isNull(div[0][1].firstChild);
       
    98     },
       
    99     */
       
   100     "clears the inner HTML as a function": function(div) {
       
   101       div.html(function() { return ""; });
       
   102       assert.equal(div[0][0].innerHTML, "");
       
   103       assert.isNull(div[0][0].firstChild);
       
   104       assert.equal(div[0][1].innerHTML, "");
       
   105       assert.isNull(div[0][1].firstChild);
       
   106     },
       
   107     "ignores null nodes": function(div) {
       
   108       var some = d3.selectAll("div");
       
   109       some[0][0] = null;
       
   110       div[0][0].innerHTML = "<h1>foo</h1>";
       
   111       some.html("bar");
       
   112       assert.equal(div[0][0].textContent, "foo");
       
   113       assert.equal(div[0][1].textContent, "bar");
       
   114     },
       
   115     "returns the current selection": function(div) {
       
   116       assert.isTrue(div.html("foo") === div);
       
   117     }
       
   118   }
       
   119 });
       
   120 
       
   121 suite.export(module);