toolkit/javascript/d3/test/core/selection-attr-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.attr");
       
     8 
       
     9 suite.addBatch({
       
    10   "select(body)": {
       
    11     topic: function() {
       
    12       return d3.select("body");
       
    13     },
       
    14     "sets an attribute as a string": function(body) {
       
    15       body.attr("bgcolor", "red");
       
    16       assert.equal(document.body.getAttribute("bgcolor"), "red");
       
    17     },
       
    18     "sets an attribute as a number": function(body) {
       
    19       body.attr("opacity", 1);
       
    20       assert.equal(document.body.getAttribute("opacity"), "1");
       
    21     },
       
    22     "sets an attribute as a function": function(body) {
       
    23       body.attr("bgcolor", function() { return "orange"; });
       
    24       assert.equal(document.body.getAttribute("bgcolor"), "orange");
       
    25     },
       
    26     "sets an attribute as a function of data": function(body) {
       
    27       body.data(["cyan"]).attr("bgcolor", String);
       
    28       assert.equal(document.body.getAttribute("bgcolor"), "cyan");
       
    29     },
       
    30     "sets an attribute as a function of index": function(body) {
       
    31       body.attr("bgcolor", function(d, i) { return "orange-" + i; });
       
    32       assert.equal(document.body.getAttribute("bgcolor"), "orange-0");
       
    33     },
       
    34     "sets a namespaced attribute as a string": function(body) {
       
    35       body.attr("xlink:href", "url");
       
    36       assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url");
       
    37     },
       
    38     "sets a namespaced attribute as a function": function(body) {
       
    39       body.data(["orange"]).attr("xlink:href", function(d, i) { return d + "-" + i; });
       
    40       assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0");
       
    41     },
       
    42     "gets an attribute value": function(body) {
       
    43       document.body.setAttribute("bgcolor", "yellow");
       
    44       assert.equal(body.attr("bgcolor"), "yellow");
       
    45     },
       
    46     "gets a namespaced attribute value": function(body) {
       
    47       document.body.setAttributeNS("http://www.w3.org/1999/xlink", "foo", "bar");
       
    48       assert.equal(body.attr("xlink:foo"), "bar");
       
    49     },
       
    50     "removes an attribute as null": function(body) {
       
    51       body.attr("bgcolor", "red").attr("bgcolor", null);
       
    52       assert.equal(body.attr("bgcolor"), "");
       
    53     },
       
    54     "removes an attribute as a function": function(body) {
       
    55       body.attr("bgcolor", "red").attr("bgcolor", function() { return null; });
       
    56       assert.equal(body.attr("bgcolor"), "");
       
    57     },
       
    58     "removes a namespaced attribute as null": function(body) {
       
    59       body.attr("xlink:href", "url").attr("xlink:href", null);
       
    60       assert.equal(body.attr("bgcolor"), "");
       
    61     },
       
    62     "removes a namespaced attribute as a function": function(body) {
       
    63       body.attr("xlink:href", "url").attr("xlink:href", function() { return null; });
       
    64       assert.equal(body.attr("xlink:href"), "");
       
    65     },
       
    66     "returns the current selection": function(body) {
       
    67       assert.isTrue(body.attr("foo", "bar") === body);
       
    68     }
       
    69   }
       
    70 });
       
    71 
       
    72 suite.addBatch({
       
    73   "selectAll(div)": {
       
    74     topic: function() {
       
    75       return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
       
    76     },
       
    77     "sets an attribute as a string": function(div) {
       
    78       div.attr("bgcolor", "red");
       
    79       assert.equal(div[0][0].getAttribute("bgcolor"), "red");
       
    80       assert.equal(div[0][1].getAttribute("bgcolor"), "red");
       
    81     },
       
    82     "sets an attribute as a number": function(div) {
       
    83       div.attr("opacity", 0.4);
       
    84       assert.equal(div[0][0].getAttribute("opacity"), "0.4");
       
    85       assert.equal(div[0][1].getAttribute("opacity"), "0.4");
       
    86     },
       
    87     "sets an attribute as a function": function(div) {
       
    88       div.attr("bgcolor", function() { return "coral"; });
       
    89       assert.equal(div[0][0].getAttribute("bgcolor"), "coral");
       
    90       assert.equal(div[0][1].getAttribute("bgcolor"), "coral");
       
    91     },
       
    92     "sets an attribute as a function of data": function(div) {
       
    93       div.attr("bgcolor", d3.interpolateRgb("brown", "steelblue"));
       
    94       assert.equal(div[0][0].getAttribute("bgcolor"), "#a52a2a");
       
    95       assert.equal(div[0][1].getAttribute("bgcolor"), "#4682b4");
       
    96     },
       
    97     "sets an attribute as a function of index": function(div) {
       
    98       div.attr("bgcolor", function(d, i) { return "color-" + i; });
       
    99       assert.equal(div[0][0].getAttribute("bgcolor"), "color-0");
       
   100       assert.equal(div[0][1].getAttribute("bgcolor"), "color-1");
       
   101     },
       
   102     "sets a namespaced attribute as a string": function(div) {
       
   103       div.attr("xlink:href", "url");
       
   104       assert.equal(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url");
       
   105       assert.equal(div[0][1].getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url");
       
   106     },
       
   107     "sets a namespaced attribute as a function": function(div) {
       
   108       div.data(["red", "blue"]).attr("xlink:href", function(d, i) { return d + "-" + i; });
       
   109       assert.equal(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "href"), "red-0");
       
   110       assert.equal(div[0][1].getAttributeNS("http://www.w3.org/1999/xlink", "href"), "blue-1");
       
   111     },
       
   112     "gets an attribute value": function(div) {
       
   113       div[0][0].setAttribute("bgcolor", "purple");
       
   114       assert.equal(div.attr("bgcolor"), "purple");
       
   115     },
       
   116     "gets a namespaced attribute value": function(div) {
       
   117       div[0][0].setAttributeNS("http://www.w3.org/1999/xlink", "foo", "bar");
       
   118       assert.equal(div.attr("xlink:foo"), "bar");
       
   119     },
       
   120     "removes an attribute as null": function(div) {
       
   121       div.attr("href", "url").attr("href", null);
       
   122       assert.equal(div[0][0].getAttribute("href"), "");
       
   123       assert.equal(div[0][1].getAttribute("href"), "");
       
   124     },
       
   125     "removes an attribute as a function": function(div) {
       
   126       div.attr("href", "url").attr("href", function() { return null; });
       
   127       assert.equal(div[0][0].getAttribute("href"), "");
       
   128       assert.equal(div[0][1].getAttribute("href"), "");
       
   129     },
       
   130     "removes a namespaced attribute as null": function(div) {
       
   131       div.attr("xlink:foo", "bar").attr("xlink:foo", null);
       
   132       assert.equal(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "foo"), "");
       
   133       assert.equal(div[0][1].getAttributeNS("http://www.w3.org/1999/xlink", "foo"), "");
       
   134     },
       
   135     "removes a namespaced attribute as a function": function(div) {
       
   136       div.attr("xlink:foo", "bar").attr("xlink:foo", function() { return null; });
       
   137       assert.equal(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "foo"), "");
       
   138       assert.equal(div[0][1].getAttributeNS("http://www.w3.org/1999/xlink", "foo"), "");
       
   139     },
       
   140     "ignores null nodes": function(div) {
       
   141       var some = d3.selectAll("div");
       
   142       some[0][1] = null;
       
   143       some.attr("href", null).attr("href", "url");
       
   144       assert.equal(div[0][0].getAttribute("href"), "url");
       
   145       assert.equal(div[0][1].getAttribute("href"), "");
       
   146     },
       
   147     "returns the current selection": function(div) {
       
   148       assert.isTrue(div.attr("foo", "bar") === div);
       
   149     }
       
   150   }
       
   151 });
       
   152 
       
   153 suite.export(module);