tooltip_specs.ts 4.8 KB

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