core.title.tests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Test the rectangle element
  2. describe('Title block tests', function() {
  3. it('Should be constructed', function() {
  4. var title = new Chart.Title({});
  5. expect(title).not.toBe(undefined);
  6. });
  7. it('Should have the correct default config', function() {
  8. expect(Chart.defaults.global.title).toEqual({
  9. display: false,
  10. position: 'top',
  11. fullWidth: true,
  12. fontStyle: 'bold',
  13. padding: 10,
  14. text: ''
  15. })
  16. });
  17. it('should update correctly', function() {
  18. var chart = {};
  19. var options = Chart.helpers.clone(Chart.defaults.global.title);
  20. options.text = "My title";
  21. var title = new Chart.Title({
  22. chart: chart,
  23. options: options
  24. });
  25. var minSize = title.update(400, 200);
  26. expect(minSize).toEqual({
  27. width: 400,
  28. height: 0
  29. });
  30. // Now we have a height since we display
  31. title.options.display = true;
  32. minSize = title.update(400, 200);
  33. expect(minSize).toEqual({
  34. width: 400,
  35. height: 32
  36. });
  37. });
  38. it('should update correctly when vertical', function() {
  39. var chart = {};
  40. var options = Chart.helpers.clone(Chart.defaults.global.title);
  41. options.text = "My title";
  42. options.position = 'left';
  43. var title = new Chart.Title({
  44. chart: chart,
  45. options: options
  46. });
  47. var minSize = title.update(200, 400);
  48. expect(minSize).toEqual({
  49. width: 0,
  50. height: 400
  51. });
  52. // Now we have a height since we display
  53. title.options.display = true;
  54. minSize = title.update(200, 400);
  55. expect(minSize).toEqual({
  56. width: 32,
  57. height: 400
  58. });
  59. });
  60. it('should draw correctly horizontally', function() {
  61. var chart = {};
  62. var context = window.createMockContext();
  63. var options = Chart.helpers.clone(Chart.defaults.global.title);
  64. options.text = "My title";
  65. var title = new Chart.Title({
  66. chart: chart,
  67. options: options,
  68. ctx: context
  69. });
  70. title.update(400, 200);
  71. title.draw();
  72. expect(context.getCalls()).toEqual([]);
  73. // Now we have a height since we display
  74. title.options.display = true;
  75. var minSize = title.update(400, 200);
  76. title.top = 50;
  77. title.left = 100;
  78. title.bottom = title.top + minSize.height;
  79. title.right = title.left + minSize.width;
  80. title.draw();
  81. expect(context.getCalls()).toEqual([{
  82. name: 'setFillStyle',
  83. args: ['#666']
  84. }, {
  85. name: 'fillText',
  86. args: ['My title', 300, 66]
  87. }]);
  88. });
  89. it ('should draw correctly vertically', function() {
  90. var chart = {};
  91. var context = window.createMockContext();
  92. var options = Chart.helpers.clone(Chart.defaults.global.title);
  93. options.text = "My title";
  94. options.position = 'left';
  95. var title = new Chart.Title({
  96. chart: chart,
  97. options: options,
  98. ctx: context
  99. });
  100. title.update(200, 400);
  101. title.draw();
  102. expect(context.getCalls()).toEqual([]);
  103. // Now we have a height since we display
  104. title.options.display = true;
  105. var minSize = title.update(200, 400);
  106. title.top = 50;
  107. title.left = 100;
  108. title.bottom = title.top + minSize.height;
  109. title.right = title.left + minSize.width;
  110. title.draw();
  111. expect(context.getCalls()).toEqual([{
  112. name: 'setFillStyle',
  113. args: ['#666']
  114. }, {
  115. name: 'save',
  116. args: []
  117. }, {
  118. name: 'translate',
  119. args: [106, 250]
  120. }, {
  121. name: 'rotate',
  122. args: [-0.5 * Math.PI]
  123. }, {
  124. name: 'fillText',
  125. args: ['My title', 0, 0]
  126. }, {
  127. name: 'restore',
  128. args: []
  129. }]);
  130. // Rotation is other way on right side
  131. title.options.position = 'right';
  132. // Reset call tracker
  133. context.resetCalls();
  134. minSize = title.update(200, 400);
  135. title.top = 50;
  136. title.left = 100;
  137. title.bottom = title.top + minSize.height;
  138. title.right = title.left + minSize.width;
  139. title.draw();
  140. expect(context.getCalls()).toEqual([{
  141. name: 'setFillStyle',
  142. args: ['#666']
  143. }, {
  144. name: 'save',
  145. args: []
  146. }, {
  147. name: 'translate',
  148. args: [126, 250]
  149. }, {
  150. name: 'rotate',
  151. args: [0.5 * Math.PI]
  152. }, {
  153. name: 'fillText',
  154. args: ['My title', 0, 0]
  155. }, {
  156. name: 'restore',
  157. args: []
  158. }]);
  159. });
  160. });