Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

471 lines
9.1KB

  1. // Test the point element
  2. describe('Point element tests', function() {
  3. it ('Should be constructed', function() {
  4. var point = new Chart.elements.Point({
  5. _datasetIndex: 2,
  6. _index: 1
  7. });
  8. expect(point).not.toBe(undefined);
  9. expect(point._datasetIndex).toBe(2);
  10. expect(point._index).toBe(1);
  11. });
  12. it ('Should correctly identify as in range', function() {
  13. var point = new Chart.elements.Point({
  14. _datasetIndex: 2,
  15. _index: 1
  16. });
  17. // Safely handles if these are called before the viewmodel is instantiated
  18. expect(point.inRange(5)).toBe(false);
  19. expect(point.inLabelRange(5)).toBe(false);
  20. // Attach a view object as if we were the controller
  21. point._view = {
  22. radius: 2,
  23. hitRadius: 3,
  24. x: 10,
  25. y: 15
  26. };
  27. expect(point.inRange(10, 15)).toBe(true);
  28. expect(point.inRange(10, 10)).toBe(false);
  29. expect(point.inRange(10, 5)).toBe(false);
  30. expect(point.inRange(5, 5)).toBe(false);
  31. expect(point.inLabelRange(5)).toBe(false);
  32. expect(point.inLabelRange(7)).toBe(true);
  33. expect(point.inLabelRange(10)).toBe(true);
  34. expect(point.inLabelRange(12)).toBe(true);
  35. expect(point.inLabelRange(15)).toBe(false);
  36. expect(point.inLabelRange(20)).toBe(false);
  37. });
  38. it ('should get the correct tooltip position', function() {
  39. var point = new Chart.elements.Point({
  40. _datasetIndex: 2,
  41. _index: 1
  42. });
  43. // Attach a view object as if we were the controller
  44. point._view = {
  45. radius: 2,
  46. borderWidth: 6,
  47. x: 10,
  48. y: 15
  49. };
  50. expect(point.tooltipPosition()).toEqual({
  51. x: 10,
  52. y: 15,
  53. padding: 8
  54. });
  55. });
  56. it ('should draw correctly', function() {
  57. var mockContext = window.createMockContext();
  58. var point = new Chart.elements.Point({
  59. _datasetIndex: 2,
  60. _index: 1,
  61. _chart: {
  62. ctx: mockContext,
  63. }
  64. });
  65. // Attach a view object as if we were the controller
  66. point._view = {
  67. radius: 2,
  68. pointStyle: 'circle',
  69. hitRadius: 3,
  70. borderColor: 'rgba(1, 2, 3, 1)',
  71. borderWidth: 6,
  72. backgroundColor: 'rgba(0, 255, 0)',
  73. x: 10,
  74. y: 15,
  75. ctx: mockContext
  76. };
  77. point.draw();
  78. expect(mockContext.getCalls()).toEqual([{
  79. name: 'setStrokeStyle',
  80. args: ['rgba(1, 2, 3, 1)']
  81. }, {
  82. name: 'setLineWidth',
  83. args: [6]
  84. }, {
  85. name: 'setFillStyle',
  86. args: ['rgba(0, 255, 0)']
  87. }, {
  88. name: 'beginPath',
  89. args: []
  90. }, {
  91. name: 'arc',
  92. args: [10, 15, 2, 0, 2 * Math.PI]
  93. }, {
  94. name: 'closePath',
  95. args: [],
  96. }, {
  97. name: 'fill',
  98. args: [],
  99. }, {
  100. name: 'stroke',
  101. args: []
  102. }]);
  103. mockContext.resetCalls();
  104. point._view.pointStyle = 'triangle';
  105. point.draw();
  106. expect(mockContext.getCalls()).toEqual([{
  107. name: 'setStrokeStyle',
  108. args: ['rgba(1, 2, 3, 1)']
  109. }, {
  110. name: 'setLineWidth',
  111. args: [6]
  112. }, {
  113. name: 'setFillStyle',
  114. args: ['rgba(0, 255, 0)']
  115. }, {
  116. name: 'beginPath',
  117. args: []
  118. }, {
  119. name: 'moveTo',
  120. args: [10 - 3 * 2 / Math.sqrt(3) / 2, 15 + 3 * 2 / Math.sqrt(3) * Math.sqrt(3) / 2 / 3]
  121. }, {
  122. name: 'lineTo',
  123. args: [10 + 3 * 2 / Math.sqrt(3) / 2, 15 + 3 * 2 / Math.sqrt(3) * Math.sqrt(3) / 2 / 3],
  124. }, {
  125. name: 'lineTo',
  126. args: [10, 15 - 2 * 3 * 2 / Math.sqrt(3) * Math.sqrt(3) / 2 / 3],
  127. }, {
  128. name: 'closePath',
  129. args: [],
  130. }, {
  131. name: 'fill',
  132. args: [],
  133. }, {
  134. name: 'stroke',
  135. args: []
  136. }]);
  137. mockContext.resetCalls();
  138. point._view.pointStyle = 'rect';
  139. point.draw();
  140. expect(mockContext.getCalls()).toEqual([{
  141. name: 'setStrokeStyle',
  142. args: ['rgba(1, 2, 3, 1)']
  143. }, {
  144. name: 'setLineWidth',
  145. args: [6]
  146. }, {
  147. name: 'setFillStyle',
  148. args: ['rgba(0, 255, 0)']
  149. }, {
  150. name: 'fillRect',
  151. args: [10 - 1 / Math.SQRT2 * 2, 15 - 1 / Math.SQRT2 * 2, 2 / Math.SQRT2 * 2, 2 / Math.SQRT2 * 2]
  152. }, {
  153. name: 'strokeRect',
  154. args: [10 - 1 / Math.SQRT2 * 2, 15 - 1 / Math.SQRT2 * 2, 2 / Math.SQRT2 * 2, 2 / Math.SQRT2 * 2]
  155. }, {
  156. name: 'stroke',
  157. args: []
  158. }]);
  159. mockContext.resetCalls();
  160. point._view.pointStyle = 'rectRot';
  161. point.draw();
  162. expect(mockContext.getCalls()).toEqual([{
  163. name: 'setStrokeStyle',
  164. args: ['rgba(1, 2, 3, 1)']
  165. }, {
  166. name: 'setLineWidth',
  167. args: [6]
  168. }, {
  169. name: 'setFillStyle',
  170. args: ['rgba(0, 255, 0)']
  171. }, {
  172. name: 'beginPath',
  173. args: []
  174. }, {
  175. name: 'moveTo',
  176. args: [10 - 1 / Math.SQRT2 * 2, 15]
  177. }, {
  178. name: 'lineTo',
  179. args: [10, 15 + 1 / Math.SQRT2 * 2]
  180. }, {
  181. name: 'lineTo',
  182. args: [10 + 1 / Math.SQRT2 * 2, 15],
  183. }, {
  184. name: 'lineTo',
  185. args: [10, 15 - 1 / Math.SQRT2 * 2],
  186. }, {
  187. name: 'closePath',
  188. args: []
  189. }, {
  190. name: 'fill',
  191. args: [],
  192. }, {
  193. name: 'stroke',
  194. args: []
  195. }]);
  196. mockContext.resetCalls();
  197. point._view.pointStyle = 'cross';
  198. point.draw();
  199. expect(mockContext.getCalls()).toEqual([{
  200. name: 'setStrokeStyle',
  201. args: ['rgba(1, 2, 3, 1)']
  202. }, {
  203. name: 'setLineWidth',
  204. args: [6]
  205. }, {
  206. name: 'setFillStyle',
  207. args: ['rgba(0, 255, 0)']
  208. }, {
  209. name: 'beginPath',
  210. args: []
  211. }, {
  212. name: 'moveTo',
  213. args: [10, 17]
  214. }, {
  215. name: 'lineTo',
  216. args: [10, 13],
  217. }, {
  218. name: 'moveTo',
  219. args: [8, 15],
  220. }, {
  221. name: 'lineTo',
  222. args: [12, 15],
  223. },{
  224. name: 'closePath',
  225. args: [],
  226. }, {
  227. name: 'stroke',
  228. args: []
  229. }]);
  230. mockContext.resetCalls();
  231. point._view.pointStyle = 'crossRot';
  232. point.draw();
  233. expect(mockContext.getCalls()).toEqual([{
  234. name: 'setStrokeStyle',
  235. args: ['rgba(1, 2, 3, 1)']
  236. }, {
  237. name: 'setLineWidth',
  238. args: [6]
  239. }, {
  240. name: 'setFillStyle',
  241. args: ['rgba(0, 255, 0)']
  242. }, {
  243. name: 'beginPath',
  244. args: []
  245. }, {
  246. name: 'moveTo',
  247. args: [10 - Math.cos(Math.PI / 4) * 2, 15 - Math.sin(Math.PI / 4) * 2]
  248. }, {
  249. name: 'lineTo',
  250. args: [10 + Math.cos(Math.PI / 4) * 2, 15 + Math.sin(Math.PI / 4) * 2],
  251. }, {
  252. name: 'moveTo',
  253. args: [10 - Math.cos(Math.PI / 4) * 2, 15 + Math.sin(Math.PI / 4) * 2],
  254. }, {
  255. name: 'lineTo',
  256. args: [10 + Math.cos(Math.PI / 4) * 2, 15 - Math.sin(Math.PI / 4) * 2],
  257. }, {
  258. name: 'closePath',
  259. args: [],
  260. }, {
  261. name: 'stroke',
  262. args: []
  263. }]);
  264. mockContext.resetCalls();
  265. point._view.pointStyle = 'star';
  266. point.draw();
  267. expect(mockContext.getCalls()).toEqual([{
  268. name: 'setStrokeStyle',
  269. args: ['rgba(1, 2, 3, 1)']
  270. }, {
  271. name: 'setLineWidth',
  272. args: [6]
  273. }, {
  274. name: 'setFillStyle',
  275. args: ['rgba(0, 255, 0)']
  276. }, {
  277. name: 'beginPath',
  278. args: []
  279. }, {
  280. name: 'moveTo',
  281. args: [10, 17]
  282. }, {
  283. name: 'lineTo',
  284. args: [10, 13],
  285. }, {
  286. name: 'moveTo',
  287. args: [8, 15],
  288. }, {
  289. name: 'lineTo',
  290. args: [12, 15],
  291. },{
  292. name: 'moveTo',
  293. args: [10 - Math.cos(Math.PI / 4) * 2, 15 - Math.sin(Math.PI / 4) * 2]
  294. }, {
  295. name: 'lineTo',
  296. args: [10 + Math.cos(Math.PI / 4) * 2, 15 + Math.sin(Math.PI / 4) * 2],
  297. }, {
  298. name: 'moveTo',
  299. args: [10 - Math.cos(Math.PI / 4) * 2, 15 + Math.sin(Math.PI / 4) * 2],
  300. }, {
  301. name: 'lineTo',
  302. args: [10 + Math.cos(Math.PI / 4) * 2, 15 - Math.sin(Math.PI / 4) * 2],
  303. }, {
  304. name: 'closePath',
  305. args: [],
  306. }, {
  307. name: 'stroke',
  308. args: []
  309. }]);
  310. mockContext.resetCalls();
  311. point._view.pointStyle = 'line';
  312. point.draw();
  313. expect(mockContext.getCalls()).toEqual([{
  314. name: 'setStrokeStyle',
  315. args: ['rgba(1, 2, 3, 1)']
  316. }, {
  317. name: 'setLineWidth',
  318. args: [6]
  319. }, {
  320. name: 'setFillStyle',
  321. args: ['rgba(0, 255, 0)']
  322. }, {
  323. name: 'beginPath',
  324. args: []
  325. }, {
  326. name: 'moveTo',
  327. args: [8, 15]
  328. }, {
  329. name: 'lineTo',
  330. args: [12, 15],
  331. }, {
  332. name: 'closePath',
  333. args: [],
  334. }, {
  335. name: 'stroke',
  336. args: []
  337. }]);
  338. mockContext.resetCalls();
  339. point._view.pointStyle = 'dash';
  340. point.draw();
  341. expect(mockContext.getCalls()).toEqual([{
  342. name: 'setStrokeStyle',
  343. args: ['rgba(1, 2, 3, 1)']
  344. }, {
  345. name: 'setLineWidth',
  346. args: [6]
  347. }, {
  348. name: 'setFillStyle',
  349. args: ['rgba(0, 255, 0)']
  350. }, {
  351. name: 'beginPath',
  352. args: []
  353. }, {
  354. name: 'moveTo',
  355. args: [10, 15]
  356. }, {
  357. name: 'lineTo',
  358. args: [12, 15],
  359. }, {
  360. name: 'closePath',
  361. args: [],
  362. }, {
  363. name: 'stroke',
  364. args: []
  365. }]);
  366. });
  367. it ('should draw correctly with default settings if necessary', function() {
  368. var mockContext = window.createMockContext();
  369. var point = new Chart.elements.Point({
  370. _datasetIndex: 2,
  371. _index: 1,
  372. _chart: {
  373. ctx: mockContext,
  374. }
  375. });
  376. // Attach a view object as if we were the controller
  377. point._view = {
  378. radius: 2,
  379. hitRadius: 3,
  380. x: 10,
  381. y: 15,
  382. ctx: mockContext
  383. };
  384. point.draw();
  385. expect(mockContext.getCalls()).toEqual([{
  386. name: 'setStrokeStyle',
  387. args: ['rgba(0,0,0,0.1)']
  388. }, {
  389. name: 'setLineWidth',
  390. args: [1]
  391. }, {
  392. name: 'setFillStyle',
  393. args: ['rgba(0,0,0,0.1)']
  394. }, {
  395. name: 'beginPath',
  396. args: []
  397. }, {
  398. name: 'arc',
  399. args: [10, 15, 2, 0, 2 * Math.PI]
  400. }, {
  401. name: 'closePath',
  402. args: [],
  403. }, {
  404. name: 'fill',
  405. args: [],
  406. }, {
  407. name: 'stroke',
  408. args: []
  409. }]);
  410. });
  411. it ('should not draw if skipped', function() {
  412. var mockContext = window.createMockContext();
  413. var point = new Chart.elements.Point({
  414. _datasetIndex: 2,
  415. _index: 1,
  416. _chart: {
  417. ctx: mockContext,
  418. }
  419. });
  420. // Attach a view object as if we were the controller
  421. point._view = {
  422. radius: 2,
  423. hitRadius: 3,
  424. x: 10,
  425. y: 15,
  426. ctx: mockContext,
  427. skip: true
  428. };
  429. point.draw();
  430. expect(mockContext.getCalls()).toEqual([]);
  431. });
  432. });