element.point.tests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. describe('Chart.elements.Point', function() {
  2. describe('auto', jasmine.fixture.specs('element.point'));
  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 get the correct area', function() {
  57. var point = new Chart.elements.Point({
  58. _datasetIndex: 2,
  59. _index: 1
  60. });
  61. // Attach a view object as if we were the controller
  62. point._view = {
  63. radius: 2,
  64. };
  65. expect(point.getArea()).toEqual(Math.PI * 4);
  66. });
  67. it('should get the correct center point', function() {
  68. var point = new Chart.elements.Point({
  69. _datasetIndex: 2,
  70. _index: 1
  71. });
  72. // Attach a view object as if we were the controller
  73. point._view = {
  74. radius: 2,
  75. x: 10,
  76. y: 10
  77. };
  78. expect(point.getCenterPoint()).toEqual({x: 10, y: 10});
  79. });
  80. it ('should draw correctly with default settings if necessary', function() {
  81. var mockContext = window.createMockContext();
  82. var point = new Chart.elements.Point({
  83. _datasetIndex: 2,
  84. _index: 1,
  85. _chart: {
  86. ctx: mockContext,
  87. }
  88. });
  89. // Attach a view object as if we were the controller
  90. point._view = {
  91. radius: 2,
  92. hitRadius: 3,
  93. x: 10,
  94. y: 15,
  95. ctx: mockContext
  96. };
  97. point.draw();
  98. expect(mockContext.getCalls()).toEqual([{
  99. name: 'setStrokeStyle',
  100. args: ['rgba(0,0,0,0.1)']
  101. }, {
  102. name: 'setLineWidth',
  103. args: [1]
  104. }, {
  105. name: 'setFillStyle',
  106. args: ['rgba(0,0,0,0.1)']
  107. }, {
  108. name: 'beginPath',
  109. args: []
  110. }, {
  111. name: 'arc',
  112. args: [10, 15, 2, 0, 2 * Math.PI]
  113. }, {
  114. name: 'closePath',
  115. args: [],
  116. }, {
  117. name: 'fill',
  118. args: [],
  119. }, {
  120. name: 'stroke',
  121. args: []
  122. }]);
  123. });
  124. it ('should not draw if skipped', function() {
  125. var mockContext = window.createMockContext();
  126. var point = new Chart.elements.Point({
  127. _datasetIndex: 2,
  128. _index: 1,
  129. _chart: {
  130. ctx: mockContext,
  131. }
  132. });
  133. // Attach a view object as if we were the controller
  134. point._view = {
  135. radius: 2,
  136. hitRadius: 3,
  137. x: 10,
  138. y: 15,
  139. ctx: mockContext,
  140. skip: true
  141. };
  142. point.draw();
  143. expect(mockContext.getCalls()).toEqual([]);
  144. });
  145. });