jquery.flot.legend.Test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe("flot legend plugin", function() {
  4. var placeholder, plot;
  5. var options, legendContainer, legendSettings
  6. beforeEach(function() {
  7. var legendSettings = {
  8. position: "nw",
  9. show: true,
  10. container: null
  11. };
  12. options = {
  13. legend: legendSettings,
  14. series: {
  15. shadowSize: 0, // don't draw shadows
  16. }
  17. };
  18. placeholder = setFixtures('<div id="test-container" style="width: 600px;height: 400px">')
  19. .find('#test-container');
  20. });
  21. var positions = ['nw', 'ne', 'sw', 'se'];
  22. positions.forEach(function (pos) {
  23. it ('shold draw a legend over graph at cardinal position: ' + pos + ', if a container is not provided', function () {
  24. options.legend.position = pos;
  25. plot = $.plot(placeholder, [[1, 3, 5, 6]], options);
  26. var legend = document.getElementsByClassName('legend')[0];
  27. expect(legend.style.position).toBe('absolute');
  28. switch (pos) {
  29. case "nw":
  30. expect(legend.style.top).toContain('px')
  31. expect(legend.style.bottom).toBe('');
  32. expect(legend.style.left).toContain('px')
  33. expect(legend.style.right).toBe('');
  34. break;
  35. case "ne":
  36. expect(legend.style.top).toContain('px')
  37. expect(legend.style.bottom).toBe('');
  38. expect(legend.style.left).toBe('');
  39. expect(legend.style.right).toContain('px')
  40. break;
  41. case "sw":
  42. expect(legend.style.top).toBe('');
  43. expect(legend.style.bottom).toContain('px')
  44. expect(legend.style.left).toContain('px')
  45. expect(legend.style.right).toBe('');
  46. break;
  47. case "se":
  48. expect(legend.style.top).toBe('');
  49. expect(legend.style.bottom).toContain('px')
  50. expect(legend.style.left).toBe('');
  51. expect(legend.style.right).toContain('px')
  52. break;
  53. }
  54. });
  55. });
  56. it('should draw the legend inside the container if one is provided', function(){
  57. var legendContainer = document.createElement("div");
  58. document.body.appendChild(legendContainer);
  59. options.legend.container = legendContainer;
  60. plot = $.plot(placeholder, [[1, 3, 5, 6]], options);
  61. expect(legendContainer.style.width).toContain('em');
  62. expect(legendContainer.style.height).toContain('em');
  63. document.body.removeChild(legendContainer);
  64. });
  65. it('should assign a default plot label if none is provided', function(){
  66. plot = $.plot(placeholder, [[1, 3, 5, 6]], options);
  67. var legendSvg = document.getElementsByClassName('legendLayer')[0];
  68. var firstLegendEntry = legendSvg.getElementsByTagNameNS('http://www.w3.org/2000/svg', 'g')[0];
  69. var entryLabel = firstLegendEntry.getElementsByTagNameNS('http://www.w3.org/2000/svg', 'text')[0];
  70. expect(entryLabel.textContent).toBe('Plot 1');
  71. });
  72. it('should display the plot label', function(){
  73. var label = 'custom label';
  74. options.series.label = label;
  75. plot = $.plot(placeholder, [[1, 3, 5, 6]], options);
  76. var legendSvg = document.getElementsByClassName('legendLayer')[0];
  77. var firstLegendEntry = legendSvg.getElementsByTagNameNS('http://www.w3.org/2000/svg', 'g')[0];
  78. var entryLabel = firstLegendEntry.getElementsByTagNameNS('http://www.w3.org/2000/svg', 'text')[0];
  79. expect(entryLabel.textContent).toBe(label);
  80. });
  81. it('should take into account the show option', function() {
  82. options.legend.show = false;
  83. plot = $.plot(placeholder, [[1, 3, 5, 6]], options);
  84. var legendSvg = document.getElementsByClassName('legendLayer')[0];
  85. expect(legendSvg).toBe(undefined);
  86. });
  87. });