jquery.flot.axislabels.Test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe('flot axis labels plugin', function() {
  4. var placeholder, plot;
  5. var options;
  6. beforeEach(function() {
  7. options = {
  8. xaxes: [
  9. { position: 'bottom', axisLabel: 'Bottom 1' },
  10. { position: 'top', axisLabel: 'Bottom 2', show: true },
  11. ],
  12. yaxes: [
  13. { position: 'left', axisLabel: 'Left' },
  14. { position: 'right', axisLabel: 'Right', show: true }
  15. ]
  16. };
  17. placeholder = setFixtures('<div id="test-container" style="width: 600px;height: 400px">')
  18. .find('#test-container');
  19. });
  20. it('creates a html text node for each axis label', function () {
  21. plot = $.plot(placeholder, [[]], options);
  22. var labels$ = $('.axisLabels'),
  23. labels = labels$.map(function(i, label) {
  24. return label.textContent;
  25. }).get();
  26. expect(labels.length).toBe(4);
  27. expect(labels).toContain(options.xaxes[0].axisLabel);
  28. expect(labels).toContain(options.xaxes[1].axisLabel);
  29. expect(labels).toContain(options.yaxes[0].axisLabel);
  30. expect(labels).toContain(options.yaxes[1].axisLabel);
  31. });
  32. it('doesn`t create a html text node for each axis label when the plugin is disabled', function () {
  33. options.axisLabels = {
  34. show: false
  35. };
  36. plot = $.plot(placeholder, [[]], options);
  37. var labels$ = $('.axisLabels'),
  38. labels = labels$.map(function(i, label) {
  39. return label.innerText;
  40. }).get();
  41. expect(labels.length).toBe(0);
  42. });
  43. it('shrinks the drawing area to make space for the axis labels', function () {
  44. plot = $.plot(placeholder, [[]], options);
  45. var width = plot.width(),
  46. height = plot.height();
  47. options.axisLabels = {
  48. show: false
  49. };
  50. plot = $.plot(placeholder, [[]], options);
  51. var widthNoLabels = plot.width(),
  52. heightNoLabels = plot.height();
  53. expect(widthNoLabels).toBeGreaterThan(width);
  54. expect(heightNoLabels).toBeGreaterThan(height);
  55. });
  56. it('centers the labels of x axes horizontally', function () {
  57. options.xaxes[0].axisLabel = 'short label';
  58. options.xaxes[1].axisLabel = 'very long axis label';
  59. plot = $.plot(placeholder, [[]], options);
  60. var box1 = $('.x1Label')[0].getBoundingClientRect(),
  61. box2 = $('.x2Label')[0].getBoundingClientRect();
  62. expect((box1.left + box1.width / 2) - (box2.left + box2.width / 2)).toBeLessThan(2);
  63. });
  64. it('centers the labels of y axes vertically', function () {
  65. options.yaxes[0].axisLabel = 'short label';
  66. options.yaxes[1].axisLabel = 'very long axis label';
  67. plot = $.plot(placeholder, [[]], options);
  68. var box1 = $('.y1Label')[0].getBoundingClientRect(),
  69. box2 = $('.y2Label')[0].getBoundingClientRect(),
  70. c1 = box1.top + box1.height / 2,
  71. c2 = box2.top + box2.height / 2;
  72. // The error on the y axes can be up to 1px because of the rotation
  73. expect(Math.abs(c1 - c2)).toBeLessThan(2);
  74. });
  75. it('should not duplicate the labels when the data is redrawn', function () {
  76. plot = $.plot(placeholder, [[1, 2, 3]], options);
  77. var labels$ = $('.axisLabels');
  78. expect(labels$.length).toBe(4);
  79. plot.setData([[4, 5, 6]]);
  80. plot.setupGrid();
  81. plot.draw();
  82. labels$ = $('.axisLabels');
  83. expect(labels$.length).toBe(4);
  84. });
  85. it('should not duplicate the labels when the plot is recreated', function () {
  86. plot = $.plot(placeholder, [[1, 2, 3]], options);
  87. var labels$ = $('.axisLabels');
  88. expect(labels$.length).toBe(4);
  89. plot = $.plot(placeholder, [[1, 2, 3]], options);
  90. labels$ = $('.axisLabels');
  91. expect(labels$.length).toBe(4);
  92. });
  93. it('should reserve extra space when axis.boxPosition is specified', function () {
  94. var size = 20,
  95. options = {
  96. xaxes: [
  97. { position: 'bottom', axisLabel: 'Bottom 1', boxPosition: {centerX: size, centerY: size} },
  98. { position: 'bottom', axisLabel: 'Bottom 2', show: true }
  99. ]
  100. };
  101. plot = $.plot(placeholder, [[1, 2, 3]], options);
  102. var axes = plot.getXAxes();
  103. expect(axes[0].labelHeight).toBe(axes[1].labelHeight + size);
  104. expect(axes[0].labelWidth).toBe(axes[1].labelWidth + size);
  105. });
  106. it('should reserve the specified space by axis.boxPosition even if axisLabel not visible', function () {
  107. var size = 20,
  108. options = {
  109. yaxes: [
  110. { position: 'right', boxPosition: {centerX: size, centerY: size} },
  111. { position: 'right', show: true }
  112. ]
  113. };
  114. plot = $.plot(placeholder, [[1, 2, 3]], options);
  115. var axes = plot.getYAxes();
  116. expect(axes[0].labelHeight).toBe(axes[1].labelHeight + size);
  117. expect(axes[0].labelWidth).toBe(axes[1].labelWidth + size);
  118. });
  119. });