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