graph_tooltip.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. jest.mock('app/core/core', () => ({}));
  2. import $ from 'jquery';
  3. import GraphTooltip from '../graph_tooltip';
  4. const scope = {
  5. appEvent: jest.fn(),
  6. onAppEvent: jest.fn(),
  7. ctrl: {},
  8. };
  9. const elem = $('<div></div>');
  10. const dashboard = {};
  11. const getSeriesFn = () => {};
  12. function describeSharedTooltip(desc, fn) {
  13. const ctx: any = {};
  14. ctx.ctrl = scope.ctrl;
  15. ctx.ctrl.panel = {
  16. tooltip: {
  17. shared: true,
  18. },
  19. legend: {},
  20. stack: false,
  21. };
  22. ctx.setup = function(setupFn) {
  23. ctx.setupFn = setupFn;
  24. };
  25. describe(desc, function() {
  26. beforeEach(function() {
  27. ctx.setupFn();
  28. const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  29. ctx.results = tooltip.getMultiSeriesPlotHoverInfo(ctx.data, ctx.pos);
  30. });
  31. fn(ctx);
  32. });
  33. }
  34. describe('findHoverIndexFromData', function() {
  35. const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  36. const series = {
  37. data: [[100, 0], [101, 0], [102, 0], [103, 0], [104, 0], [105, 0], [106, 0], [107, 0]],
  38. };
  39. it('should return 0 if posX out of lower bounds', function() {
  40. const posX = 99;
  41. expect(tooltip.findHoverIndexFromData(posX, series)).toBe(0);
  42. });
  43. it('should return n - 1 if posX out of upper bounds', function() {
  44. const posX = 108;
  45. expect(tooltip.findHoverIndexFromData(posX, series)).toBe(series.data.length - 1);
  46. });
  47. it('should return i if posX in series', function() {
  48. const posX = 104;
  49. expect(tooltip.findHoverIndexFromData(posX, series)).toBe(4);
  50. });
  51. it('should return i if posX not in series and i + 1 > posX', function() {
  52. const posX = 104.9;
  53. expect(tooltip.findHoverIndexFromData(posX, series)).toBe(4);
  54. });
  55. });
  56. describeSharedTooltip('steppedLine false, stack false', function(ctx) {
  57. ctx.setup(function() {
  58. ctx.data = [
  59. { data: [[10, 15], [12, 20]], lines: {}, hideTooltip: false },
  60. { data: [[10, 2], [12, 3]], lines: {}, hideTooltip: false },
  61. ];
  62. ctx.pos = { x: 11 };
  63. });
  64. it('should return 2 series', function() {
  65. expect(ctx.results.length).toBe(2);
  66. });
  67. it('should add time to results array', function() {
  68. expect(ctx.results.time).toBe(10);
  69. });
  70. it('should set value and hoverIndex', function() {
  71. expect(ctx.results[0].value).toBe(15);
  72. expect(ctx.results[1].value).toBe(2);
  73. expect(ctx.results[0].hoverIndex).toBe(0);
  74. });
  75. });
  76. describeSharedTooltip('one series is hidden', function(ctx) {
  77. ctx.setup(function() {
  78. ctx.data = [{ data: [[10, 15], [12, 20]] }, { data: [] }];
  79. ctx.pos = { x: 11 };
  80. });
  81. });
  82. describeSharedTooltip('steppedLine false, stack true, individual false', function(ctx) {
  83. ctx.setup(function() {
  84. ctx.data = [
  85. {
  86. data: [[10, 15], [12, 20]],
  87. lines: {},
  88. datapoints: {
  89. pointsize: 2,
  90. points: [[10, 15], [12, 20]],
  91. },
  92. stack: true,
  93. hideTooltip: false,
  94. },
  95. {
  96. data: [[10, 2], [12, 3]],
  97. lines: {},
  98. datapoints: {
  99. pointsize: 2,
  100. points: [[10, 2], [12, 3]],
  101. },
  102. stack: true,
  103. hideTooltip: false,
  104. },
  105. ];
  106. ctx.ctrl.panel.stack = true;
  107. ctx.pos = { x: 11 };
  108. });
  109. it('should show stacked value', function() {
  110. expect(ctx.results[1].value).toBe(17);
  111. });
  112. });
  113. describeSharedTooltip('steppedLine false, stack true, individual false, series stack false', function(ctx) {
  114. ctx.setup(function() {
  115. ctx.data = [
  116. {
  117. data: [[10, 15], [12, 20]],
  118. lines: {},
  119. datapoints: {
  120. pointsize: 2,
  121. points: [[10, 15], [12, 20]],
  122. },
  123. stack: true,
  124. hideTooltip: false,
  125. },
  126. {
  127. data: [[10, 2], [12, 3]],
  128. lines: {},
  129. datapoints: {
  130. pointsize: 2,
  131. points: [[10, 2], [12, 3]],
  132. },
  133. stack: false,
  134. hideTooltip: false,
  135. },
  136. ];
  137. ctx.ctrl.panel.stack = true;
  138. ctx.pos = { x: 11 };
  139. });
  140. it('should not show stacked value', function() {
  141. expect(ctx.results[1].value).toBe(2);
  142. });
  143. });
  144. describeSharedTooltip('steppedLine false, stack true, individual true', function(ctx) {
  145. ctx.setup(function() {
  146. ctx.data = [
  147. {
  148. data: [[10, 15], [12, 20]],
  149. lines: {},
  150. datapoints: {
  151. pointsize: 2,
  152. points: [[10, 15], [12, 20]],
  153. },
  154. stack: true,
  155. hideTooltip: false,
  156. },
  157. {
  158. data: [[10, 2], [12, 3]],
  159. lines: {},
  160. datapoints: {
  161. pointsize: 2,
  162. points: [[10, 2], [12, 3]],
  163. },
  164. stack: false,
  165. hideTooltip: false,
  166. },
  167. ];
  168. ctx.ctrl.panel.stack = true;
  169. ctx.ctrl.panel.tooltip.value_type = 'individual';
  170. ctx.pos = { x: 11 };
  171. });
  172. it('should not show stacked value', function() {
  173. expect(ctx.results[1].value).toBe(2);
  174. });
  175. });