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

303 lines
6.6KB

  1. // Test the rectangle element
  2. describe('Rectangle element tests', function() {
  3. it ('Should be constructed', function() {
  4. var rectangle = new Chart.elements.Rectangle({
  5. _datasetIndex: 2,
  6. _index: 1
  7. });
  8. expect(rectangle).not.toBe(undefined);
  9. expect(rectangle._datasetIndex).toBe(2);
  10. expect(rectangle._index).toBe(1);
  11. });
  12. it ('Should correctly identify as in range', function() {
  13. var rectangle = new Chart.elements.Rectangle({
  14. _datasetIndex: 2,
  15. _index: 1
  16. });
  17. // Safely handles if these are called before the viewmodel is instantiated
  18. expect(rectangle.inRange(5)).toBe(false);
  19. expect(rectangle.inLabelRange(5)).toBe(false);
  20. // Attach a view object as if we were the controller
  21. rectangle._view = {
  22. base: 0,
  23. width: 4,
  24. x: 10,
  25. y: 15
  26. };
  27. expect(rectangle.inRange(10, 15)).toBe(true);
  28. expect(rectangle.inRange(10, 10)).toBe(true);
  29. expect(rectangle.inRange(10, 16)).toBe(false);
  30. expect(rectangle.inRange(5, 5)).toBe(false);
  31. expect(rectangle.inLabelRange(5)).toBe(false);
  32. expect(rectangle.inLabelRange(7)).toBe(false);
  33. expect(rectangle.inLabelRange(10)).toBe(true);
  34. expect(rectangle.inLabelRange(12)).toBe(true);
  35. expect(rectangle.inLabelRange(15)).toBe(false);
  36. expect(rectangle.inLabelRange(20)).toBe(false);
  37. // Test when the y is below the base (negative bar)
  38. var negativeRectangle = new Chart.elements.Rectangle({
  39. _datasetIndex: 2,
  40. _index: 1
  41. });
  42. // Attach a view object as if we were the controller
  43. negativeRectangle._view = {
  44. base: 0,
  45. width: 4,
  46. x: 10,
  47. y: -15
  48. };
  49. expect(negativeRectangle.inRange(10, -16)).toBe(false);
  50. expect(negativeRectangle.inRange(10, 1)).toBe(false);
  51. expect(negativeRectangle.inRange(10, -5)).toBe(true);
  52. });
  53. it ('should get the correct height', function() {
  54. var rectangle = new Chart.elements.Rectangle({
  55. _datasetIndex: 2,
  56. _index: 1
  57. });
  58. // Attach a view object as if we were the controller
  59. rectangle._view = {
  60. base: 0,
  61. width: 4,
  62. x: 10,
  63. y: 15
  64. };
  65. expect(rectangle.height()).toBe(-15);
  66. // Test when the y is below the base (negative bar)
  67. var negativeRectangle = new Chart.elements.Rectangle({
  68. _datasetIndex: 2,
  69. _index: 1
  70. });
  71. // Attach a view object as if we were the controller
  72. negativeRectangle._view = {
  73. base: -10,
  74. width: 4,
  75. x: 10,
  76. y: -15
  77. };
  78. expect(negativeRectangle.height()).toBe(5);
  79. });
  80. it ('should get the correct tooltip position', function() {
  81. var rectangle = new Chart.elements.Rectangle({
  82. _datasetIndex: 2,
  83. _index: 1
  84. });
  85. // Attach a view object as if we were the controller
  86. rectangle._view = {
  87. base: 0,
  88. width: 4,
  89. x: 10,
  90. y: 15
  91. };
  92. expect(rectangle.tooltipPosition()).toEqual({
  93. x: 10,
  94. y: 15,
  95. });
  96. // Test when the y is below the base (negative bar)
  97. var negativeRectangle = new Chart.elements.Rectangle({
  98. _datasetIndex: 2,
  99. _index: 1
  100. });
  101. // Attach a view object as if we were the controller
  102. negativeRectangle._view = {
  103. base: -10,
  104. width: 4,
  105. x: 10,
  106. y: -15
  107. };
  108. expect(negativeRectangle.tooltipPosition()).toEqual({
  109. x: 10,
  110. y: -15,
  111. });
  112. });
  113. it ('should draw correctly', function() {
  114. var mockContext = window.createMockContext();
  115. var rectangle = new Chart.elements.Rectangle({
  116. _datasetIndex: 2,
  117. _index: 1,
  118. _chart: {
  119. ctx: mockContext,
  120. }
  121. });
  122. // Attach a view object as if we were the controller
  123. rectangle._view = {
  124. backgroundColor: 'rgb(255, 0, 0)',
  125. base: 0,
  126. borderColor: 'rgb(0, 0, 255)',
  127. borderWidth: 1,
  128. ctx: mockContext,
  129. width: 4,
  130. x: 10,
  131. y: 15,
  132. };
  133. rectangle.draw();
  134. expect(mockContext.getCalls()).toEqual([{
  135. name: 'beginPath',
  136. args: [],
  137. }, {
  138. name: 'setFillStyle',
  139. args: ['rgb(255, 0, 0)']
  140. }, {
  141. name: 'setStrokeStyle',
  142. args: ['rgb(0, 0, 255)'],
  143. }, {
  144. name: 'setLineWidth',
  145. args: [1]
  146. }, {
  147. name: 'moveTo',
  148. args: [8.5, 0]
  149. }, {
  150. name: 'lineTo',
  151. args: [8.5, 15.5]
  152. }, {
  153. name: 'lineTo',
  154. args: [11.5, 15.5]
  155. }, {
  156. name: 'lineTo',
  157. args: [11.5, 0]
  158. }, {
  159. name: 'fill',
  160. args: [],
  161. }, {
  162. name: 'stroke',
  163. args: []
  164. }]);
  165. });
  166. it ('should draw correctly with no stroke', function() {
  167. var mockContext = window.createMockContext();
  168. var rectangle = new Chart.elements.Rectangle({
  169. _datasetIndex: 2,
  170. _index: 1,
  171. _chart: {
  172. ctx: mockContext,
  173. }
  174. });
  175. // Attach a view object as if we were the controller
  176. rectangle._view = {
  177. backgroundColor: 'rgb(255, 0, 0)',
  178. base: 0,
  179. borderColor: 'rgb(0, 0, 255)',
  180. ctx: mockContext,
  181. width: 4,
  182. x: 10,
  183. y: 15,
  184. };
  185. rectangle.draw();
  186. expect(mockContext.getCalls()).toEqual([{
  187. name: 'beginPath',
  188. args: [],
  189. }, {
  190. name: 'setFillStyle',
  191. args: ['rgb(255, 0, 0)']
  192. }, {
  193. name: 'setStrokeStyle',
  194. args: ['rgb(0, 0, 255)'],
  195. }, {
  196. name: 'setLineWidth',
  197. args: [undefined]
  198. }, {
  199. name: 'moveTo',
  200. args: [8, 0]
  201. }, {
  202. name: 'lineTo',
  203. args: [8, 15]
  204. }, {
  205. name: 'lineTo',
  206. args: [12, 15]
  207. }, {
  208. name: 'lineTo',
  209. args: [12, 0]
  210. }, {
  211. name: 'fill',
  212. args: [],
  213. }]);
  214. });
  215. function testBorderSkipped (borderSkipped, expectedDrawCalls) {
  216. var mockContext = window.createMockContext();
  217. var rectangle = new Chart.elements.Rectangle({
  218. _chart: { ctx: mockContext }
  219. });
  220. // Attach a view object as if we were the controller
  221. rectangle._view = {
  222. borderSkipped: borderSkipped, // set tested 'borderSkipped' parameter
  223. ctx: mockContext,
  224. base: 0,
  225. width: 4,
  226. x: 10,
  227. y: 15,
  228. };
  229. rectangle.draw();
  230. var drawCalls = rectangle._view.ctx.getCalls().splice(4, 4);
  231. expect(drawCalls).toEqual(expectedDrawCalls);
  232. }
  233. it ('should draw correctly respecting "borderSkipped" == "bottom"', function() {
  234. testBorderSkipped ('bottom', [
  235. { name: 'moveTo', args: [8, 0] },
  236. { name: 'lineTo', args: [8, 15] },
  237. { name: 'lineTo', args: [12, 15] },
  238. { name: 'lineTo', args: [12, 0] },
  239. ]);
  240. });
  241. it ('should draw correctly respecting "borderSkipped" == "left"', function() {
  242. testBorderSkipped ('left', [
  243. { name: 'moveTo', args: [8, 15] },
  244. { name: 'lineTo', args: [12, 15] },
  245. { name: 'lineTo', args: [12, 0] },
  246. { name: 'lineTo', args: [8, 0] },
  247. ]);
  248. });
  249. it ('should draw correctly respecting "borderSkipped" == "top"', function() {
  250. testBorderSkipped ('top', [
  251. { name: 'moveTo', args: [12, 15] },
  252. { name: 'lineTo', args: [12, 0] },
  253. { name: 'lineTo', args: [8, 0] },
  254. { name: 'lineTo', args: [8, 15] },
  255. ]);
  256. });
  257. it ('should draw correctly respecting "borderSkipped" == "right"', function() {
  258. testBorderSkipped ('right', [
  259. { name: 'moveTo', args: [12, 0] },
  260. { name: 'lineTo', args: [8, 0] },
  261. { name: 'lineTo', args: [8, 15] },
  262. { name: 'lineTo', args: [12, 15] },
  263. ]);
  264. });
  265. });