consolidating scripts
[dygraphs.git] / auto_tests / tests / utils_test.js
CommitLineData
48e614ac
DV
1/**
2 * @fileoverview Tests for stand-alone functions in dygraph-utils.js
3 *
4 * @author danvdk@gmail.com (Dan Vanderkam)
5 */
6
e8c70e4e
DV
7import * as utils from '../../src/dygraph-utils';
8
89fdcedb 9describe("utils-tests", function() {
48e614ac 10
89fdcedb 11it('testUpdate', function() {
48e614ac
DV
12 var a = {
13 a: 1,
14 b: [1, 2, 3],
15 c: { x: 1, y: 2},
16 d: { f: 10, g: 20}
17 };
89fdcedb
DV
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']);
48e614ac 22
e8c70e4e 23 utils.update(a, { c: { x: 2 } });
89fdcedb 24 assert.deepEqual({x: 2}, a['c']);
48e614ac 25
e8c70e4e 26 utils.update(a, { d: null });
89fdcedb 27 assert.equal(null, a['d']);
48e614ac 28
e8c70e4e 29 utils.update(a, { a: 10, b: [1, 2] });
89fdcedb
DV
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']);
34});
48e614ac 35
89fdcedb 36it('testUpdateDeep', function() {
48e614ac
DV
37 var a = {
38 a: 1,
39 b: [1, 2, 3],
40 c: { x: 1, y: 2},
41 d: { f: 10, g: 20}
42 };
89fdcedb
DV
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']);
48e614ac 47
e8c70e4e 48 utils.updateDeep(a, { c: { x: 2 } });
89fdcedb 49 assert.deepEqual({x: 2, y: 2}, a['c']);
48e614ac 50
e8c70e4e 51 utils.updateDeep(a, { d: null });
89fdcedb 52 assert.equal(null, a['d']);
48e614ac 53
e8c70e4e 54 utils.updateDeep(a, { a: 10, b: [1, 2] });
89fdcedb
DV
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']);
59});
48e614ac 60
89fdcedb 61it('testUpdateDeepDecoupled', function() {
48e614ac
DV
62 var a = {
63 a: 1,
64 b: [1, 2, 3],
65 c: { x: "original", y: 2},
66 };
67
68 var b = {};
e8c70e4e 69 utils.updateDeep(b, a);
48e614ac
DV
70
71 b.a = 2;
89fdcedb 72 assert.equal(1, a.a);
48e614ac
DV
73
74 b.b[0] = 2;
89fdcedb 75 assert.equal(1, a.b[0]);
48e614ac
DV
76
77 b.c.x = "new value";
89fdcedb
DV
78 assert.equal("original", a.c.x);
79});
7d1afbb9
RK
80
81
89fdcedb 82it('testIterator_nopredicate', function() {
7d1afbb9 83 var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
e8c70e4e 84 var iter = utils.createIterator(array, 1, 4);
89fdcedb
DV
85 assert.isTrue(iter.hasNext);
86 assert.equal('b', iter.peek);
87 assert.equal('b', iter.next());
88 assert.isTrue(iter.hasNext);
7d1afbb9 89
89fdcedb
DV
90 assert.equal('c', iter.peek);
91 assert.equal('c', iter.next());
7d1afbb9 92
89fdcedb
DV
93 assert.isTrue(iter.hasNext);
94 assert.equal('d', iter.next());
7d1afbb9 95
89fdcedb
DV
96 assert.isTrue(iter.hasNext);
97 assert.equal('e', iter.next());
7d1afbb9 98
89fdcedb
DV
99 assert.isFalse(iter.hasNext);
100});
7d1afbb9 101
89fdcedb 102it('testIterator_predicate', function() {
7d1afbb9 103 var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
e8c70e4e 104 var iter = utils.createIterator(array, 1, 4,
7d1afbb9 105 function(array, idx) { return array[idx] !== 'd' });
89fdcedb
DV
106 assert.isTrue(iter.hasNext);
107 assert.equal('b', iter.peek);
108 assert.equal('b', iter.next());
109 assert.isTrue(iter.hasNext);
7d1afbb9 110
89fdcedb
DV
111 assert.equal('c', iter.peek);
112 assert.equal('c', iter.next());
7d1afbb9 113
89fdcedb
DV
114 assert.isTrue(iter.hasNext);
115 assert.equal('e', iter.next());
7d1afbb9 116
89fdcedb
DV
117 assert.isFalse(iter.hasNext);
118});
7d1afbb9 119
89fdcedb 120it('testIterator_empty', function() {
7d1afbb9 121 var array = [];
e8c70e4e 122 var iter = utils.createIterator([], 0, 0);
89fdcedb
DV
123 assert.isFalse(iter.hasNext);
124});
7d1afbb9 125
89fdcedb 126it('testIterator_outOfRange', function() {
7d1afbb9 127 var array = ['a', 'b', 'c'];
e8c70e4e 128 var iter = utils.createIterator(array, 1, 4,
7d1afbb9 129 function(array, idx) { return array[idx] !== 'd' });
89fdcedb
DV
130 assert.isTrue(iter.hasNext);
131 assert.equal('b', iter.peek);
132 assert.equal('b', iter.next());
133 assert.isTrue(iter.hasNext);
7d1afbb9 134
89fdcedb
DV
135 assert.equal('c', iter.peek);
136 assert.equal('c', iter.next());
7d1afbb9 137
89fdcedb
DV
138 assert.isFalse(iter.hasNext);
139});
7d1afbb9
RK
140
141// Makes sure full array is tested, and that the predicate isn't called
142// with invalid boundaries.
89fdcedb 143it('testIterator_whole_array', function() {
7d1afbb9 144 var array = ['a', 'b', 'c'];
e8c70e4e 145 var iter = utils.createIterator(array, 0, array.length,
7d1afbb9
RK
146 function(array, idx) {
147 if (idx < 0 || idx >= array.length) {
148 throw "err";
149 } else {
150 return true;
89fdcedb 151 }
199fd784 152 });
89fdcedb
DV
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());
161});
162
163it('testIterator_no_args', function() {
64885539 164 var array = ['a', 'b', 'c'];
e8c70e4e 165 var iter = utils.createIterator(array);
89fdcedb
DV
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());
174});
175
176it('testToRGB', function() {
e8c70e4e
DV
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'));
337a79bc 180 assert.deepEqual({r: 255, g: 200, b: 150, a: 0.6},
e8c70e4e 181 utils.toRGB_('rgba(255, 200, 150, 0.6)'));
89fdcedb
DV
182});
183
184it('testIsPixelChangingOptionList', function() {
e8c70e4e 185 var isPx = utils.isPixelChangingOptionList;
89fdcedb
DV
186 assert.isTrue(isPx([], { axes: { y: { digitsAfterDecimal: 3 }}}));
187 assert.isFalse(isPx([], { axes: { y: { axisLineColor: 'blue' }}}));
188});
8887663f 189
ace524b5 190/*
89fdcedb 191it('testDateSet', function() {
9a4fd029
DV
192 var base = new Date(1383455100000);
193 var d = new Date(base);
194
195 // A one hour shift -- this is surprising behavior!
196 d.setMilliseconds(10);
89fdcedb 197 assert.equal(3600010, d.getTime() - base.getTime());
9a4fd029
DV
198
199 // setDateSameTZ compensates for this surprise.
200 d = new Date(base);
201 Dygraph.setDateSameTZ(d, {ms: 10});
89fdcedb
DV
202 assert.equal(10, d.getTime() - base.getTime());
203});
ace524b5 204*/
89fdcedb
DV
205
206});