tooltip-spec.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. describe('c3 chart tooltip', function () {
  2. 'use strict';
  3. var chart;
  4. var tooltipConfiguration = {};
  5. var dataOrder = 'desc';
  6. var dataGroups;
  7. var args = function () {
  8. return {
  9. data: {
  10. columns: [
  11. ['data1', 30, 200, 100, 400, 150, 250], // 1130
  12. ['data2', 50, 20, 10, 40, 15, 25], // 160
  13. ['data3', 150, 120, 110, 140, 115, 125] // 760
  14. ],
  15. order: dataOrder,
  16. groups: dataGroups,
  17. },
  18. tooltip: tooltipConfiguration
  19. };
  20. };
  21. beforeEach(function (done) {
  22. chart = window.initChart(chart, args(), done);
  23. dataOrder = 'desc';
  24. dataGroups = undefined;
  25. });
  26. describe('tooltip position', function () {
  27. beforeAll(function () {
  28. tooltipConfiguration = {};
  29. });
  30. describe('without left margin', function () {
  31. it('should show tooltip on proper position', function () {
  32. var eventRect = d3.select('.c3-event-rect').node(),
  33. x = chart.internal.x(1),
  34. y = chart.internal.y(200);
  35. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  36. var tooltipContainer = d3.select('.c3-tooltip-container'),
  37. top = Math.floor(+tooltipContainer.style('top').replace(/px/, '')),
  38. left = Math.floor(+tooltipContainer.style('left').replace(/px/, ''));
  39. expect(top).toBeGreaterThan(0);
  40. expect(left).toBeGreaterThan(0);
  41. });
  42. });
  43. describe('with left margin', function () {
  44. beforeAll(function () {
  45. d3.select('#chart').style('margin-left', '300px');
  46. });
  47. it('should show tooltip on proper position', function () {
  48. var eventRect = d3.select('.c3-event-rect').node(),
  49. x = chart.internal.x(1) + 300, // add margin-left
  50. y = chart.internal.y(200);
  51. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  52. var tooltipContainer = d3.select('.c3-tooltip-container'),
  53. top = Math.floor(+tooltipContainer.style('top').replace(/px/, '')),
  54. left = Math.floor(+tooltipContainer.style('left').replace(/px/, ''));
  55. expect(top).toBeGreaterThan(0);
  56. expect(left).toBeGreaterThan(0);
  57. });
  58. afterAll(function () {
  59. d3.select('#chart').style('margin-left', null);
  60. });
  61. });
  62. });
  63. describe('tooltip positionFunction', function () {
  64. var topExpected = 37, leftExpected = 79;
  65. beforeAll(function () {
  66. tooltipConfiguration = {
  67. position: function (data, width, height, element) {
  68. expect(data.length).toBe(args().data.columns.length);
  69. expect(data[0]).toEqual(jasmine.objectContaining({
  70. index: 2,
  71. value: 100,
  72. id: 'data1'
  73. }));
  74. expect(width).toBeGreaterThan(0);
  75. expect(height).toBeGreaterThan(0);
  76. expect(element).toBe(d3.select('.c3-event-rect').node());
  77. return {top: topExpected, left: leftExpected};
  78. }
  79. };
  80. });
  81. it('should be set to the coordinate where the function returned', function () {
  82. var eventRect = d3.select('.c3-event-rect').node(),
  83. x = chart.internal.x(2),
  84. y = chart.internal.y(100);
  85. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  86. var tooltipContainer = d3.select('.c3-tooltip-container'),
  87. top = Math.floor(+tooltipContainer.style('top').replace(/px/, '')),
  88. left = Math.floor(+tooltipContainer.style('left').replace(/px/, ''));
  89. expect(top).toBeGreaterThan(0);
  90. expect(left).toBeGreaterThan(0);
  91. });
  92. });
  93. describe('tooltip getTooltipContent', function () {
  94. beforeAll(function () {
  95. tooltipConfiguration = {
  96. data_order: 'desc'
  97. };
  98. });
  99. it('should sort values desc', function () {
  100. var eventRect = d3.select('.c3-event-rect').node(),
  101. x = chart.internal.x(2),
  102. y = chart.internal.y(100);
  103. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  104. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  105. return node.className;
  106. });
  107. expect(classes[0]).toBe(''); // header
  108. expect(classes[1]).toBe('c3-tooltip-name--data3');
  109. expect(classes[2]).toBe('c3-tooltip-name--data1');
  110. expect(classes[3]).toBe('c3-tooltip-name--data2');
  111. });
  112. });
  113. describe('tooltip with data_order as desc with grouped data', function() {
  114. beforeAll(function() {
  115. dataOrder = 'desc';
  116. dataGroups = [ [ 'data1', 'data2', 'data3' ]];
  117. });
  118. it('should display each data in descending order', function() {
  119. var eventRect = d3.select('.c3-event-rect').node(),
  120. x = chart.internal.x(2),
  121. y = chart.internal.y(220);
  122. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  123. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  124. return node.className;
  125. });
  126. expect(classes[0]).toBe(''); // header
  127. expect(classes[1]).toBe('c3-tooltip-name--data1'); // 1130
  128. expect(classes[2]).toBe('c3-tooltip-name--data3'); // 760
  129. expect(classes[3]).toBe('c3-tooltip-name--data2'); // 160
  130. });
  131. });
  132. describe('tooltip with data_order as asc with grouped data', function() {
  133. beforeAll(function() {
  134. dataOrder = 'asc';
  135. dataGroups = [ [ 'data1', 'data2', 'data3' ]];
  136. });
  137. it('should display each data in ascending order', function() {
  138. var eventRect = d3.select('.c3-event-rect').node(),
  139. x = chart.internal.x(2),
  140. y = chart.internal.y(220);
  141. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  142. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  143. return node.className;
  144. });
  145. expect(classes[0]).toBe(''); // header
  146. expect(classes[1]).toBe('c3-tooltip-name--data2'); // 160
  147. expect(classes[2]).toBe('c3-tooltip-name--data3'); // 760
  148. expect(classes[3]).toBe('c3-tooltip-name--data1'); // 1130
  149. });
  150. });
  151. describe('tooltip with data_order as NULL with grouped data', function() {
  152. beforeAll(function() {
  153. dataOrder = null;
  154. dataGroups = [ [ 'data1', 'data2', 'data3' ]];
  155. });
  156. it('should display each data in given order', function() {
  157. var eventRect = d3.select('.c3-event-rect').node(),
  158. x = chart.internal.x(2),
  159. y = chart.internal.y(220);
  160. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  161. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  162. return node.className;
  163. });
  164. expect(classes[0]).toBe(''); // header
  165. expect(classes[1]).toBe('c3-tooltip-name--data1');
  166. expect(classes[2]).toBe('c3-tooltip-name--data2');
  167. expect(classes[3]).toBe('c3-tooltip-name--data3');
  168. });
  169. });
  170. describe('tooltip with data_order as Function with grouped data', function() {
  171. beforeAll(function() {
  172. var order = [ 'data2', 'data1', 'data3' ];
  173. dataOrder = function(data1, data2) {
  174. return order.indexOf(data1.id) - order.indexOf(data2.id);
  175. };
  176. dataGroups = [ [ 'data1', 'data2', 'data3' ]];
  177. });
  178. it('should display each data in order given by function', function() {
  179. var eventRect = d3.select('.c3-event-rect').node(),
  180. x = chart.internal.x(2),
  181. y = chart.internal.y(220);
  182. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  183. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  184. return node.className;
  185. });
  186. expect(classes[0]).toBe(''); // header
  187. expect(classes[1]).toBe('c3-tooltip-name--data2');
  188. expect(classes[2]).toBe('c3-tooltip-name--data1');
  189. expect(classes[3]).toBe('c3-tooltip-name--data3');
  190. });
  191. });
  192. describe('tooltip with data_order as Array with grouped data', function() {
  193. beforeAll(function() {
  194. dataOrder = [ 'data2', 'data1', 'data3' ];
  195. dataGroups = [ [ 'data1', 'data2', 'data3' ]];
  196. });
  197. it('should display each data in order given by array', function() {
  198. var eventRect = d3.select('.c3-event-rect').node(),
  199. x = chart.internal.x(2),
  200. y = chart.internal.y(220);
  201. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  202. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  203. return node.className;
  204. });
  205. expect(classes[0]).toBe(''); // header
  206. expect(classes[1]).toBe('c3-tooltip-name--data2');
  207. expect(classes[2]).toBe('c3-tooltip-name--data1');
  208. expect(classes[3]).toBe('c3-tooltip-name--data3');
  209. });
  210. });
  211. describe('tooltip with data_order as desc with un-grouped data', function() {
  212. beforeAll(function() {
  213. dataOrder = 'desc';
  214. });
  215. it('should display each tooltip value descending order', function() {
  216. var eventRect = d3.select('.c3-event-rect').node(),
  217. x = chart.internal.x(2),
  218. y = chart.internal.y(100);
  219. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  220. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  221. return node.className;
  222. });
  223. expect(classes[0]).toBe(''); // header
  224. expect(classes[1]).toBe('c3-tooltip-name--data3'); // 110
  225. expect(classes[2]).toBe('c3-tooltip-name--data1'); // 100
  226. expect(classes[3]).toBe('c3-tooltip-name--data2'); // 10
  227. });
  228. });
  229. describe('tooltip with data_order as asc with un-grouped data', function() {
  230. beforeAll(function() {
  231. dataOrder = 'asc';
  232. });
  233. it('should display each tooltip value in ascending order', function() {
  234. var eventRect = d3.select('.c3-event-rect').node(),
  235. x = chart.internal.x(2),
  236. y = chart.internal.y(100);
  237. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  238. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  239. return node.className;
  240. });
  241. expect(classes[0]).toBe(''); // header
  242. expect(classes[1]).toBe('c3-tooltip-name--data2'); // 10
  243. expect(classes[2]).toBe('c3-tooltip-name--data1'); // 100
  244. expect(classes[3]).toBe('c3-tooltip-name--data3'); // 110
  245. });
  246. });
  247. describe('tooltip with data_order as NULL with un-grouped data', function() {
  248. beforeAll(function() {
  249. dataOrder = null;
  250. });
  251. it('should display each tooltip value in given data order', function() {
  252. var eventRect = d3.select('.c3-event-rect').node(),
  253. x = chart.internal.x(2),
  254. y = chart.internal.y(100);
  255. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  256. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  257. return node.className;
  258. });
  259. expect(classes[0]).toBe(''); // header
  260. expect(classes[1]).toBe('c3-tooltip-name--data1');
  261. expect(classes[2]).toBe('c3-tooltip-name--data2');
  262. expect(classes[3]).toBe('c3-tooltip-name--data3');
  263. });
  264. });
  265. describe('tooltip with data_order as Function with un-grouped data', function() {
  266. beforeAll(function() {
  267. var order = [ 'data2', 'data1', 'data3' ];
  268. dataOrder = function(data1, data2) {
  269. return order.indexOf(data1.id) - order.indexOf(data2.id);
  270. };
  271. });
  272. it('should display each tooltip value in data order given by function', function() {
  273. var eventRect = d3.select('.c3-event-rect').node(),
  274. x = chart.internal.x(2),
  275. y = chart.internal.y(100);
  276. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  277. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  278. return node.className;
  279. });
  280. expect(classes[0]).toBe(''); // header
  281. expect(classes[1]).toBe('c3-tooltip-name--data2');
  282. expect(classes[2]).toBe('c3-tooltip-name--data1');
  283. expect(classes[3]).toBe('c3-tooltip-name--data3');
  284. });
  285. });
  286. describe('tooltip with data_order as Array with un-grouped data', function() {
  287. beforeAll(function() {
  288. dataOrder = [ 'data2', 'data1', 'data3' ];
  289. });
  290. it('should display each tooltip value in data order given by array', function() {
  291. var eventRect = d3.select('.c3-event-rect').node(),
  292. x = chart.internal.x(2),
  293. y = chart.internal.y(100);
  294. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  295. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  296. return node.className;
  297. });
  298. expect(classes[0]).toBe(''); // header
  299. expect(classes[1]).toBe('c3-tooltip-name--data2');
  300. expect(classes[2]).toBe('c3-tooltip-name--data1');
  301. expect(classes[3]).toBe('c3-tooltip-name--data3');
  302. });
  303. });
  304. describe('tooltip with tooltip_order as desc', function() {
  305. beforeAll(function() {
  306. tooltipConfiguration = {
  307. order: 'desc'
  308. };
  309. // this should be ignored
  310. dataOrder = 'asc';
  311. dataGroups = [ [ 'data1', 'data2', 'data3' ]];
  312. });
  313. it('should display each tooltip value descending order', function() {
  314. var eventRect = d3.select('.c3-event-rect').node(),
  315. x = chart.internal.x(2),
  316. y = chart.internal.y(100);
  317. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  318. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  319. return node.className;
  320. });
  321. expect(classes[0]).toBe(''); // header
  322. expect(classes[1]).toBe('c3-tooltip-name--data3'); // 110
  323. expect(classes[2]).toBe('c3-tooltip-name--data1'); // 100
  324. expect(classes[3]).toBe('c3-tooltip-name--data2'); // 10
  325. });
  326. });
  327. describe('tooltip with tooltip_order as asc', function() {
  328. beforeAll(function() {
  329. tooltipConfiguration = {
  330. order: 'asc'
  331. };
  332. // this should be ignored
  333. dataOrder = 'desc';
  334. dataGroups = [ [ 'data1', 'data2', 'data3' ]];
  335. });
  336. it('should display each tooltip value in ascending order', function() {
  337. var eventRect = d3.select('.c3-event-rect').node(),
  338. x = chart.internal.x(2),
  339. y = chart.internal.y(220);
  340. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  341. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  342. return node.className;
  343. });
  344. expect(classes[0]).toBe(''); // header
  345. expect(classes[1]).toBe('c3-tooltip-name--data2'); // 10
  346. expect(classes[2]).toBe('c3-tooltip-name--data1'); // 100
  347. expect(classes[3]).toBe('c3-tooltip-name--data3'); // 110
  348. });
  349. });
  350. describe('tooltip with tooltip_order as NULL', function() {
  351. beforeAll(function() {
  352. tooltipConfiguration = {
  353. order: null
  354. };
  355. });
  356. it('should display each tooltip value in given order', function() {
  357. var eventRect = d3.select('.c3-event-rect').node(),
  358. x = chart.internal.x(2),
  359. y = chart.internal.y(100);
  360. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  361. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  362. return node.className;
  363. });
  364. expect(classes[0]).toBe(''); // header
  365. expect(classes[1]).toBe('c3-tooltip-name--data1');
  366. expect(classes[2]).toBe('c3-tooltip-name--data2');
  367. expect(classes[3]).toBe('c3-tooltip-name--data3');
  368. });
  369. });
  370. describe('tooltip with tooltip_order as Function', function() {
  371. beforeAll(function() {
  372. var order = [ 'data2', 'data1', 'data3' ];
  373. tooltipConfiguration = {
  374. order: function(data1, data2) {
  375. return order.indexOf(data1.id) - order.indexOf(data2.id);
  376. }
  377. };
  378. });
  379. it('should display each tooltip value in data order given by function', function() {
  380. var eventRect = d3.select('.c3-event-rect').node(),
  381. x = chart.internal.x(2),
  382. y = chart.internal.y(100);
  383. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  384. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  385. return node.className;
  386. });
  387. expect(classes[0]).toBe(''); // header
  388. expect(classes[1]).toBe('c3-tooltip-name--data2');
  389. expect(classes[2]).toBe('c3-tooltip-name--data1');
  390. expect(classes[3]).toBe('c3-tooltip-name--data3');
  391. });
  392. });
  393. describe('tooltip with tooltip_order as Array', function() {
  394. beforeAll(function() {
  395. tooltipConfiguration = {
  396. order: [ 'data2', 'data1', 'data3' ]
  397. };
  398. });
  399. it('should display each tooltip value in data order given by array', function() {
  400. var eventRect = d3.select('.c3-event-rect').node(),
  401. x = chart.internal.x(2),
  402. y = chart.internal.y(100);
  403. window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
  404. var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
  405. return node.className;
  406. });
  407. expect(classes[0]).toBe(''); // header
  408. expect(classes[1]).toBe('c3-tooltip-name--data2');
  409. expect(classes[2]).toBe('c3-tooltip-name--data1');
  410. expect(classes[3]).toBe('c3-tooltip-name--data3');
  411. });
  412. });
  413. });