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