fdc20dc4e1544fa5facd1493ab8080099399f3a4
[dygraphs.git] / auto_tests / tests / synchronize.js
1 /**
2 * @fileoverview Tests synchronizer.
3 *
4 * @author nyx@nyx.cz (Marek Janda)
5 */
6
7 import Dygraph from '../../src/dygraph';
8 import '../../src/extras/synchronizer'; // Sets Dygraph.synchronize
9
10 import DygraphOps from './DygraphOps';
11
12 describe("synchronize", function() {
13 var gs;
14 var originalCallbackCalled;
15 var data = "X,a,b,c\n" +
16 "10,-1,1,2\n" +
17 "11,0,3,1\n" +
18 "12,1,4,2\n" +
19 "13,0,2,3\n";
20 var h_row, h_pts;
21 var graph = document.getElementById('graph');
22
23 beforeEach(function() {
24 graph.innerHTML = "<div id='graph1'></div><div id='graph2'></div>";
25 originalCallbackCalled = false;
26 h_row = 0, h_pts = [];
27 gs = [];
28
29 var highlightCallback = function(e, x, pts, row) {
30 originalCallbackCalled = true;
31
32 h_row = row;
33 h_pts = pts;
34 assert.equal(gs[0], this);
35 };
36
37 gs.push(new Dygraph(document.getElementById("graph1"), data, {
38 width: 100,
39 height: 100,
40 visibility: [false, true, true],
41 highlightCallback: highlightCallback
42 }));
43 gs.push(new Dygraph(document.getElementById("graph2"), data, {
44 width: 100,
45 height: 100,
46 visibility: [false, true, true],
47 }));
48 });
49
50 afterEach(function() {
51
52 });
53
54 /**
55 * This tests if original highlightCallback is called when synchronizer is attached
56 */
57 it('testOriginalHighlightCallbackStillWorks', function() {
58 var sync = Dygraph.synchronize(gs);
59
60 DygraphOps.dispatchMouseMove(gs[1], 5, 5);
61 // check that chart2 doesn't trigger highlightCallback on chart1
62 assert.equal(originalCallbackCalled, false);
63
64 DygraphOps.dispatchMouseMove(gs[0], 13, 10);
65 // check that original highlightCallback was called
66 assert.equal(originalCallbackCalled, true);
67
68 sync.detach();
69 });
70
71 /**
72 * This tests if selection is propagated correctly between charts
73 */
74 it('testChartsAreSynchronized', function() {
75 DygraphOps.dispatchMouseMove(gs[0], 13, 10);
76 assert.notEqual(gs[0].getSelection(), gs[1].getSelection());
77 DygraphOps.dispatchMouseMove(gs[0], 0, 0);
78
79 var sync = Dygraph.synchronize(gs);
80
81 DygraphOps.dispatchMouseMove(gs[0], 13, 10);
82
83 //check correct row is highlighted on second chart
84 assert.equal(3, h_row);
85 //check there are only two points (because first series is hidden)
86 assert.equal(2, h_pts.length);
87 //check that selection on both charts is the same
88 assert.equal(gs[0].getSelection(), gs[1].getSelection());
89
90 sync.detach();
91 });
92
93 /**
94 * This tests if detach works
95 */
96 it('testSynchronizerDetach', function() {
97 var sync = Dygraph.synchronize(gs);
98 DygraphOps.dispatchMouseMove(gs[1], 10, 10);
99 sync.detach();
100
101 originalCallbackCalled = false;
102 DygraphOps.dispatchMouseMove(gs[1], 0, 0);
103
104 //check that chart2 doesn't have highlightCallback
105 assert.equal(originalCallbackCalled, false);
106
107 DygraphOps.dispatchMouseMove(gs[0], 13, 10);
108
109 //check that original callback was re-attached
110 assert.equal(originalCallbackCalled, true);
111
112 //check that selection isn't synchronized anymore
113 assert.equal(gs[0].getSelection(), 3);
114 assert.equal(gs[1].getSelection(), 0);
115 });
116 });