Don't clear overlay on mouseup if not zooming.
[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
7var UtilsTestCase = TestCase("utils-tests");
8
9UtilsTestCase.prototype.testUpdate = function() {
10 var a = {
11 a: 1,
12 b: [1, 2, 3],
13 c: { x: 1, y: 2},
14 d: { f: 10, g: 20}
15 };
16 assertEquals(1, a['a']);
17 assertEquals([1, 2, 3], a['b']);
18 assertEquals({x: 1, y: 2}, a['c']);
19 assertEquals({f: 10, g: 20}, a['d']);
20
21 Dygraph.update(a, { c: { x: 2 } });
22 assertEquals({x: 2}, a['c']);
23
24 Dygraph.update(a, { d: null });
25 assertEquals(null, a['d']);
26
27 Dygraph.update(a, { a: 10, b: [1, 2] });
28 assertEquals(10, a['a']);
29 assertEquals([1, 2], a['b']);
30 assertEquals({x: 2}, a['c']);
31 assertEquals(null, a['d']);
32};
33
34UtilsTestCase.prototype.testUpdateDeep = function() {
35 var a = {
36 a: 1,
37 b: [1, 2, 3],
38 c: { x: 1, y: 2},
39 d: { f: 10, g: 20}
40 };
41 assertEquals(1, a['a']);
42 assertEquals([1, 2, 3], a['b']);
43 assertEquals({x: 1, y: 2}, a['c']);
44 assertEquals({f: 10, g: 20}, a['d']);
45
46 Dygraph.updateDeep(a, { c: { x: 2 } });
47 assertEquals({x: 2, y: 2}, a['c']);
48
49 Dygraph.updateDeep(a, { d: null });
50 assertEquals(null, a['d']);
51
52 Dygraph.updateDeep(a, { a: 10, b: [1, 2] });
53 assertEquals(10, a['a']);
54 assertEquals([1, 2], a['b']);
55 assertEquals({x: 2, y: 2}, a['c']);
56 assertEquals(null, a['d']);
57};
58
59UtilsTestCase.prototype.testUpdateDeepDecoupled = function() {
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;
70 assertEquals(1, a.a);
71
72 b.b[0] = 2;
73 assertEquals(1, a.b[0]);
74
75 b.c.x = "new value";
76 assertEquals("original", a.c.x);
77};
7d1afbb9
RK
78
79
80UtilsTestCase.prototype.testIterator_nopredicate = function() {
81 var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
82 var iter = Dygraph.createIterator(array, 1, 4);
ff1074cd
RK
83 assertTrue(iter.hasNext);
84 assertEquals('b', iter.peek);
7d1afbb9 85 assertEquals('b', iter.next());
ff1074cd 86 assertTrue(iter.hasNext);
7d1afbb9 87
ff1074cd 88 assertEquals('c', iter.peek);
7d1afbb9
RK
89 assertEquals('c', iter.next());
90
ff1074cd 91 assertTrue(iter.hasNext);
7d1afbb9
RK
92 assertEquals('d', iter.next());
93
ff1074cd 94 assertTrue(iter.hasNext);
7d1afbb9
RK
95 assertEquals('e', iter.next());
96
ff1074cd 97 assertFalse(iter.hasNext);
7d1afbb9
RK
98}
99
100UtilsTestCase.prototype.testIterator_predicate = function() {
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' });
ff1074cd
RK
104 assertTrue(iter.hasNext);
105 assertEquals('b', iter.peek);
7d1afbb9 106 assertEquals('b', iter.next());
ff1074cd 107 assertTrue(iter.hasNext);
7d1afbb9 108
ff1074cd 109 assertEquals('c', iter.peek);
7d1afbb9
RK
110 assertEquals('c', iter.next());
111
ff1074cd 112 assertTrue(iter.hasNext);
7d1afbb9
RK
113 assertEquals('e', iter.next());
114
ff1074cd 115 assertFalse(iter.hasNext);
7d1afbb9
RK
116}
117
118UtilsTestCase.prototype.testIterator_empty = function() {
119 var array = [];
120 var iter = Dygraph.createIterator([], 0, 0);
ff1074cd 121 assertFalse(iter.hasNext);
7d1afbb9
RK
122}
123
124UtilsTestCase.prototype.testIterator_outOfRange = function() {
125 var array = ['a', 'b', 'c'];
126 var iter = Dygraph.createIterator(array, 1, 4,
127 function(array, idx) { return array[idx] !== 'd' });
ff1074cd
RK
128 assertTrue(iter.hasNext);
129 assertEquals('b', iter.peek);
7d1afbb9 130 assertEquals('b', iter.next());
ff1074cd 131 assertTrue(iter.hasNext);
7d1afbb9 132
ff1074cd 133 assertEquals('c', iter.peek);
7d1afbb9
RK
134 assertEquals('c', iter.next());
135
ff1074cd 136 assertFalse(iter.hasNext);
7d1afbb9
RK
137}
138
139// Makes sure full array is tested, and that the predicate isn't called
140// with invalid boundaries.
141UtilsTestCase.prototype.testIterator_whole_array = function() {
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;
199fd784
RK
149 };
150 });
ff1074cd 151 assertTrue(iter.hasNext);
7d1afbb9 152 assertEquals('a', iter.next());
ff1074cd 153 assertTrue(iter.hasNext);
7d1afbb9 154 assertEquals('b', iter.next());
ff1074cd 155 assertTrue(iter.hasNext);
7d1afbb9 156 assertEquals('c', iter.next());
ff1074cd 157 assertFalse(iter.hasNext);
7d1afbb9 158 assertNull(iter.next());
64885539
RK
159}
160
161UtilsTestCase.prototype.testIterator_no_args = function() {
162 var array = ['a', 'b', 'c'];
163 var iter = Dygraph.createIterator(array);
ff1074cd 164 assertTrue(iter.hasNext);
64885539 165 assertEquals('a', iter.next());
ff1074cd 166 assertTrue(iter.hasNext);
64885539 167 assertEquals('b', iter.next());
ff1074cd 168 assertTrue(iter.hasNext);
64885539 169 assertEquals('c', iter.next());
ff1074cd 170 assertFalse(iter.hasNext);
64885539
RK
171 assertNull(iter.next());
172}