jquery.flot.hover.Test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. describe("flot hover plugin", function () {
  2. var placeholder, plot, options;
  3. var rgba = window.colors.rgba;
  4. var getEntireCanvasData = window.colors.getEntireCanvasData;
  5. var canvasData = window.colors.canvasData;
  6. beforeEach(function () {
  7. placeholder = setFixtures('<div id="test-container" style="width: 600px;height: 400px">')
  8. .find('#test-container');
  9. options = {
  10. grid: { hoverable: true, clickable: true },
  11. pan: { enableTouch: true, active: true },
  12. series: {
  13. lines: {
  14. show: true
  15. },
  16. points: {
  17. show: false
  18. }
  19. }
  20. };
  21. jasmine.clock().install().mockDate();
  22. jasmine.addMatchers(window.colors.jasmineMatchers);
  23. });
  24. afterEach(function() {
  25. jasmine.clock().uninstall();
  26. });
  27. describe('touch hover', function() {
  28. it('tap on plot triggers plothover event', function() {
  29. plot = $.plot(placeholder, [ [ [0, 0], [10, 10] ] ], options);
  30. var eventHolder = plot.getEventHolder(),
  31. axisx = plot.getXAxes()[0],
  32. axisy = plot.getYAxes()[0],
  33. coords = [{x: axisx.p2c(0.5), y: axisy.p2c(-3.5)}],
  34. spy = jasmine.createSpy('spy');
  35. $(plot.getPlaceholder()).on('plothover', spy);
  36. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  37. jasmine.clock().tick(50);
  38. simulate.sendTouchEvents(coords, eventHolder, 'touchend');
  39. expect(spy).toHaveBeenCalled();
  40. expect(spy.calls.count()).toBe(1);
  41. });
  42. it('pan plot triggers plothovercleanup event', function() {
  43. plot = $.plot(placeholder, [ [ [0, 0], [10, 10] ] ], options);
  44. var eventHolder = plot.getEventHolder(),
  45. axisx = plot.getXAxes()[0],
  46. axisy = plot.getYAxes()[0],
  47. coords = [{x: axisx.p2c(0.5), y: axisy.p2c(-3.5)}],
  48. spy = jasmine.createSpy('spy');
  49. $(plot.getPlaceholder()).on('plothovercleanup', spy);
  50. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  51. expect(spy).toHaveBeenCalled();
  52. expect(spy.calls.count()).toBe(1);
  53. });
  54. it('set data to the plot triggers plothovercleanup event', function() {
  55. plot = $.plot(placeholder, [ [ [0, 0], [10, 10] ] ], options);
  56. var spy = jasmine.createSpy('spy');
  57. $(plot.getPlaceholder()).on('plothovercleanup', spy);
  58. plot.setData([1, 2, 3, 4]);
  59. expect(spy).toHaveBeenCalled();
  60. });
  61. });
  62. describe('mouse hover', function() {
  63. beforeEach(function() {
  64. options.series.hoverable = true;
  65. options.series.highlightColor = 'rgba(10, 20, 30, 1)';
  66. });
  67. it('should highlight the point when hovered', function() {
  68. plot = $.plot(placeholder, [ [ [0, 0], [2, 3], [10, 10] ] ], options);
  69. var eventHolder = plot.getEventHolder(),
  70. canvas = eventHolder,
  71. offset = plot.getPlotOffset(),
  72. axisx = plot.getXAxes()[0],
  73. axisy = plot.getYAxes()[0],
  74. x = axisx.p2c(2) + offset.left,
  75. y = axisy.p2c(3) + offset.top,
  76. noButton = 0;
  77. simulate.mouseMove(eventHolder, x, y, noButton);
  78. jasmine.clock().tick(100);
  79. expect(getEntireCanvasData(canvas)).toContainPixelColor(rgba(10, 20, 30, 1));
  80. });
  81. it('should highlight the point when hovered from a small distance', function() {
  82. plot = $.plot(placeholder, [ [ [0, 0], [2, 3], [10, 10] ] ], options);
  83. var eventHolder = plot.getEventHolder(),
  84. canvas = eventHolder,
  85. offset = plot.getPlotOffset(),
  86. epsilon = 2,
  87. axisx = plot.getXAxes()[0],
  88. axisy = plot.getYAxes()[0],
  89. x = axisx.p2c(2) + offset.left + epsilon,
  90. y = axisy.p2c(3) + offset.top - epsilon,
  91. noButton = 0;
  92. simulate.mouseMove(eventHolder, x, y, noButton);
  93. jasmine.clock().tick(100);
  94. expect(getEntireCanvasData(canvas)).toContainPixelColor(rgba(10, 20, 30, 1));
  95. });
  96. it('should not highlight the point when hovered and the grid is not hoverable', function() {
  97. options.grid.hoverable = false;
  98. plot = $.plot(placeholder, [ [ [0, 0], [2, 3], [10, 10] ] ], options);
  99. var eventHolder = plot.getEventHolder(),
  100. canvas = eventHolder,
  101. offset = plot.getPlotOffset(),
  102. axisx = plot.getXAxes()[0],
  103. axisy = plot.getYAxes()[0],
  104. x = axisx.p2c(2) + offset.left,
  105. y = axisy.p2c(3) + offset.top,
  106. noButton = 0;
  107. simulate.mouseMove(eventHolder, x, y, noButton);
  108. jasmine.clock().tick(100);
  109. expect(getEntireCanvasData(canvas)).not.toContainPixelColor(rgba(10, 20, 30, 1));
  110. });
  111. it('should not highlight the point when hovered and the series is not hoverable', function() {
  112. options.series.hoverable = false;
  113. plot = $.plot(placeholder, [ [ [0, 0], [2, 3], [10, 10] ] ], options);
  114. var eventHolder = plot.getEventHolder(),
  115. canvas = eventHolder,
  116. offset = plot.getPlotOffset(),
  117. axisx = plot.getXAxes()[0],
  118. axisy = plot.getYAxes()[0],
  119. x = axisx.p2c(2) + offset.left,
  120. y = axisy.p2c(3) + offset.top,
  121. noButton = 0;
  122. simulate.mouseMove(eventHolder, x, y, noButton);
  123. jasmine.clock().tick(100);
  124. expect(getEntireCanvasData(canvas)).not.toContainPixelColor(rgba(10, 20, 30, 1));
  125. });
  126. it('should unhighlight the previouse point when hovering a new one', function() {
  127. plot = $.plot(placeholder, [ [ [0, 0], [2, 3], [10, 10] ] ], options);
  128. var eventHolder = plot.getEventHolder(),
  129. canvas = eventHolder,
  130. offset = plot.getPlotOffset(),
  131. axisx = plot.getXAxes()[0],
  132. axisy = plot.getYAxes()[0],
  133. x1 = axisx.p2c(2) + offset.left,
  134. y1 = axisy.p2c(3) + offset.top,
  135. x2 = axisx.p2c(10) + offset.left,
  136. y2 = axisy.p2c(10) + offset.top,
  137. r = 5,
  138. noButton = 0;
  139. simulate.mouseMove(eventHolder, x1, y1, noButton);
  140. jasmine.clock().tick(100);
  141. expect(canvasData(canvas, x1 - r, y1 - r, 2 * r, 2 * r)).toContainPixelColor(rgba(10, 20, 30, 1));
  142. expect(canvasData(canvas, x2 - r, y2 - r, 2 * r, 2 * r)).not.toContainPixelColor(rgba(10, 20, 30, 1));
  143. simulate.mouseMove(eventHolder, x2, y2, noButton);
  144. jasmine.clock().tick(100);
  145. expect(canvasData(canvas, x1 - r, y1 - r, 2 * r, 2 * r)).not.toContainPixelColor(rgba(10, 20, 30, 1));
  146. expect(canvasData(canvas, x2 - r, y2 - r, 2 * r, 2 * r)).toContainPixelColor(rgba(10, 20, 30, 1));
  147. });
  148. it('should update the current hover point to the placeholder when the plot created again', function () {
  149. plot = $.plot(placeholder, [ [ [0, 0], [2, 3], [10, 10] ] ], options);
  150. var eventHolder = plot.getEventHolder(),
  151. offset = plot.getPlotOffset(),
  152. axisx = plot.getXAxes()[0],
  153. axisy = plot.getYAxes()[0],
  154. x = axisx.p2c(2) + offset.left,
  155. y = axisy.p2c(3) + offset.top,
  156. noButton = 0;
  157. let evt = simulate.mouseMove(eventHolder, x, y, noButton);
  158. jasmine.clock().tick(1000);
  159. plot = $.plot(placeholder, [ [ [0, 0], [2, 3], [10, 10] ] ], options);
  160. expect(plot.getPlaceholder()[0].lastMouseMoveEvent.originalEvent.x).toEqual(evt.x);
  161. expect(plot.getPlaceholder()[0].lastMouseMoveEvent.originalEvent.y).toEqual(evt.y);
  162. });
  163. it('should highlight a bar when hovered', function() {
  164. options.series.bars = { show: true, barWidth: 0.5 };
  165. options.series.lines = undefined;
  166. plot = $.plot(placeholder, [ [ [0, 3], [1, 5], [2, 4] ] ], options);
  167. var eventHolder = plot.getEventHolder(),
  168. canvas = eventHolder,
  169. offset = plot.getPlotOffset(),
  170. axisx = plot.getXAxes()[0],
  171. axisy = plot.getYAxes()[0],
  172. x = axisx.p2c(1.25) + offset.left,
  173. y = axisy.p2c(2) + offset.top,
  174. noButton = 0;
  175. simulate.mouseMove(eventHolder, x, y, noButton);
  176. jasmine.clock().tick(100);
  177. expect(getEntireCanvasData(canvas)).toContainPixelColor(rgba(10, 20, 30, 1));
  178. });
  179. });
  180. });