graph_tooltip.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. jest.mock('app/core/core', () => ({}));
  2. import $ from 'jquery';
  3. import GraphTooltip from '../graph_tooltip';
  4. const scope: any = {
  5. appEvent: jest.fn(),
  6. onAppEvent: jest.fn(),
  7. ctrl: {
  8. panel: {
  9. tooltip: {},
  10. },
  11. },
  12. };
  13. const elem = $('<div></div>');
  14. const dashboard = {};
  15. const getSeriesFn = () => {};
  16. describe('findHoverIndexFromData', () => {
  17. // @ts-ignore
  18. const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  19. const series = {
  20. data: [
  21. [100, 0],
  22. [101, 0],
  23. [102, 0],
  24. [103, 0],
  25. [104, 0],
  26. [105, 0],
  27. [106, 0],
  28. [107, 0],
  29. ],
  30. };
  31. it('should return 0 if posX out of lower bounds', () => {
  32. const posX = 99;
  33. expect(tooltip.findHoverIndexFromData(posX, series)).toBe(0);
  34. });
  35. it('should return n - 1 if posX out of upper bounds', () => {
  36. const posX = 108;
  37. expect(tooltip.findHoverIndexFromData(posX, series)).toBe(series.data.length - 1);
  38. });
  39. it('should return i if posX in series', () => {
  40. const posX = 104;
  41. expect(tooltip.findHoverIndexFromData(posX, series)).toBe(4);
  42. });
  43. it('should return i if posX not in series and i + 1 > posX', () => {
  44. const posX = 104.9;
  45. expect(tooltip.findHoverIndexFromData(posX, series)).toBe(4);
  46. });
  47. });
  48. describe('with a shared tooltip', () => {
  49. beforeEach(() => {
  50. scope.ctrl.panel = {
  51. tooltip: {
  52. shared: true,
  53. },
  54. legend: {},
  55. stack: false,
  56. };
  57. });
  58. describe('steppedLine false, stack false', () => {
  59. const data = [
  60. {
  61. data: [
  62. [10, 15],
  63. [12, 20],
  64. ],
  65. lines: {},
  66. hideTooltip: false,
  67. },
  68. {
  69. data: [
  70. [10, 2],
  71. [12, 3],
  72. ],
  73. lines: {},
  74. hideTooltip: false,
  75. },
  76. ];
  77. const pos = { x: 11 };
  78. it('should return 2 series', () => {
  79. // @ts-ignore
  80. const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  81. const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
  82. expect(results.length).toBe(2);
  83. });
  84. it('should add time to results array', () => {
  85. // @ts-ignore
  86. const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  87. const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
  88. expect(results.time).toBe(10);
  89. });
  90. it('should set value and hoverIndex', () => {
  91. // @ts-ignore
  92. const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  93. const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
  94. expect(results[0].value).toBe(15);
  95. expect(results[1].value).toBe(2);
  96. expect(results[0].hoverIndex).toBe(0);
  97. });
  98. });
  99. describe('steppedLine false, stack true, individual false', () => {
  100. const data = [
  101. {
  102. data: [
  103. [10, 15],
  104. [12, 20],
  105. ],
  106. lines: {},
  107. datapoints: {
  108. pointsize: 2,
  109. points: [
  110. [10, 15],
  111. [12, 20],
  112. ],
  113. },
  114. stack: true,
  115. hideTooltip: false,
  116. },
  117. {
  118. data: [
  119. [10, 2],
  120. [12, 3],
  121. ],
  122. lines: {},
  123. datapoints: {
  124. pointsize: 2,
  125. points: [
  126. [10, 2],
  127. [12, 3],
  128. ],
  129. },
  130. stack: true,
  131. hideTooltip: false,
  132. },
  133. ];
  134. scope.ctrl.panel.stack = true;
  135. const pos = { x: 11 };
  136. it('should show stacked value', () => {
  137. // @ts-ignore
  138. const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  139. const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
  140. expect(results[1].value).toBe(17);
  141. });
  142. });
  143. describe('steppedLine false, stack true, individual false, series stack false', () => {
  144. const data = [
  145. {
  146. data: [
  147. [10, 15],
  148. [12, 20],
  149. ],
  150. lines: {},
  151. datapoints: {
  152. pointsize: 2,
  153. points: [
  154. [10, 15],
  155. [12, 20],
  156. ],
  157. },
  158. stack: true,
  159. hideTooltip: false,
  160. },
  161. {
  162. data: [
  163. [10, 2],
  164. [12, 3],
  165. ],
  166. lines: {},
  167. datapoints: {
  168. pointsize: 2,
  169. points: [
  170. [10, 2],
  171. [12, 3],
  172. ],
  173. },
  174. stack: false,
  175. hideTooltip: false,
  176. },
  177. ];
  178. scope.ctrl.panel.stack = true;
  179. const pos = { x: 11 };
  180. it('should not show stacked value', () => {
  181. // @ts-ignore
  182. const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  183. const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
  184. expect(results[1].value).toBe(2);
  185. });
  186. });
  187. describe('steppedLine false, stack true, individual true', () => {
  188. const data = [
  189. {
  190. data: [
  191. [10, 15],
  192. [12, 20],
  193. ],
  194. lines: {},
  195. datapoints: {
  196. pointsize: 2,
  197. points: [
  198. [10, 15],
  199. [12, 20],
  200. ],
  201. },
  202. stack: true,
  203. hideTooltip: false,
  204. },
  205. {
  206. data: [
  207. [10, 2],
  208. [12, 3],
  209. ],
  210. lines: {},
  211. datapoints: {
  212. pointsize: 2,
  213. points: [
  214. [10, 2],
  215. [12, 3],
  216. ],
  217. },
  218. stack: false,
  219. hideTooltip: false,
  220. },
  221. ];
  222. scope.ctrl.panel.stack = true;
  223. scope.ctrl.panel.tooltip.value_type = 'individual';
  224. const pos = { x: 11 };
  225. it('should not show stacked value', () => {
  226. // @ts-ignore
  227. const tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
  228. const results = tooltip.getMultiSeriesPlotHoverInfo(data, pos);
  229. expect(results[1].value).toBe(2);
  230. });
  231. });
  232. });