tooltip-spec.ts 17 KB

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