Skip to content

Commit af4b510

Browse files
committed
add tests for number display. Closes #291
1 parent 6d3627e commit af4b510

File tree

8 files changed

+117
-5
lines changed

8 files changed

+117
-5
lines changed

dc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5370,6 +5370,8 @@ dc.numberDisplay = function (parent, chartGroup) {
53705370
return _chart.valueAccessor()(valObj);
53715371
};
53725372

5373+
_chart.transitionDuration(250); // good default
5374+
53735375
_chart.doRender = function () {
53745376
var newValue = _chart.value(),
53755377
span = _chart.selectAll("."+SPAN_CLASS);
@@ -5381,7 +5383,7 @@ dc.numberDisplay = function (parent, chartGroup) {
53815383
.attr("class", SPAN_CLASS);
53825384

53835385
span.transition()
5384-
.duration(250)
5386+
.duration(_chart.transitionDuration())
53855387
.ease('quad-out-in')
53865388
.tween("text", function () {
53875389
var interp = d3.interpolateNumber(this.lastValue || 0, newValue);

dc.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dc.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/number-display.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ dc.numberDisplay = function (parent, chartGroup) {
4343
return _chart.valueAccessor()(valObj);
4444
};
4545

46+
_chart.transitionDuration(250); // good default
47+
4648
_chart.doRender = function () {
4749
var newValue = _chart.value(),
4850
span = _chart.selectAll("."+SPAN_CLASS);
@@ -54,7 +56,7 @@ dc.numberDisplay = function (parent, chartGroup) {
5456
.attr("class", SPAN_CLASS);
5557

5658
span.transition()
57-
.duration(250)
59+
.duration(_chart.transitionDuration())
5860
.ease('quad-out-in')
5961
.tween("text", function () {
6062
var interp = d3.interpolateNumber(this.lastValue || 0, newValue);

test/env-data.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/number-display-test.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
require("./env");
2+
3+
var vows = require('vows');
4+
var assert = require('assert');
5+
6+
var suite = vows.describe('Number Display');
7+
8+
var meanGroup = groupAll2.reduce(
9+
function (p, v) {
10+
++p.n;
11+
p.tot += +v.value;
12+
return p;
13+
},
14+
function (p, v) {
15+
--p.n;
16+
p.tot -= +v.value;
17+
return p;
18+
},
19+
function () { return {n:0,tot:0}; }
20+
);
21+
22+
function average(d) {
23+
return d.n ? d.tot / d.n : 0;
24+
}
25+
26+
function buildChart(id) {
27+
countryDimension.filter("CA");
28+
var chart = dc.numberDisplay(id)
29+
.transitionDuration(0)
30+
.group(meanGroup)
31+
.formatNumber(d3.format(".3s"))
32+
.valueAccessor(average);
33+
chart.render();
34+
d3.timer.flush();
35+
return chart;
36+
}
37+
38+
suite.addBatch({
39+
'Empty Div': {
40+
topic: function() {
41+
d3.select("body").append("div").attr("id","empty-div");
42+
return buildChart("#empty-div");
43+
},
44+
'should generate something': function(chart) {
45+
assert.isNotNull(chart);
46+
},
47+
'should be registered': function(chart) {
48+
assert.isTrue(dc.hasChart(chart));
49+
},
50+
'should return a value': function(chart) {
51+
assert.equal(chart.value(), "38.5");
52+
},
53+
'should have text value in child': function(chart) {
54+
assert.equal(chart.select("span.number-display").text(), "38.5");
55+
},
56+
'redraw':{
57+
topic: function(chart) {
58+
resetAllFilters();
59+
chart.redraw();
60+
d3.timer.flush();
61+
return chart;
62+
},
63+
'should update value': function(chart) {
64+
assert.equal(chart.select("span.number-display").text(), "41.8");
65+
}
66+
},
67+
'teardown': function() {
68+
resetAllFilters();
69+
resetBody();
70+
}
71+
},
72+
'Div with embedded span': {
73+
topic: function() {
74+
var div = d3.select("body").append("div").attr("id","full-div");
75+
var p = div.append("p").html("There are <span class=\"number-display\">_</span> Total Widgets.");
76+
return buildChart("#full-div");
77+
},
78+
'should have text value in child': function(chart) {
79+
assert.equal(chart.root().text(), "There are 38.5 Total Widgets.");
80+
},
81+
'teardown': function() {
82+
resetAllFilters();
83+
resetBody();
84+
}
85+
},
86+
'Inline nonspan element' : {
87+
topic: function() {
88+
var div = d3.select("body").append("div").attr("id","section");
89+
div.append("p").html("There are <em id=\"nonspan\"></em> Total Widgets.");
90+
return buildChart("#nonspan");
91+
},
92+
'should have text value in child': function(chart) {
93+
assert.equal(d3.select("body").select("#section").html(),
94+
'<p>There are <em id="nonspan" class="dc-chart"><span class="number-display">38.5</span></em> Total Widgets.</p>');
95+
},
96+
'teardown': function() {
97+
resetAllFilters();
98+
resetBody();
99+
}
100+
}
101+
});
102+
103+
suite.export(module);
104+

web/js/dc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5370,6 +5370,8 @@ dc.numberDisplay = function (parent, chartGroup) {
53705370
return _chart.valueAccessor()(valObj);
53715371
};
53725372

5373+
_chart.transitionDuration(250); // good default
5374+
53735375
_chart.doRender = function () {
53745376
var newValue = _chart.value(),
53755377
span = _chart.selectAll("."+SPAN_CLASS);
@@ -5381,7 +5383,7 @@ dc.numberDisplay = function (parent, chartGroup) {
53815383
.attr("class", SPAN_CLASS);
53825384

53835385
span.transition()
5384-
.duration(250)
5386+
.duration(_chart.transitionDuration())
53855387
.ease('quad-out-in')
53865388
.tween("text", function () {
53875389
var interp = d3.interpolateNumber(this.lastValue || 0, newValue);

web/js/env-data.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)