2 * @fileoverview Tests for stand-alone functions in dygraph-utils.js
4 * @author danvdk@gmail.com (Dan Vanderkam)
7 import * as utils from
'../../src/dygraph-utils';
9 describe("utils-tests", function() {
11 it('testUpdate', function() {
18 assert
.equal(1, a
['a']);
19 assert
.deepEqual([1, 2, 3], a
['b']);
20 assert
.deepEqual({x
: 1, y
: 2}, a
['c']);
21 assert
.deepEqual({f
: 10, g
: 20}, a
['d']);
23 utils
.update(a
, { c
: { x
: 2 } });
24 assert
.deepEqual({x
: 2}, a
['c']);
26 utils
.update(a
, { d
: null });
27 assert
.equal(null, a
['d']);
29 utils
.update(a
, { a
: 10, b
: [1, 2] });
30 assert
.equal(10, a
['a']);
31 assert
.deepEqual([1, 2], a
['b']);
32 assert
.deepEqual({x
: 2}, a
['c']);
33 assert
.equal(null, a
['d']);
36 it('testUpdateDeep', function() {
43 assert
.equal(1, a
['a']);
44 assert
.deepEqual([1, 2, 3], a
['b']);
45 assert
.deepEqual({x
: 1, y
: 2}, a
['c']);
46 assert
.deepEqual({f
: 10, g
: 20}, a
['d']);
48 utils
.updateDeep(a
, { c
: { x
: 2 } });
49 assert
.deepEqual({x
: 2, y
: 2}, a
['c']);
51 utils
.updateDeep(a
, { d
: null });
52 assert
.equal(null, a
['d']);
54 utils
.updateDeep(a
, { a
: 10, b
: [1, 2] });
55 assert
.equal(10, a
['a']);
56 assert
.deepEqual([1, 2], a
['b']);
57 assert
.deepEqual({x
: 2, y
: 2}, a
['c']);
58 assert
.equal(null, a
['d']);
61 it('testUpdateDeepDecoupled', function() {
65 c
: { x
: "original", y
: 2},
69 utils
.updateDeep(b
, a
);
75 assert
.equal(1, a
.b
[0]);
78 assert
.equal("original", a
.c
.x
);
82 it('testIterator_nopredicate', function() {
83 var array
= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
84 var iter
= utils
.createIterator(array
, 1, 4);
85 assert
.isTrue(iter
.hasNext
);
86 assert
.equal('b', iter
.peek
);
87 assert
.equal('b', iter
.next());
88 assert
.isTrue(iter
.hasNext
);
90 assert
.equal('c', iter
.peek
);
91 assert
.equal('c', iter
.next());
93 assert
.isTrue(iter
.hasNext
);
94 assert
.equal('d', iter
.next());
96 assert
.isTrue(iter
.hasNext
);
97 assert
.equal('e', iter
.next());
99 assert
.isFalse(iter
.hasNext
);
102 it('testIterator_predicate', function() {
103 var array
= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
104 var iter
= utils
.createIterator(array
, 1, 4,
105 function(array
, idx
) { return array
[idx
] !== 'd' });
106 assert
.isTrue(iter
.hasNext
);
107 assert
.equal('b', iter
.peek
);
108 assert
.equal('b', iter
.next());
109 assert
.isTrue(iter
.hasNext
);
111 assert
.equal('c', iter
.peek
);
112 assert
.equal('c', iter
.next());
114 assert
.isTrue(iter
.hasNext
);
115 assert
.equal('e', iter
.next());
117 assert
.isFalse(iter
.hasNext
);
120 it('testIterator_empty', function() {
122 var iter
= utils
.createIterator([], 0, 0);
123 assert
.isFalse(iter
.hasNext
);
126 it('testIterator_outOfRange', function() {
127 var array
= ['a', 'b', 'c'];
128 var iter
= utils
.createIterator(array
, 1, 4,
129 function(array
, idx
) { return array
[idx
] !== 'd' });
130 assert
.isTrue(iter
.hasNext
);
131 assert
.equal('b', iter
.peek
);
132 assert
.equal('b', iter
.next());
133 assert
.isTrue(iter
.hasNext
);
135 assert
.equal('c', iter
.peek
);
136 assert
.equal('c', iter
.next());
138 assert
.isFalse(iter
.hasNext
);
141 // Makes sure full array is tested, and that the predicate isn't called
142 // with invalid boundaries.
143 it('testIterator_whole_array', function() {
144 var array
= ['a', 'b', 'c'];
145 var iter
= utils
.createIterator(array
, 0, array
.length
,
146 function(array
, idx
) {
147 if (idx
< 0 || idx
>= array
.length
) {
153 assert
.isTrue(iter
.hasNext
);
154 assert
.equal('a', iter
.next());
155 assert
.isTrue(iter
.hasNext
);
156 assert
.equal('b', iter
.next());
157 assert
.isTrue(iter
.hasNext
);
158 assert
.equal('c', iter
.next());
159 assert
.isFalse(iter
.hasNext
);
160 assert
.isNull(iter
.next());
163 it('testIterator_no_args', function() {
164 var array
= ['a', 'b', 'c'];
165 var iter
= utils
.createIterator(array
);
166 assert
.isTrue(iter
.hasNext
);
167 assert
.equal('a', iter
.next());
168 assert
.isTrue(iter
.hasNext
);
169 assert
.equal('b', iter
.next());
170 assert
.isTrue(iter
.hasNext
);
171 assert
.equal('c', iter
.next());
172 assert
.isFalse(iter
.hasNext
);
173 assert
.isNull(iter
.next());
176 it('testToRGB', function() {
177 assert
.deepEqual({r
: 255, g
: 200, b
: 150}, utils
.toRGB_('rgb(255,200,150)'));
178 assert
.deepEqual({r
: 255, g
: 200, b
: 150}, utils
.toRGB_('#FFC896'));
179 assert
.deepEqual({r
: 255, g
: 0, b
: 0}, utils
.toRGB_('red'));
180 assert
.deepEqual({r
: 255, g
: 200, b
: 150, a
: 0.6},
181 utils
.toRGB_('rgba(255, 200, 150, 0.6)'));
184 it('testIsPixelChangingOptionList', function() {
185 var isPx
= utils
.isPixelChangingOptionList
;
186 assert
.isTrue(isPx([], { axes
: { y
: { digitsAfterDecimal
: 3 }}}));
187 assert
.isFalse(isPx([], { axes
: { y
: { axisLineColor
: 'blue' }}}));
191 it('testDateSet', function() {
192 var base = new Date(1383455100000);
193 var d = new Date(base);
195 // A one hour shift -- this is surprising behavior!
196 d.setMilliseconds(10);
197 assert.equal(3600010, d.getTime() - base.getTime());
199 // setDateSameTZ compensates for this surprise.
201 Dygraph.setDateSameTZ(d, {ms: 10});
202 assert.equal(10, d.getTime() - base.getTime());