tooltip_specs.ts 4.6 KB

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