Rewrite tests to use ES6 modules.
[dygraphs.git] / auto_tests / tests / Util.js
CommitLineData
fa607ffb
RK
1/**
2 * @fileoverview Utility functions for Dygraphs.
3 *
4 * @author konigsberg@google.com (Robert Konigsberg)
5 */
e8c70e4e
DV
6
7import * as utils from '../../src/dygraph-utils';
8
fa607ffb
RK
9var Util = {};
10
11/**
846038aa
RK
12 * Get the y-labels for a given axis.
13 *
14 * You can specify a parent if more than one graph is in the document.
fa607ffb
RK
15 */
16Util.getYLabels = function(axis_num, parent) {
17 axis_num = axis_num || "";
18 parent = parent || document;
19 var y_labels = parent.getElementsByClassName("dygraph-axis-label-y" + axis_num);
20 var ary = [];
21 for (var i = 0; i < y_labels.length; i++) {
0b85865a 22 ary.push(y_labels[i].innerHTML.replace(/&#160;|&nbsp;/g, ' '));
fa607ffb
RK
23 }
24 return ary;
d574a45e 25};
fa607ffb
RK
26
27/**
846038aa
RK
28 * Get the x-labels for a given axis.
29 *
30 * You can specify a parent if more than one graph is in the document.
31 */
32Util.getXLabels = function(parent) {
33 parent = parent || document;
34 var x_labels = parent.getElementsByClassName("dygraph-axis-label-x");
35 var ary = [];
36 for (var i = 0; i < x_labels.length; i++) {
0b85865a 37 ary.push(x_labels[i].innerHTML.replace(/&#160;|&nbsp;/g, ' '));
846038aa
RK
38 }
39 return ary;
d574a45e 40};
846038aa
RK
41
42/**
fa607ffb
RK
43 * Returns all text in tags w/ a given css class, sorted.
44 * You can specify a parent if more than one graph is on the document.
45 */
46Util.getClassTexts = function(css_class, parent) {
47 parent = parent || document;
48 var texts = [];
49 var els = parent.getElementsByClassName(css_class);
50 for (var i = 0; i < els.length; i++) {
51 texts[i] = els[i].textContent;
52 }
53 texts.sort();
54 return texts;
d574a45e 55};
846038aa 56
457deb39
DV
57// Convert &nbsp; to a normal space
58Util.nbspToSpace = function(str) {
59 var re = new RegExp(String.fromCharCode(160), 'g');
60 return str.replace(re, ' ');
61};
62
846038aa
RK
63Util.getLegend = function(parent) {
64 parent = parent || document;
65 var legend = parent.getElementsByClassName("dygraph-legend")[0];
457deb39 66 return Util.nbspToSpace(legend.textContent);
d574a45e 67};
846038aa 68
d574a45e 69/**
3a84670d 70 * Assert that all elements have a certain style property.
d574a45e 71 */
3a84670d 72Util.assertStyleOfChildren = function(selector, property, expectedValue) {
89fdcedb 73 assert.isTrue(selector.length > 0);
dc910fce
DV
74 for (var idx = 0; idx < selector.length; idx++) {
75 var child = selector[idx];
76 assert.equal(expectedValue, window.getComputedStyle(child)[property]);
77 }
d574a45e 78};
846038aa 79
71a1930b
RK
80
81/**
82 * Takes in an array of strings and returns an array of floats.
83 */
84Util.makeNumbers = function(ary) {
85 var ret = [];
86 for (var i = 0; i < ary.length; i++) {
87 ret.push(parseFloat(ary[i]));
88 }
89 return ret;
901b2ebb 90};
12b879f4
DV
91
92
93/**
94 * Sample a pixel from the canvas.
95 * Returns an [r, g, b, a] tuple where each values is in [0, 255].
e8c70e4e 96 * This is _very_ slow! If you want to sample many pixels, use PixelSampler.
12b879f4
DV
97 */
98Util.samplePixel = function(canvas, x, y) {
99 var ctx = canvas.getContext("2d"); // bypasses Proxy if applied.
100
12b879f4
DV
101 var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
102
e8c70e4e 103 var scale = utils.getContextPixelRatio(ctx);
37819481
PH
104
105 var i = 4 * (x * scale + imageData.width * y * scale);
12b879f4
DV
106 var d = imageData.data;
107 return [d[i], d[i+1], d[i+2], d[i+3]];
108};
5bcc58b4
DV
109
110/**
111 * Overrides the browser's built-in XMLHttpRequest with a mock.
112 * Usage:
113 *
114 * var mockXhr = Util.overrideXMLHttpRequest(your_data);
115 * ... call code that does an XHR ...
116 * mockXhr.respond(); // restores default behavior.
117 * ... do your assertions ...
118 */
119Util.overrideXMLHttpRequest = function(data) {
120 var originalXMLHttpRequest = XMLHttpRequest;
121
122 var requests = [];
123 var FakeXMLHttpRequest = function () {
124 requests.push(this);
125 };
126 FakeXMLHttpRequest.prototype.open = function () {};
127 FakeXMLHttpRequest.prototype.send = function () {
128 this.readyState = 4;
129 this.status = 200;
130 this.responseText = data;
131 };
132 FakeXMLHttpRequest.restore = function() {
89fdcedb 133 window.XMLHttpRequest = originalXMLHttpRequest;
5bcc58b4
DV
134 };
135 FakeXMLHttpRequest.respond = function() {
136 for (var i = 0; i < requests.length; i++) {
137 requests[i].onreadystatechange();
138 }
139 FakeXMLHttpRequest.restore();
140 };
89fdcedb 141 window.XMLHttpRequest = FakeXMLHttpRequest;
5bcc58b4
DV
142 return FakeXMLHttpRequest;
143};
144
464b5f50
DV
145/**
146 * Format a date as 2000/01/23
b7a1dc22 147 * @param {number} dateMillis Millis since epoch.
464b5f50
DV
148 * @return {string} The date formatted as YYYY-MM-DD.
149 */
b7a1dc22 150Util.formatDate = function(dateMillis) {
e8c70e4e 151 return utils.dateString_(dateMillis).slice(0, 10); // 10 == "YYYY/MM/DD".length
464b5f50 152};
1b7afc93
DV
153
154/**
155 * Capture console.{log,warn,error} statements into obj.
156 * obj will look like {log:[], warn:[], error:[]}
157 * This returns a function which will restore the original console.
158 */
159Util.captureConsole = function(obj) {
160 obj.log = [];
161 obj.warn = [];
162 obj.error = [];
163 var orig = [console.log, console.warn, console.error];
164 console.log = function(text) { obj.log.push(text); };
165 console.warn = function(text) { obj.warn.push(text); };
166 console.error = function(text) { obj.error.push(text); };
167
168 return function() {
169 console.log = orig[0];
170 console.warn = orig[1];
171 console.error = orig[2];
172 };
173};
e8c70e4e
DV
174
175export default Util;