element.rectangle.tests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 get the correct vertical area', function() {
  114. var rectangle = new Chart.elements.Rectangle({
  115. _datasetIndex: 2,
  116. _index: 1
  117. });
  118. // Attach a view object as if we were the controller
  119. rectangle._view = {
  120. base: 0,
  121. width: 4,
  122. x: 10,
  123. y: 15
  124. };
  125. expect(rectangle.getArea()).toEqual(60);
  126. });
  127. it('should get the correct horizontal area', function() {
  128. var rectangle = new Chart.elements.Rectangle({
  129. _datasetIndex: 2,
  130. _index: 1
  131. });
  132. // Attach a view object as if we were the controller
  133. rectangle._view = {
  134. base: 0,
  135. height: 4,
  136. x: 10,
  137. y: 15
  138. };
  139. expect(rectangle.getArea()).toEqual(40);
  140. });
  141. it('should get the center', function() {
  142. var rectangle = new Chart.elements.Rectangle({
  143. _datasetIndex: 2,
  144. _index: 1
  145. });
  146. // Attach a view object as if we were the controller
  147. rectangle._view = {
  148. base: 0,
  149. width: 4,
  150. x: 10,
  151. y: 15
  152. };
  153. expect(rectangle.getCenterPoint()).toEqual({x: 10, y: 7.5});
  154. });
  155. });