Skip to content

Commit 9473156

Browse files
add table header tests
for #1015
1 parent 58fe586 commit 9473156

File tree

1 file changed

+110
-72
lines changed

1 file changed

+110
-72
lines changed

spec/data-table-spec.js

+110-72
Original file line numberDiff line numberDiff line change
@@ -35,75 +35,90 @@ describe('dc.dataTable', function () {
3535
return d.status;
3636
}]
3737
);
38-
chart.render();
3938
});
4039

41-
describe('creation', function () {
42-
it('generates something', function () {
43-
expect(chart).not.toBeNull();
44-
});
45-
it('registers', function () {
46-
expect(dc.hasChart(chart)).toBeTruthy();
47-
});
48-
it('sets size', function () {
49-
expect(chart.size()).toEqual(3);
50-
});
51-
it('sets sortBy', function () {
52-
expect(chart.sortBy()).not.toBeNull();
53-
});
54-
it('sets order', function () {
55-
expect(chart.order()).toBe(d3.descending);
56-
});
57-
it('group should be set', function () {
58-
expect(chart.group()).toEqual(valueGroup);
59-
});
60-
it('group tr should not be undefined', function () {
61-
expect(typeof(chart.selectAll('tr.dc-table-group')[0][0])).not.toBe('undefined');
62-
});
63-
it('sets column span set on group tr', function () {
64-
expect(chart.selectAll('tr.dc-table-group td')[0][0].getAttribute('colspan')).toEqual('2');
65-
});
66-
it('creates id column', function () {
67-
expect(chart.selectAll('td._0')[0][0].innerHTML).toEqual('9');
68-
expect(chart.selectAll('td._0')[0][1].innerHTML).toEqual('8');
69-
expect(chart.selectAll('td._0')[0][2].innerHTML).toEqual('3');
70-
});
71-
it('creates status column', function () {
72-
expect(chart.selectAll('td._1')[0][0].innerHTML).toEqual('T');
73-
expect(chart.selectAll('td._1')[0][1].innerHTML).toEqual('F');
74-
expect(chart.selectAll('td._1')[0][2].innerHTML).toEqual('T');
75-
});
76-
});
77-
78-
describe('slicing entries', function () {
40+
describe('simple table', function () {
7941
beforeEach(function () {
80-
chart.beginSlice(1);
81-
chart.redraw();
42+
chart.render();
8243
});
8344

84-
it('slice beginning', function () {
85-
expect(chart.selectAll('tr.dc-table-row')[0].length).toEqual(2);
45+
describe('creation', function () {
46+
it('generates something', function () {
47+
expect(chart).not.toBeNull();
48+
});
49+
it('registers', function () {
50+
expect(dc.hasChart(chart)).toBeTruthy();
51+
});
52+
it('sets size', function () {
53+
expect(chart.size()).toEqual(3);
54+
});
55+
it('sets sortBy', function () {
56+
expect(chart.sortBy()).not.toBeNull();
57+
});
58+
it('sets order', function () {
59+
expect(chart.order()).toBe(d3.descending);
60+
});
61+
it('group should be set', function () {
62+
expect(chart.group()).toEqual(valueGroup);
63+
});
64+
it('group tr should not be undefined', function () {
65+
expect(typeof(chart.selectAll('tr.dc-table-group')[0][0])).not.toBe('undefined');
66+
});
67+
it('sets column span set on group tr', function () {
68+
expect(chart.selectAll('tr.dc-table-group td')[0][0].getAttribute('colspan')).toEqual('2');
69+
});
70+
it('creates id column', function () {
71+
expect(chart.selectAll('td._0')[0][0].innerHTML).toEqual('9');
72+
expect(chart.selectAll('td._0')[0][1].innerHTML).toEqual('8');
73+
expect(chart.selectAll('td._0')[0][2].innerHTML).toEqual('3');
74+
});
75+
it('creates status column', function () {
76+
expect(chart.selectAll('td._1')[0][0].innerHTML).toEqual('T');
77+
expect(chart.selectAll('td._1')[0][1].innerHTML).toEqual('F');
78+
expect(chart.selectAll('td._1')[0][2].innerHTML).toEqual('T');
79+
});
8680
});
8781

88-
it('slice beginning and end', function () {
89-
chart.endSlice(2);
90-
chart.redraw();
82+
describe('slicing entries', function () {
83+
beforeEach(function () {
84+
chart.beginSlice(1);
85+
chart.redraw();
86+
});
9187

92-
expect(chart.selectAll('tr.dc-table-row')[0].length).toEqual(1);
93-
});
94-
});
88+
it('slice beginning', function () {
89+
expect(chart.selectAll('tr.dc-table-row')[0].length).toEqual(2);
90+
});
9591

96-
describe('external filter', function () {
97-
beforeEach(function () {
98-
countryDimension.filter('CA');
99-
chart.redraw();
92+
it('slice beginning and end', function () {
93+
chart.endSlice(2);
94+
chart.redraw();
95+
96+
expect(chart.selectAll('tr.dc-table-row')[0].length).toEqual(1);
97+
});
10098
});
101-
it('renders only filtered data set', function () {
102-
expect(chart.selectAll('td._0')[0].length).toEqual(2);
99+
100+
describe('external filter', function () {
101+
beforeEach(function () {
102+
countryDimension.filter('CA');
103+
chart.redraw();
104+
});
105+
it('renders only filtered data set', function () {
106+
expect(chart.selectAll('td._0')[0].length).toEqual(2);
107+
});
108+
it('renders the correctly filtered records', function () {
109+
expect(chart.selectAll('td._0')[0][0].innerHTML).toEqual('7');
110+
expect(chart.selectAll('td._0')[0][1].innerHTML).toEqual('5');
111+
});
103112
});
104-
it('renders the correctly filtered records', function () {
105-
expect(chart.selectAll('td._0')[0][0].innerHTML).toEqual('7');
106-
expect(chart.selectAll('td._0')[0][1].innerHTML).toEqual('5');
113+
114+
describe('ascending order', function () {
115+
beforeEach(function () {
116+
chart.order(d3.ascending);
117+
chart.redraw();
118+
});
119+
it('uses dimension.bottom() instead of top()', function () {
120+
expect(chart.selectAll('td._0')[0][0].innerHTML).toEqual('1');
121+
});
107122
});
108123
});
109124

@@ -128,16 +143,6 @@ describe('dc.dataTable', function () {
128143
});
129144
});
130145

131-
describe('ascending order', function () {
132-
beforeEach(function () {
133-
chart.order(d3.ascending);
134-
chart.redraw();
135-
});
136-
it('uses dimension.bottom() instead of top()', function () {
137-
expect(chart.selectAll('td._0')[0][0].innerHTML).toEqual('1');
138-
});
139-
});
140-
141146
describe('specifying chart columns with label', function () {
142147
beforeEach(function () {
143148
chart.columns(['state']);
@@ -182,14 +187,47 @@ describe('dc.dataTable', function () {
182187
}]);
183188
chart.render();
184189
});
185-
it('should render result of calling function with field and header for label', function () {
190+
it('should produce correct table header with single column', function () {
191+
var thead = chart.selectAll('thead');
192+
expect(thead.length).toBe(1);
193+
var tr = thead.selectAll('tr');
194+
expect(tr.length).toBe(1);
195+
var colheader = tr.selectAll('th.dc-table-head')[0].map(function (d) {return d.textContent;});
196+
expect(colheader.length).toEqual(1);
197+
expect(colheader[0]).toEqual('Test ID');
198+
});
199+
200+
it('should render correct values in rows', function () {
186201
var cols = chart.selectAll('td.dc-table-column')[0].map(function (d) {return d.textContent;});
187202
var expected = ['test9', 'test8', 'test3'];
188203
expect(cols.length).toEqual(expected.length);
189-
expected.forEach(function (d) {
190-
expect(cols).toContain(d);
204+
expected.forEach(function (d, i) {
205+
expect(cols[i]).toEqual(d);
191206
});
192-
var colheader = chart.selectAll('th.dc-table-head')[0].map(function (d) {return d.textContent;});
207+
});
208+
});
209+
210+
describe('with existing table header', function () {
211+
beforeEach(function () {
212+
// add some garbage for table to replace
213+
d3.select('#data-table')
214+
.selectAll('thead').data([0]).enter().append('thead')
215+
.selectAll('tr').data([1,2]).enter().append('tr')
216+
.selectAll('th').data([1,2,3]).enter().append('th');
217+
chart.columns([{
218+
label: 'Test ID',
219+
format: function (d) {
220+
return 'test' + d.id;
221+
}
222+
}]);
223+
chart.render();
224+
});
225+
it('should produce correct table header with single column', function () {
226+
var thead = chart.selectAll('thead');
227+
expect(thead.length).toBe(1);
228+
var tr = thead.selectAll('tr');
229+
expect(tr.length).toBe(1);
230+
var colheader = tr.selectAll('th.dc-table-head')[0].map(function (d) {return d.textContent;});
193231
expect(colheader.length).toEqual(1);
194232
expect(colheader[0]).toEqual('Test ID');
195233
});

0 commit comments

Comments
 (0)