forked from dc-js/dc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-display-spec.js
168 lines (165 loc) · 6.29 KB
/
number-display-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* global appendChartID, loadDateFixture */
describe('dc.numberDisplay', function () {
var data, meanGroup;
var countryDimension;
function average (d) {
return d.n ? d.tot / d.n : 0;
}
beforeEach(function () {
data = crossfilter(loadDateFixture());
var groupAll = data.groupAll();
meanGroup = groupAll.reduce(
function (p, v) {
++p.n;
p.tot += +v.value;
return p;
},
function (p, v) {
--p.n;
p.tot -= +v.value;
return p;
},
function () { return {n: 0,tot: 0}; }
);
countryDimension = data.dimension(function (d) {
return d.countrycode;
});
countryDimension.filter('CA');
});
function buildChart (id) {
var chart = dc.numberDisplay(id)
.transitionDuration(0)
.group(meanGroup)
.formatNumber(d3.format('.3s'))
.valueAccessor(average);
chart.render();
d3.timer.flush();
return chart;
}
describe('Empty Div', function () {
var chart;
beforeEach(function () {
var id = 'empty-div';
appendChartID(id);
chart = buildChart('#' + id);
});
it('should generate something', function () {
expect(chart).not.toBeNull();
});
it('should be registered', function () {
expect(dc.hasChart(chart)).toBeTruthy();
});
it('should return a value', function () {
expect(chart.value()).toEqual(38.5);
});
it('should have text value in child', function () {
expect(chart.select('span.number-display').text()).toEqual('38.5');
});
describe('redraw', function () {
beforeEach(function () {
countryDimension.filterAll();
chart.redraw();
d3.timer.flush();
});
it('should update value', function () {
expect(chart.select('span.number-display').text()).toEqual('41.8');
});
});
describe('html with one, some and none', function () {
beforeEach(function () {
chart.html({one: '%number number',none: 'no number',some: '%number numbers'});
chart.redraw();
d3.timer.flush();
});
it('should use some for some', function () {
expect(chart.select('span.number-display').text()).toEqual('38.5 numbers');
});
});
describe('html with one, some and none', function () {
beforeEach(function () {
chart.html({one: '%number number',none: 'no number',some: '%number numbers'});
chart.valueAccessor(function (d) {return 1;});
chart.redraw();
d3.timer.flush();
});
it('should use one for one', function () {
expect(chart.select('span.number-display').text()).toEqual('1.00 number');
});
});
describe('html with one, some and none', function () {
beforeEach(function () {
chart.html({one: '%number number',none: 'no number',some: '%number numbers'});
chart.valueAccessor(function (d) {return 0;});
chart.redraw();
d3.timer.flush();
});
it('should use zero for zero', function () {
expect(chart.select('span.number-display').text()).toEqual('no number');
});
});
describe('html with just one', function () {
beforeEach(function () {
chart.html({one: '%number number'});
chart.redraw();
d3.timer.flush();
});
it('should use one for showing some', function () {
expect(chart.select('span.number-display').text()).toEqual('38.5 number');
});
});
describe('html with just some', function () {
beforeEach(function () {
chart.html({some: '%number numbers'});
chart.redraw();
d3.timer.flush();
});
it('should use some for showing one', function () {
expect(chart.select('span.number-display').text()).toEqual('38.5 numbers');
});
});
describe('html with just none', function () {
beforeEach(function () {
chart.html({});
chart.redraw();
d3.timer.flush();
});
it('should just show the number in case of some and one', function () {
expect(chart.select('span.number-display').text()).toEqual('38.5');
});
});
afterEach(function () {
countryDimension.filterAll();
});
});
describe('Div with embedded span', function () {
var chart;
beforeEach(function () {
var id = 'full-div';
var div = appendChartID(id);
div.append('p').html('There are <span class="number-display">_</span> Total Widgets.');
chart = buildChart('#' + id);
});
it('should have text value in child', function () {
expect(chart.root().text()).toEqual('There are 38.5 Total Widgets.');
});
afterEach(function () {
countryDimension.filterAll();
});
});
describe('Inline nonspan element' , function () {
beforeEach(function () {
var div = d3.select('body').append('div').attr('id','number-display-test-section');
div.append('p').html('There are <em id="nonspan"></em> Total Widgets.');
buildChart('#nonspan');
});
it('should have text value in child', function () {
expect(d3.select('body').select('#number-display-test-section').html())
.toMatch(new RegExp('<p>There are <em (?:id="nonspan" class="dc-chart"|class="dc-chart" id="nonspan")>' +
'<span class="number-display">38.5</span></em> Total Widgets.</p>'));
});
afterEach(function () {
countryDimension.filterAll();
d3.select('#number-display-test-section').remove();
});
});
});