6d96e26e1b197b10489e04c6c41e2e941aa71f9e
[dygraphs.git] / auto_tests / tests / utils_test.js
1 /**
2 * @fileoverview Tests for stand-alone functions in dygraph-utils.js
3 *
4 * @author danvdk@gmail.com (Dan Vanderkam)
5 */
6
7 import * as utils from '../../src/dygraph-utils';
8
9 describe("utils-tests", function() {
10
11 it('testUpdate', function() {
12 var a = {
13 a: 1,
14 b: [1, 2, 3],
15 c: { x: 1, y: 2},
16 d: { f: 10, g: 20}
17 };
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']);
22
23 utils.update(a, { c: { x: 2 } });
24 assert.deepEqual({x: 2}, a['c']);
25
26 utils.update(a, { d: null });
27 assert.equal(null, a['d']);
28
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']);
34 });
35
36 it('testUpdateDeep', function() {
37 var a = {
38 a: 1,
39 b: [1, 2, 3],
40 c: { x: 1, y: 2},
41 d: { f: 10, g: 20}
42 };
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']);
47
48 utils.updateDeep(a, { c: { x: 2 } });
49 assert.deepEqual({x: 2, y: 2}, a['c']);
50
51 utils.updateDeep(a, { d: null });
52 assert.equal(null, a['d']);
53
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']);
59 });
60
61 it('testUpdateDeepDecoupled', function() {
62 var a = {
63 a: 1,
64 b: [1, 2, 3],
65 c: { x: "original", y: 2},
66 };
67
68 var b = {};
69 utils.updateDeep(b, a);
70
71 b.a = 2;
72 assert.equal(1, a.a);
73
74 b.b[0] = 2;
75 assert.equal(1, a.b[0]);
76
77 b.c.x = "new value";
78 assert.equal("original", a.c.x);
79 });
80
81
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);
89
90 assert.equal('c', iter.peek);
91 assert.equal('c', iter.next());
92
93 assert.isTrue(iter.hasNext);
94 assert.equal('d', iter.next());
95
96 assert.isTrue(iter.hasNext);
97 assert.equal('e', iter.next());
98
99 assert.isFalse(iter.hasNext);
100 });
101
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);
110
111 assert.equal('c', iter.peek);
112 assert.equal('c', iter.next());
113
114 assert.isTrue(iter.hasNext);
115 assert.equal('e', iter.next());
116
117 assert.isFalse(iter.hasNext);
118 });
119
120 it('testIterator_empty', function() {
121 var array = [];
122 var iter = utils.createIterator([], 0, 0);
123 assert.isFalse(iter.hasNext);
124 });
125
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);
134
135 assert.equal('c', iter.peek);
136 assert.equal('c', iter.next());
137
138 assert.isFalse(iter.hasNext);
139 });
140
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) {
148 throw "err";
149 } else {
150 return true;
151 }
152 });
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
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());
174 });
175
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)'));
182 });
183
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' }}}));
188 });
189
190 /*
191 it('testDateSet', function() {
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);
197 assert.equal(3600010, d.getTime() - base.getTime());
198
199 // setDateSameTZ compensates for this surprise.
200 d = new Date(base);
201 Dygraph.setDateSameTZ(d, {ms: 10});
202 assert.equal(10, d.getTime() - base.getTime());
203 });
204 */
205
206 });