element.arc.tests.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 draw correctly with no border', function() {
  54. var mockContext = window.createMockContext();
  55. var arc = new Chart.elements.Arc({
  56. _datasetIndex: 2,
  57. _index: 1,
  58. _chart: {
  59. ctx: mockContext,
  60. }
  61. });
  62. // Mock out the view as if the controller put it there
  63. arc._view = {
  64. startAngle: 0,
  65. endAngle: Math.PI / 2,
  66. x: 10,
  67. y: 5,
  68. innerRadius: 1,
  69. outerRadius: 3,
  70. backgroundColor: 'rgb(0, 0, 255)',
  71. borderColor: 'rgb(255, 0, 0)',
  72. };
  73. arc.draw();
  74. expect(mockContext.getCalls()).toEqual([{
  75. name: 'beginPath',
  76. args: []
  77. }, {
  78. name: 'arc',
  79. args: [10, 5, 3, 0, Math.PI / 2]
  80. }, {
  81. name: 'arc',
  82. args: [10, 5, 1, Math.PI / 2, 0, true]
  83. }, {
  84. name: 'closePath',
  85. args: []
  86. }, {
  87. name: 'setStrokeStyle',
  88. args: ['rgb(255, 0, 0)']
  89. }, {
  90. name: 'setLineWidth',
  91. args: [undefined]
  92. }, {
  93. name: 'setFillStyle',
  94. args: ['rgb(0, 0, 255)']
  95. }, {
  96. name: 'fill',
  97. args: []
  98. }, {
  99. name: 'setLineJoin',
  100. args: ['bevel']
  101. }]);
  102. });
  103. it ('should draw correctly with a border', function() {
  104. var mockContext = window.createMockContext();
  105. var arc = new Chart.elements.Arc({
  106. _datasetIndex: 2,
  107. _index: 1,
  108. _chart: {
  109. ctx: mockContext,
  110. }
  111. });
  112. // Mock out the view as if the controller put it there
  113. arc._view = {
  114. startAngle: 0,
  115. endAngle: Math.PI / 2,
  116. x: 10,
  117. y: 5,
  118. innerRadius: 1,
  119. outerRadius: 3,
  120. backgroundColor: 'rgb(0, 0, 255)',
  121. borderColor: 'rgb(255, 0, 0)',
  122. borderWidth: 5
  123. };
  124. arc.draw();
  125. expect(mockContext.getCalls()).toEqual([{
  126. name: 'beginPath',
  127. args: []
  128. }, {
  129. name: 'arc',
  130. args: [10, 5, 3, 0, Math.PI / 2]
  131. }, {
  132. name: 'arc',
  133. args: [10, 5, 1, Math.PI / 2, 0, true]
  134. }, {
  135. name: 'closePath',
  136. args: []
  137. }, {
  138. name: 'setStrokeStyle',
  139. args: ['rgb(255, 0, 0)']
  140. }, {
  141. name: 'setLineWidth',
  142. args: [5]
  143. }, {
  144. name: 'setFillStyle',
  145. args: ['rgb(0, 0, 255)']
  146. }, {
  147. name: 'fill',
  148. args: []
  149. }, {
  150. name: 'setLineJoin',
  151. args: ['bevel']
  152. }, {
  153. name: 'stroke',
  154. args: []
  155. }]);
  156. });
  157. });