Fork off Karma file for in-browser debug
[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
89fdcedb 7describe("utils-tests", function() {
48e614ac 8
89fdcedb 9it('testUpdate', function() {
48e614ac
DV
10 var a = {
11 a: 1,
12 b: [1, 2, 3],
13 c: { x: 1, y: 2},
14 d: { f: 10, g: 20}
15 };
89fdcedb
DV
16 assert.equal(1, a['a']);
17 assert.deepEqual([1, 2, 3], a['b']);
18 assert.deepEqual({x: 1, y: 2}, a['c']);
19 assert.deepEqual({f: 10, g: 20}, a['d']);
48e614ac
DV
20
21 Dygraph.update(a, { c: { x: 2 } });
89fdcedb 22 assert.deepEqual({x: 2}, a['c']);
48e614ac
DV
23
24 Dygraph.update(a, { d: null });
89fdcedb 25 assert.equal(null, a['d']);
48e614ac
DV
26
27 Dygraph.update(a, { a: 10, b: [1, 2] });
89fdcedb
DV
28 assert.equal(10, a['a']);
29 assert.deepEqual([1, 2], a['b']);
30 assert.deepEqual({x: 2}, a['c']);
31 assert.equal(null, a['d']);
32});
48e614ac 33
89fdcedb 34it('testUpdateDeep', function() {
48e614ac
DV
35 var a = {
36 a: 1,
37 b: [1, 2, 3],
38 c: { x: 1, y: 2},
39 d: { f: 10, g: 20}
40 };
89fdcedb
DV
41 assert.equal(1, a['a']);
42 assert.deepEqual([1, 2, 3], a['b']);
43 assert.deepEqual({x: 1, y: 2}, a['c']);
44 assert.deepEqual({f: 10, g: 20}, a['d']);
48e614ac
DV
45
46 Dygraph.updateDeep(a, { c: { x: 2 } });
89fdcedb 47 assert.deepEqual({x: 2, y: 2}, a['c']);
48e614ac
DV
48
49 Dygraph.updateDeep(a, { d: null });
89fdcedb 50 assert.equal(null, a['d']);
48e614ac
DV
51
52 Dygraph.updateDeep(a, { a: 10, b: [1, 2] });
89fdcedb
DV
53 assert.equal(10, a['a']);
54 assert.deepEqual([1, 2], a['b']);
55 assert.deepEqual({x: 2, y: 2}, a['c']);
56 assert.equal(null, a['d']);
57});
48e614ac 58
89fdcedb 59it('testUpdateDeepDecoupled', function() {
48e614ac
DV
60 var a = {
61 a: 1,
62 b: [1, 2, 3],
63 c: { x: "original", y: 2},
64 };
65
66 var b = {};
67 Dygraph.updateDeep(b, a);
68
69 b.a = 2;
89fdcedb 70 assert.equal(1, a.a);
48e614ac
DV
71
72 b.b[0] = 2;
89fdcedb 73 assert.equal(1, a.b[0]);
48e614ac
DV
74
75 b.c.x = "new value";
89fdcedb
DV
76 assert.equal("original", a.c.x);
77});
7d1afbb9
RK
78
79
89fdcedb 80it('testIterator_nopredicate', function() {
7d1afbb9
RK
81 var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
82 var iter = Dygraph.createIterator(array, 1, 4);
89fdcedb
DV
83 assert.isTrue(iter.hasNext);
84 assert.equal('b', iter.peek);
85 assert.equal('b', iter.next());
86 assert.isTrue(iter.hasNext);
7d1afbb9 87
89fdcedb
DV
88 assert.equal('c', iter.peek);
89 assert.equal('c', iter.next());
7d1afbb9 90
89fdcedb
DV
91 assert.isTrue(iter.hasNext);
92 assert.equal('d', iter.next());
7d1afbb9 93
89fdcedb
DV
94 assert.isTrue(iter.hasNext);
95 assert.equal('e', iter.next());
7d1afbb9 96
89fdcedb
DV
97 assert.isFalse(iter.hasNext);
98});
7d1afbb9 99
89fdcedb 100it('testIterator_predicate', function() {
7d1afbb9
RK
101 var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
102 var iter = Dygraph.createIterator(array, 1, 4,
103 function(array, idx) { return array[idx] !== 'd' });
89fdcedb
DV
104 assert.isTrue(iter.hasNext);
105 assert.equal('b', iter.peek);
106 assert.equal('b', iter.next());
107 assert.isTrue(iter.hasNext);
7d1afbb9 108
89fdcedb
DV
109 assert.equal('c', iter.peek);
110 assert.equal('c', iter.next());
7d1afbb9 111
89fdcedb
DV
112 assert.isTrue(iter.hasNext);
113 assert.equal('e', iter.next());
7d1afbb9 114
89fdcedb
DV
115 assert.isFalse(iter.hasNext);
116});
7d1afbb9 117
89fdcedb 118it('testIterator_empty', function() {
7d1afbb9
RK
119 var array = [];
120 var iter = Dygraph.createIterator([], 0, 0);
89fdcedb
DV
121 assert.isFalse(iter.hasNext);
122});
7d1afbb9 123
89fdcedb 124it('testIterator_outOfRange', function() {
7d1afbb9
RK
125 var array = ['a', 'b', 'c'];
126 var iter = Dygraph.createIterator(array, 1, 4,
127 function(array, idx) { return array[idx] !== 'd' });
89fdcedb
DV
128 assert.isTrue(iter.hasNext);
129 assert.equal('b', iter.peek);
130 assert.equal('b', iter.next());
131 assert.isTrue(iter.hasNext);
7d1afbb9 132
89fdcedb
DV
133 assert.equal('c', iter.peek);
134 assert.equal('c', iter.next());
7d1afbb9 135
89fdcedb
DV
136 assert.isFalse(iter.hasNext);
137});
7d1afbb9
RK
138
139// Makes sure full array is tested, and that the predicate isn't called
140// with invalid boundaries.
89fdcedb 141it('testIterator_whole_array', function() {
7d1afbb9
RK
142 var array = ['a', 'b', 'c'];
143 var iter = Dygraph.createIterator(array, 0, array.length,
144 function(array, idx) {
145 if (idx < 0 || idx >= array.length) {
146 throw "err";
147 } else {
148 return true;
89fdcedb 149 }
199fd784 150 });
89fdcedb
DV
151 assert.isTrue(iter.hasNext);
152 assert.equal('a', iter.next());
153 assert.isTrue(iter.hasNext);
154 assert.equal('b', iter.next());
155 assert.isTrue(iter.hasNext);
156 assert.equal('c', iter.next());
157 assert.isFalse(iter.hasNext);
158 assert.isNull(iter.next());
159});
160
161it('testIterator_no_args', function() {
64885539
RK
162 var array = ['a', 'b', 'c'];
163 var iter = Dygraph.createIterator(array);
89fdcedb
DV
164 assert.isTrue(iter.hasNext);
165 assert.equal('a', iter.next());
166 assert.isTrue(iter.hasNext);
167 assert.equal('b', iter.next());
168 assert.isTrue(iter.hasNext);
169 assert.equal('c', iter.next());
170 assert.isFalse(iter.hasNext);
171 assert.isNull(iter.next());
172});
173
174it('testToRGB', function() {
175 assert.deepEqual({r: 255, g: 200, b: 150}, Dygraph.toRGB_('rgb(255,200,150)'));
176 assert.deepEqual({r: 255, g: 200, b: 150}, Dygraph.toRGB_('#FFC896'));
177 assert.deepEqual({r: 255, g: 0, b: 0}, Dygraph.toRGB_('red'));
178});
179
180it('testIsPixelChangingOptionList', function() {
8887663f 181 var isPx = Dygraph.isPixelChangingOptionList;
89fdcedb
DV
182 assert.isTrue(isPx([], { axes: { y: { digitsAfterDecimal: 3 }}}));
183 assert.isFalse(isPx([], { axes: { y: { axisLineColor: 'blue' }}}));
184});
8887663f 185
ace524b5 186/*
89fdcedb 187it('testDateSet', function() {
9a4fd029
DV
188 var base = new Date(1383455100000);
189 var d = new Date(base);
190
191 // A one hour shift -- this is surprising behavior!
192 d.setMilliseconds(10);
89fdcedb 193 assert.equal(3600010, d.getTime() - base.getTime());
9a4fd029
DV
194
195 // setDateSameTZ compensates for this surprise.
196 d = new Date(base);
197 Dygraph.setDateSameTZ(d, {ms: 10});
89fdcedb
DV
198 assert.equal(10, d.getTime() - base.getTime());
199});
ace524b5 200*/
89fdcedb
DV
201
202});