tooltip_specs.ts 4.5 KB

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