title-spec.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. describe('c3 chart title', function () {
  2. 'use strict';
  3. var chart, config;
  4. describe('when given a title config option', function () {
  5. describe('with no padding and no position', function () {
  6. beforeEach(function(done) {
  7. config = {
  8. data: {
  9. columns: [
  10. ['data1', 30, 200, 100, 400, 150, 250]
  11. ]
  12. },
  13. title: {
  14. text: 'new title'
  15. }
  16. };
  17. chart = window.initChart(chart, config, done);
  18. });
  19. it('renders the title at the default config position', function () {
  20. var titleEl = d3.select(".c3-title");
  21. expect(+titleEl.attr("x")).toBeCloseTo(294, -2);
  22. expect(+titleEl.attr("y")).toEqual(titleEl.node().getBBox().height);
  23. });
  24. it('renders the title text', function () {
  25. var titleEl = d3.select(".c3-title");
  26. expect(titleEl.node().textContent).toEqual('new title');
  27. });
  28. });
  29. describe('with padding', function () {
  30. var config, getConfig = function (titlePosition) {
  31. return {
  32. data: {
  33. columns: [
  34. ['data1', 30, 200, 100, 400, 150, 250]
  35. ]
  36. },
  37. title: {
  38. text: 'positioned title',
  39. padding: {
  40. top: 20,
  41. right: 30,
  42. bottom: 40,
  43. left: 50
  44. },
  45. position: titlePosition
  46. }
  47. };
  48. };
  49. describe('and position center', function () {
  50. beforeEach(function(done) {
  51. config = getConfig('top-center');
  52. chart = window.initChart(chart, config, done);
  53. });
  54. it('renders the title at the default config position', function () {
  55. var titleEl = d3.select(".c3-title");
  56. expect(+titleEl.attr("x")).toBeCloseTo(275, -2);
  57. expect(+titleEl.attr("y")).toBeCloseTo(34, -1);
  58. });
  59. it('adds the correct amount of padding to fit the title', function() {
  60. expect(chart.internal.getCurrentPaddingTop()).toEqual(
  61. config.title.padding.top + d3.select('.c3-title').node().getBBox().height + config.title.padding.bottom
  62. );
  63. });
  64. });
  65. describe('and position left', function () {
  66. beforeEach(function(done) {
  67. config = getConfig('top-left');
  68. chart = window.initChart(chart, config, done);
  69. });
  70. it('renders the title at the default config position', function () {
  71. var titleEl = d3.select(".c3-title");
  72. expect(+titleEl.attr("x")).toBeCloseTo(50, -1);
  73. expect(+titleEl.attr("y")).toBeCloseTo(34, -1);
  74. });
  75. });
  76. describe('and position right', function () {
  77. beforeEach(function(done) {
  78. config = getConfig('top-right');
  79. chart = window.initChart(chart, config, done);
  80. });
  81. it('renders the title at the default config position', function () {
  82. var titleEl = d3.select(".c3-title");
  83. expect(+titleEl.attr("x")).toBeCloseTo(520, -2);
  84. expect(+titleEl.attr("y")).toBeCloseTo(34, -1);
  85. });
  86. });
  87. });
  88. });
  89. });