-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathcore-spec.js
342 lines (293 loc) · 11.9 KB
/
core-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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* global loadDateFixture */
describe('dc.core', function () {
var valueDimension, valueGroup;
beforeEach(function () {
var data = crossfilter(loadDateFixture());
valueDimension = data.dimension(function (d) {
return d.value;
});
valueGroup = valueDimension.group();
});
describe('version', function () {
it('should use semantic versions', function () {
// from https://raw.github.com/coolaj86/semver-utils/v1.0.3/semver-utils.js
// |optional 'v'
// | | 3 segment version
// | | |optional release prefixed by '-'
// | | | |optional build prefixed by '+'
var reSemver = /^v?((\d+)\.(\d+)\.(\d+))(?:-([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?(?:\+([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?$/;
expect(dc.version).toMatch(reSemver);
});
});
describe('charts', function () {
var chart;
beforeEach(function () {
chart = dc.pieChart('#id')
.dimension(valueDimension)
.group(valueGroup);
spyOn(chart, 'filterAll');
spyOn(chart, 'render');
spyOn(chart, 'redraw');
return chart;
});
it('should register chart object', function () {
expect(dc.hasChart(chart)).toBeTruthy();
});
it('filterAll should not invoke filter on each unfiltered chart', function () {
dc.filterAll();
expect(chart.filterAll).not.toHaveBeenCalled();
});
it('renderAll should invoke filter on each chart', function () {
dc.renderAll();
expect(chart.render).toHaveBeenCalled();
});
it('should be gone after remove all', function () {
dc.deregisterAllCharts();
expect(dc.hasChart(chart)).toBeFalsy();
});
});
describe('chart w/ filter', function () {
var chart;
beforeEach(function () {
chart = dc.pieChart('#id')
.dimension(valueDimension)
.group(valueGroup)
.filter(0);
spyOn(chart, 'filterAll');
return chart;
});
it('filterAll should invoke filter on each filtered chart', function () {
dc.filterAll();
expect(chart.filterAll).toHaveBeenCalled();
});
});
describe('chartsRegistry', function () {
var chart;
var chartGrouped;
var chartGroup = 'testChartGroup';
beforeEach(function () {
chart = dc.pieChart('#id')
.dimension(valueDimension)
.group(valueGroup);
chartGrouped = dc.pieChart('#id2', chartGroup)
.dimension(valueDimension)
.group(valueGroup);
return chart;
});
it('should register chart object', function () {
expect(dc.hasChart(chart)).toBeTruthy();
});
it('should not have ungrouped chart after remove', function () {
dc.deregisterChart(chart);
expect(dc.hasChart(chart)).toBeFalsy();
});
it('should not have grouped chart after remove', function () {
dc.deregisterChart(chartGrouped, chartGroup);
expect(dc.hasChart(chartGrouped)).toBeFalsy();
});
it('should have switched to an existing group', function () {
chart.chartGroup(chartGroup);
expect(dc.hasChart(chart)).toBeTruthy();
expect(dc.chartRegistry.list(chartGroup).indexOf(chart) > -1).toBeTruthy();
expect(dc.chartRegistry.list(null).indexOf(chart) > -1).toBeFalsy();
});
it('should have switched to the global group', function () {
chart.chartGroup(null);
expect(dc.hasChart(chart)).toBeTruthy();
expect(dc.chartRegistry.list(chartGroup).indexOf(chart) > -1).toBeFalsy();
expect(dc.chartRegistry.list(null).indexOf(chart) > -1).toBeTruthy();
});
});
describe('transition', function () {
var selections;
beforeEach(function () {
selections = {
transition: function () {
return this;
},
duration: function () {
return this;
},
delay: function () {
return this;
}
};
spyOn(selections, 'transition').and.callThrough();
spyOn(selections, 'duration').and.callThrough();
spyOn(selections, 'delay').and.callThrough();
});
describe('normal', function () {
it('transition should be activated with duration', function () {
dc.transition(selections, 100, 100);
expect(selections.transition).toHaveBeenCalled();
expect(selections.duration).toHaveBeenCalled();
expect(selections.delay).toHaveBeenCalled();
expect(selections.duration).toHaveBeenCalledWith(100);
expect(selections.delay).toHaveBeenCalledWith(100);
});
it('with name', function () {
dc.transition(selections, 100, 100, 'transition-name');
expect(selections.transition).toHaveBeenCalled();
expect(selections.transition).toHaveBeenCalledWith('transition-name');
});
});
describe('skip', function () {
it('transition should not be activated with 0 duration', function () {
dc.transition(selections, 0, 0);
expect(selections.transition).not.toHaveBeenCalled();
expect(selections.duration).not.toHaveBeenCalled();
expect(selections.delay).not.toHaveBeenCalled();
});
it('transition should not be activated with dc.disableTransitions', function () {
dc.disableTransitions = true;
dc.transition(selections, 100);
expect(selections.transition).not.toHaveBeenCalled();
expect(selections.duration).not.toHaveBeenCalled();
});
afterEach(function () {
dc.disableTransitions = false;
});
});
describe('parameters', function () {
it('duration should not be called if skipped', function () {
dc.transition(selections);
expect(selections.duration).not.toHaveBeenCalled();
});
it('delay should not be called if skipped', function () {
dc.transition(selections, 100);
expect(selections.delay).not.toHaveBeenCalled();
});
});
});
describe('units', function () {
describe('.integers', function () {
var result;
beforeEach(function () {
result = dc.units.integers(0, 100);
});
it('units should be based on subtraction', function () {
expect(result).toEqual(100);
});
});
describe('.float', function () {
var result;
beforeEach(function () {
result = dc.units.fp.precision(0.001)(0.49999, 1.0);
});
it('units should be generated according to the precision', function () {
expect(result).toEqual(501);
});
});
describe('.ordinal', function () {
var result;
beforeEach(function () {
result = dc.units.ordinal('a', 'd', ['a', 'b', 'c', 'd']);
});
it('units should be based on count', function () {
expect(result.length).toEqual(4);
});
});
});
describe('round', function () {
describe('.floor', function () {
var result;
beforeEach(function () {
result = dc.round.floor(0.33);
});
it('should floored number', function () {
expect(result).toEqual(0);
});
});
});
describe('override', function () {
var o;
beforeEach(function () {
o = {
foo: function () {
return 'foo';
},
goo: function (i) {
return i;
}
};
});
it('wo/ override function should work as expected', function () {
expect(o.foo()).toEqual('foo');
});
it('should expose existing function', function () {
dc.override(o, 'foo', function () {
return this._foo() + '2';
});
expect(o.foo()).toEqual('foo2');
});
it('should expose existing function with args', function () {
dc.override(o, 'goo', function (i) {
return this._goo(i) + 2;
});
expect(o.goo(1)).toEqual(3);
});
});
describe('charts w/ grouping', function () {
var chart;
beforeEach(function () {
chart = dc.pieChart('#a', 'groupA').dimension(valueDimension).group(valueGroup).filter(0);
spyOn(chart, 'filterAll');
spyOn(chart, 'render');
dc.pieChart('#b', 'groupA').dimension(valueDimension).group(valueGroup).filter(0);
dc.bubbleChart('#c', 'groupB').dimension(valueDimension).group(valueGroup).filter(0);
dc.barChart('#b1', 'groupB').dimension(valueDimension).group(valueGroup).filter(0);
dc.lineChart('#b2', 'groupB').dimension(valueDimension).group(valueGroup).filter(0);
dc.dataCount('#b3', 'groupB').dimension(valueDimension).group(valueGroup).filter(0);
dc.dataTable('#b4', 'groupB').dimension(valueDimension).group(valueGroup).filter(0);
return chart;
});
it('should register chart object', function () {
expect(dc.hasChart(chart)).toBeTruthy();
});
it('filterAll by group should invoke filter on each chart within the group', function () {
dc.filterAll('groupA');
expect(chart.filterAll).toHaveBeenCalled();
});
it('renderAll by group should invoke filter on each chart within the group', function () {
dc.renderAll('groupA');
expect(chart.render).toHaveBeenCalled();
});
it('filterAll should not invoke filter on chart in groupA', function () {
dc.filterAll();
expect(chart.filterAll).not.toHaveBeenCalled();
});
it('renderAll should not invoke filter on chart in groupA', function () {
dc.renderAll();
expect(chart.render).not.toHaveBeenCalled();
});
it('should be gone after remove all', function () {
dc.deregisterAllCharts();
expect(dc.hasChart(chart)).toBeFalsy();
});
});
describe('render/redraw all call back', function () {
var result;
beforeEach(function () {
dc.renderlet(function (group) {
result.called = group ? group : true;
});
result = {called: false};
});
it('renderAll call back should be triggered', function () {
dc.renderAll();
expect(result.called).toBeTruthy();
});
it('redrawAll call back should be triggered', function () {
dc.redrawAll();
expect(result.called).toBeTruthy();
});
it('renderAll by group call back should be triggered', function () {
dc.renderAll('group');
expect('group').toEqual(result.called);
});
it('redrawAll by group call back should be triggered', function () {
dc.redrawAll('group');
expect('group').toEqual(result.called);
});
});
});