title-spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { d3, initChart } from './c3-helper'
  2. describe('c3 chart title', function() {
  3. 'use strict'
  4. var chart, config
  5. describe('when given a title too long', function() {
  6. beforeEach(function(done) {
  7. config = {
  8. data: {
  9. columns: [['data1', 30, 200, 100, 400, 150, 250]]
  10. },
  11. title: {
  12. text: 'this is a very long title'.repeat(10),
  13. position: 'top-center'
  14. }
  15. }
  16. chart = initChart(chart, config, done)
  17. })
  18. it('should not use negative x offset', function() {
  19. const titleEl = d3.select('.c3-title')
  20. expect(+titleEl.attr('x')).toBe(0)
  21. })
  22. })
  23. describe('when given a title config option', function() {
  24. describe('with no padding and no position', function() {
  25. beforeEach(function(done) {
  26. config = {
  27. data: {
  28. columns: [['data1', 30, 200, 100, 400, 150, 250]]
  29. },
  30. title: {
  31. text: 'new title'
  32. }
  33. }
  34. chart = initChart(chart, config, done)
  35. })
  36. it('renders the title at the default config position', function() {
  37. var titleEl = d3.select('.c3-title')
  38. expect(+titleEl.attr('x')).toBeCloseTo(294, -2)
  39. expect(+titleEl.attr('y')).toEqual(
  40. (titleEl.node() as any).getBBox().height
  41. )
  42. })
  43. it('renders the title text', function() {
  44. var titleEl = d3.select('.c3-title')
  45. expect((titleEl.node() as any).textContent).toEqual('new title')
  46. })
  47. })
  48. describe('with padding', function() {
  49. var config,
  50. getConfig = function(titlePosition) {
  51. return {
  52. data: {
  53. columns: [['data1', 30, 200, 100, 400, 150, 250]]
  54. },
  55. title: {
  56. text: 'positioned title',
  57. padding: {
  58. top: 20,
  59. right: 30,
  60. bottom: 40,
  61. left: 50
  62. },
  63. position: titlePosition
  64. }
  65. }
  66. }
  67. describe('and position center', function() {
  68. beforeEach(function(done) {
  69. config = getConfig('top-center')
  70. chart = initChart(chart, config, done)
  71. })
  72. it('renders the title at the default config position', function() {
  73. var titleEl = d3.select('.c3-title')
  74. expect(+titleEl.attr('x')).toBeCloseTo(275, -2)
  75. expect(+titleEl.attr('y')).toBeCloseTo(34, -1)
  76. })
  77. it('adds the correct amount of padding to fit the title', function() {
  78. expect(chart.internal.getCurrentPaddingTop()).toEqual(
  79. config.title.padding.top +
  80. (d3.select('.c3-title').node() as any).getBBox().height +
  81. config.title.padding.bottom
  82. )
  83. })
  84. })
  85. describe('and position left', function() {
  86. beforeEach(function(done) {
  87. config = getConfig('top-left')
  88. chart = initChart(chart, config, done)
  89. })
  90. it('renders the title at the default config position', function() {
  91. var titleEl = d3.select('.c3-title')
  92. expect(+titleEl.attr('x')).toBeCloseTo(50, -1)
  93. expect(+titleEl.attr('y')).toBeCloseTo(34, -1)
  94. })
  95. })
  96. describe('and position right', function() {
  97. beforeEach(function(done) {
  98. config = getConfig('top-right')
  99. chart = initChart(chart, config, done)
  100. })
  101. it('renders the title at the default config position', function() {
  102. var titleEl = d3.select('.c3-title')
  103. expect(+titleEl.attr('x')).toBeCloseTo(520, -2)
  104. expect(+titleEl.attr('y')).toBeCloseTo(34, -1)
  105. })
  106. })
  107. })
  108. })
  109. })