element.arc.tests.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Test the rectangle element
  2. describe('Arc element tests', function() {
  3. it ('Should be constructed', function() {
  4. var arc = new Chart.elements.Arc({
  5. _datasetIndex: 2,
  6. _index: 1
  7. });
  8. expect(arc).not.toBe(undefined);
  9. expect(arc._datasetIndex).toBe(2);
  10. expect(arc._index).toBe(1);
  11. });
  12. it ('should determine if in range', function() {
  13. var arc = new Chart.elements.Arc({
  14. _datasetIndex: 2,
  15. _index: 1
  16. });
  17. // Make sure we can run these before the view is added
  18. expect(arc.inRange(2, 2)).toBe(false);
  19. expect(arc.inLabelRange(2)).toBe(false);
  20. // Mock out the view as if the controller put it there
  21. arc._view = {
  22. startAngle: 0,
  23. endAngle: Math.PI / 2,
  24. x: 0,
  25. y: 0,
  26. innerRadius: 5,
  27. outerRadius: 10,
  28. };
  29. expect(arc.inRange(2, 2)).toBe(false);
  30. expect(arc.inRange(7, 0)).toBe(true);
  31. expect(arc.inRange(0, 11)).toBe(false);
  32. expect(arc.inRange(Math.sqrt(32), Math.sqrt(32))).toBe(true);
  33. expect(arc.inRange(-1.0 * Math.sqrt(7), Math.sqrt(7))).toBe(false);
  34. });
  35. it ('should get the tooltip position', function() {
  36. var arc = new Chart.elements.Arc({
  37. _datasetIndex: 2,
  38. _index: 1
  39. });
  40. // Mock out the view as if the controller put it there
  41. arc._view = {
  42. startAngle: 0,
  43. endAngle: Math.PI / 2,
  44. x: 0,
  45. y: 0,
  46. innerRadius: 0,
  47. outerRadius: Math.sqrt(2),
  48. };
  49. var pos = arc.tooltipPosition();
  50. expect(pos.x).toBeCloseTo(0.5);
  51. expect(pos.y).toBeCloseTo(0.5);
  52. });
  53. it ('should get the area', function() {
  54. var arc = new Chart.elements.Arc({
  55. _datasetIndex: 2,
  56. _index: 1
  57. });
  58. // Mock out the view as if the controller put it there
  59. arc._view = {
  60. startAngle: 0,
  61. endAngle: Math.PI / 2,
  62. x: 0,
  63. y: 0,
  64. innerRadius: 0,
  65. outerRadius: Math.sqrt(2),
  66. };
  67. expect(arc.getArea()).toBeCloseTo(0.5 * Math.PI, 6);
  68. });
  69. it ('should get the center', function() {
  70. var arc = new Chart.elements.Arc({
  71. _datasetIndex: 2,
  72. _index: 1
  73. });
  74. // Mock out the view as if the controller put it there
  75. arc._view = {
  76. startAngle: 0,
  77. endAngle: Math.PI / 2,
  78. x: 0,
  79. y: 0,
  80. innerRadius: 0,
  81. outerRadius: Math.sqrt(2),
  82. };
  83. var center = arc.getCenterPoint();
  84. expect(center.x).toBeCloseTo(0.5, 6);
  85. expect(center.y).toBeCloseTo(0.5, 6);
  86. });
  87. it ('should draw correctly with no border', function() {
  88. var mockContext = window.createMockContext();
  89. var arc = new Chart.elements.Arc({
  90. _datasetIndex: 2,
  91. _index: 1,
  92. _chart: {
  93. ctx: mockContext,
  94. }
  95. });
  96. // Mock out the view as if the controller put it there
  97. arc._view = {
  98. startAngle: 0,
  99. endAngle: Math.PI / 2,
  100. x: 10,
  101. y: 5,
  102. innerRadius: 1,
  103. outerRadius: 3,
  104. backgroundColor: 'rgb(0, 0, 255)',
  105. borderColor: 'rgb(255, 0, 0)',
  106. };
  107. arc.draw();
  108. expect(mockContext.getCalls()).toEqual([{
  109. name: 'beginPath',
  110. args: []
  111. }, {
  112. name: 'arc',
  113. args: [10, 5, 3, 0, Math.PI / 2]
  114. }, {
  115. name: 'arc',
  116. args: [10, 5, 1, Math.PI / 2, 0, true]
  117. }, {
  118. name: 'closePath',
  119. args: []
  120. }, {
  121. name: 'setStrokeStyle',
  122. args: ['rgb(255, 0, 0)']
  123. }, {
  124. name: 'setLineWidth',
  125. args: [undefined]
  126. }, {
  127. name: 'setFillStyle',
  128. args: ['rgb(0, 0, 255)']
  129. }, {
  130. name: 'fill',
  131. args: []
  132. }, {
  133. name: 'setLineJoin',
  134. args: ['bevel']
  135. }]);
  136. });
  137. it ('should draw correctly with a border', function() {
  138. var mockContext = window.createMockContext();
  139. var arc = new Chart.elements.Arc({
  140. _datasetIndex: 2,
  141. _index: 1,
  142. _chart: {
  143. ctx: mockContext,
  144. }
  145. });
  146. // Mock out the view as if the controller put it there
  147. arc._view = {
  148. startAngle: 0,
  149. endAngle: Math.PI / 2,
  150. x: 10,
  151. y: 5,
  152. innerRadius: 1,
  153. outerRadius: 3,
  154. backgroundColor: 'rgb(0, 0, 255)',
  155. borderColor: 'rgb(255, 0, 0)',
  156. borderWidth: 5
  157. };
  158. arc.draw();
  159. expect(mockContext.getCalls()).toEqual([{
  160. name: 'beginPath',
  161. args: []
  162. }, {
  163. name: 'arc',
  164. args: [10, 5, 3, 0, Math.PI / 2]
  165. }, {
  166. name: 'arc',
  167. args: [10, 5, 1, Math.PI / 2, 0, true]
  168. }, {
  169. name: 'closePath',
  170. args: []
  171. }, {
  172. name: 'setStrokeStyle',
  173. args: ['rgb(255, 0, 0)']
  174. }, {
  175. name: 'setLineWidth',
  176. args: [5]
  177. }, {
  178. name: 'setFillStyle',
  179. args: ['rgb(0, 0, 255)']
  180. }, {
  181. name: 'fill',
  182. args: []
  183. }, {
  184. name: 'setLineJoin',
  185. args: ['bevel']
  186. }, {
  187. name: 'stroke',
  188. args: []
  189. }]);
  190. });
  191. });