graph_tooltip.jest.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. jest.mock('app/core/core', () => ({}));
  2. import $ from 'jquery';
  3. import GraphTooltip from '../graph_tooltip';
  4. var scope = {
  5. appEvent: jest.fn(),
  6. onAppEvent: jest.fn(),
  7. ctrl: {},
  8. };
  9. var elem = $('<div></div>');
  10. var dashboard = {};
  11. var getSeriesFn;
  12. function describeSharedTooltip(desc, fn) {
  13. var 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. var 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. var tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  36. var 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. var 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. var 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. var 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. var 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 = [{ data: [[10, 15], [12, 20]], lines: {} }, { data: [[10, 2], [12, 3]], lines: {} }];
  59. ctx.pos = { x: 11 };
  60. });
  61. it('should return 2 series', function() {
  62. expect(ctx.results.length).toBe(2);
  63. });
  64. it('should add time to results array', function() {
  65. expect(ctx.results.time).toBe(10);
  66. });
  67. it('should set value and hoverIndex', function() {
  68. expect(ctx.results[0].value).toBe(15);
  69. expect(ctx.results[1].value).toBe(2);
  70. expect(ctx.results[0].hoverIndex).toBe(0);
  71. });
  72. });
  73. describeSharedTooltip('one series is hidden', function(ctx) {
  74. ctx.setup(function() {
  75. ctx.data = [{ data: [[10, 15], [12, 20]] }, { data: [] }];
  76. ctx.pos = { x: 11 };
  77. });
  78. });
  79. describeSharedTooltip('steppedLine false, stack true, individual false', function(ctx) {
  80. ctx.setup(function() {
  81. ctx.data = [
  82. {
  83. data: [[10, 15], [12, 20]],
  84. lines: {},
  85. datapoints: {
  86. pointsize: 2,
  87. points: [[10, 15], [12, 20]],
  88. },
  89. stack: true,
  90. },
  91. {
  92. data: [[10, 2], [12, 3]],
  93. lines: {},
  94. datapoints: {
  95. pointsize: 2,
  96. points: [[10, 2], [12, 3]],
  97. },
  98. stack: true,
  99. },
  100. ];
  101. ctx.ctrl.panel.stack = true;
  102. ctx.pos = { x: 11 };
  103. });
  104. it('should show stacked value', function() {
  105. expect(ctx.results[1].value).toBe(17);
  106. });
  107. });
  108. describeSharedTooltip('steppedLine false, stack true, individual false, series stack false', function(ctx) {
  109. ctx.setup(function() {
  110. ctx.data = [
  111. {
  112. data: [[10, 15], [12, 20]],
  113. lines: {},
  114. datapoints: {
  115. pointsize: 2,
  116. points: [[10, 15], [12, 20]],
  117. },
  118. stack: true,
  119. },
  120. {
  121. data: [[10, 2], [12, 3]],
  122. lines: {},
  123. datapoints: {
  124. pointsize: 2,
  125. points: [[10, 2], [12, 3]],
  126. },
  127. stack: false,
  128. },
  129. ];
  130. ctx.ctrl.panel.stack = true;
  131. ctx.pos = { x: 11 };
  132. });
  133. it('should not show stacked value', function() {
  134. expect(ctx.results[1].value).toBe(2);
  135. });
  136. });
  137. describeSharedTooltip('steppedLine false, stack true, individual true', function(ctx) {
  138. ctx.setup(function() {
  139. ctx.data = [
  140. {
  141. data: [[10, 15], [12, 20]],
  142. lines: {},
  143. datapoints: {
  144. pointsize: 2,
  145. points: [[10, 15], [12, 20]],
  146. },
  147. stack: true,
  148. },
  149. {
  150. data: [[10, 2], [12, 3]],
  151. lines: {},
  152. datapoints: {
  153. pointsize: 2,
  154. points: [[10, 2], [12, 3]],
  155. },
  156. stack: false,
  157. },
  158. ];
  159. ctx.ctrl.panel.stack = true;
  160. ctx.ctrl.panel.tooltip.value_type = 'individual';
  161. ctx.pos = { x: 11 };
  162. });
  163. it('should not show stacked value', function() {
  164. expect(ctx.results[1].value).toBe(2);
  165. });
  166. });