forked from GoogleWebComponents/google-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-tests.html
255 lines (243 loc) · 8.02 KB
/
basic-tests.html
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
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at https://polymer.github.io/LICENSE.txt
The complete set of authors may be found at https://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at https://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at https://polymer.github.io/PATENTS.txt
-->
<!doctype html>
<html>
<head>
<script src="../../webcomponentsjs/webcomponents.min.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<link rel="import" href="../google-chart.html">
</head>
<body>
<test-fixture id="chart-fixture">
<template>
<google-chart></google-chart>
</template>
</test-fixture>
<script>
suite('<google-chart>', function() {
var chart;
setup(function() {
chart = fixture('chart-fixture');
});
var waitCheckAndDone = function(check, done) {
setTimeout(function() {
if (check()) {
chart.debounce('done', done, 100);
} else {
waitCheckAndDone(check, done);
}
}, 50);
};
suite('Default Functionality', function() {
setup(function() {
chart.data = [ ['Data', 'Value'], ['Something', 1] ];
});
test('fires google-chart-ready event for initial load', function(done) {
chart.addEventListener('google-chart-ready', function() {
assert.isTrue(chart.drawn);
done();
});
});
test('fires google-chart-ready event for redraw call', function(done) {
var drawCount = 0;
chart.addEventListener('google-chart-ready', function() {
assert.isTrue(chart.drawn);
++drawCount;
if (drawCount == 4) { done(); }
else chart.redraw();
});
});
test('default type is column', function() {
assert.equal(chart.type, 'column');
});
test('can change type', function(done) {
chart.type = 'line';
waitCheckAndDone(function() {
// A circle indicates the chart type change was drawn:
return chart.$$('circle');
}, done);
});
test('default selection is null', function() {
assert.equal(chart.selection, null);
});
test('can change selection', function(done) {
chart.selection = [ {row: 0} ];
waitCheckAndDone(function() {
// A white stroked rectangle signals the selection was drawn:
return chart.$$('rect[stroke="#ffffff"]');
}, done);
});
test('default options are null', function() {
assert.equal(chart.options, null);
});
test('can change options', function(done) {
var expectedTitle = 'New Title';
var initialDraw = true;
chart.addEventListener('google-chart-ready', function() {
if (initialDraw) {
initialDraw = false;
chart.options = {'title': expectedTitle};
} else {
assert.equal(chart.$$('text').innerHTML, expectedTitle);
done();
}
});
});
test('can change deep options', function(done) {
chart.options = {'title': 'Old Title'};
var spyRedraw = sinon.spy(chart, 'redraw');
var expectedTitle = 'New Title';
var initialDraw = true;
chart.addEventListener('google-chart-ready', function() {
if (initialDraw) {
initialDraw = false;
chart.set('options.title', 'Debounced Title');
chart.set('options.title', expectedTitle);
assert.isFalse(spyRedraw.called);
} else {
assert.equal(chart.$$('text').innerHTML, expectedTitle);
assert.isTrue(spyRedraw.calledOnce);
chart.redraw.restore();
done();
}
});
});
test('creates png chart uri', function (done) {
chart.addEventListener('google-chart-ready', function(event) {
var uri = chart.imageURI;
assert.isString(uri);
assert.match(uri, /^data:image\/png;base64/, 'png regexp matches');
done();
});
});
test('can render multiple instances', function (done) {
var secondChart = document.createElement('google-chart');
secondChart.data = [ ['Data', 'Value'], ['Something', 1] ];
// Ensure second chart is rendered. Clean up test.
secondChart.addEventListener('google-chart-ready', function() {
document.body.removeChild(secondChart);
done();
});
document.body.appendChild(secondChart);
});
});
suite('Events', function() {
setup(function() {
chart.data = [ ['Data', 'Value'], ['Something', 1] ];
});
test('can be added', function(done) {
chart.events = ['onmouseover'];
chart.addEventListener('google-chart-ready', function() {
google.visualization.events.trigger(
chart._chart, 'onmouseover', {'row': 1, 'column': 5});
});
chart.addEventListener('google-chart-onmouseover', function(e) {
assert.equal(e.detail.data.row, 1);
assert.equal(e.detail.data.column, 5);
done();
});
});
});
suite('Data Source Types', function() {
test('[rows] and [cols]', function (done) {
chart.cols = [
{'label': 'Data', 'type': 'string'},
{'label': 'Value', 'type': 'number'}
];
chart.rows = [
['Something', 1]
];
chart.addEventListener('google-chart-ready', function() {
done();
});
});
test('[rows] and [cols] with date string repr is broken', function(done) {
chart.cols = [ { 'type': 'date' } ];
chart.rows = [ ['Date(1789, 3, 30)'] ];
waitCheckAndDone(function() {
return chart.$$('#chartdiv').innerHTML ==
'Error: Type mismatch. Value Date(1789, 3, 30) ' +
'does not match type date in column index 0';
}, done);
});
var setDataAndWaitForRender = function(data, done) {
chart.data = data;
chart.addEventListener('google-chart-ready', function() {
done();
});
};
test('[data] is 2D Array', function(done) {
setDataAndWaitForRender([ ['Data', 'Value'], ['Something', 1] ], done);
});
test('[data] is DataTable Object format', function(done) {
setDataAndWaitForRender({
'cols': [
{'label': 'Data', 'type': 'string'},
{'label': 'Value', 'type': 'number'}
],
'rows': [
{'c': ['Someting', 1]}
]
}, done);
});
test('[data] is DataTable', function(done) {
chart.addEventListener('google-chart-ready', function() {
done();
});
var loader = document.createElement('google-chart-loader');
loader.dataTable([
['Data', 'Value'],
['Something', 1]
]).then(function(dataTable) {
chart.data = dataTable;
});
});
test('[data] is DataTable from DataSource Query', function(done) {
chart.addEventListener('google-chart-ready', function() {
done();
});
document.createElement('google-chart-loader')
.query('query.json')
.then(function(q) {
q.send(function(res) {
chart.data = res.getDataTable();
});
});
});
test('[data] is JSON URL for 2D Array', function(done) {
setDataAndWaitForRender('test-data-array.json', done);
});
test('[data] is JSON URL for DataTable Object format', function(done) {
setDataAndWaitForRender('test-data-object.json', done);
});
test('[view] is DataView', function(done) {
chart.addEventListener('google-chart-ready', function() {
done();
});
var loader = document.createElement('google-chart-loader');
loader.dataTable([
['Data', 'Value'],
['Something', 1]
])
.then(loader.dataView.bind(loader))
.then(function(dataView) {
chart.view = dataView;
});
});
test('multiple calls to JSON URL', function(done) {
setDataAndWaitForRender('test-data-array.json', function() {
setDataAndWaitForRender('test-data-object.json', done);
});
});
});
});
</script>
</body>
</html>