core-spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { d3, initChart } from './c3-helper'
  2. describe('c3 chart', function() {
  3. 'use strict'
  4. var chart
  5. var args: any = {
  6. svg: {
  7. classname: 'customclass'
  8. },
  9. data: {
  10. columns: [
  11. ['data1', 30, 200, 100, 400, 150, 250],
  12. ['data2', 50, 20, 10, 40, 15, 25],
  13. ['data3', 150, 120, 110, 140, 115, 125]
  14. ]
  15. }
  16. }
  17. beforeEach(function(done) {
  18. chart = initChart(chart, args, done)
  19. })
  20. describe('init', function() {
  21. it('should be created', function() {
  22. var svg = d3.select('#chart svg')
  23. expect(svg).not.toBeNull()
  24. })
  25. it('should bind to window focus event', done => {
  26. const addEventListener = window.addEventListener
  27. window.addEventListener = (event, handler) => {
  28. if (event === 'focus') {
  29. setTimeout(() => {
  30. expect(handler).toBe(chart.internal.windowFocusHandler)
  31. window.addEventListener = addEventListener // restore the original
  32. done()
  33. }, 10)
  34. }
  35. }
  36. chart = initChart(chart, args, () => {})
  37. })
  38. describe('should set 3rd party property to Function', function() {
  39. beforeAll(function() {
  40. ;(Function.prototype as any).$extIsFunction = true
  41. })
  42. it('should be created even if 3rd party property has been set', function() {
  43. var svg = d3.select('#chart svg')
  44. expect(svg).not.toBeNull()
  45. })
  46. it('should be created with a custom class', function() {
  47. var svg = d3.select('#chart svg')
  48. expect(svg.attr('class')).not.toBeNull()
  49. expect(svg.attr('class')).toBe('customclass')
  50. })
  51. })
  52. })
  53. describe('size', function() {
  54. it('should have same width', function() {
  55. var svg = d3.select('#chart svg')
  56. expect(+svg.attr('width')).toBe(640)
  57. })
  58. it('should have same height', function() {
  59. var svg = d3.select('#chart svg')
  60. expect(+svg.attr('height')).toBe(480)
  61. })
  62. })
  63. describe('call resize and resized callbacks', function() {
  64. beforeAll(function() {
  65. args.bindto = '#chart'
  66. args.axis = {
  67. rotated: true
  68. }
  69. args.resize_var = false
  70. args.resized_var = false
  71. args.onresize = function() {
  72. args.resize_var = true
  73. }
  74. args.onresized = function() {
  75. args.resized_var = true
  76. }
  77. })
  78. it('arbitrary parameters should be false before resize', function() {
  79. expect(args.resize_var).toBe(false)
  80. expect(args.resized_var).toBe(false)
  81. })
  82. it('arbitrary parameters should be true after resize', function() {
  83. window.dispatchEvent(new Event('resize'))
  84. expect(args.resize_var).toBe(true)
  85. expect(args.resized_var).toBe(true)
  86. })
  87. })
  88. describe('bindto', function() {
  89. describe('selector', function() {
  90. beforeAll(function() {
  91. d3.select('#chart').html('')
  92. args.bindto = '#chart'
  93. })
  94. it('should be created', function() {
  95. var svg = d3.select('#chart svg')
  96. expect(svg.size()).toBe(1)
  97. })
  98. })
  99. describe('d3.selection object', function() {
  100. beforeAll(function() {
  101. d3.select('#chart').html('')
  102. args.bindto = d3.select('#chart')
  103. })
  104. it('should be created', function() {
  105. var svg = d3.select('#chart svg')
  106. expect(svg.size()).toBe(1)
  107. })
  108. })
  109. describe('null', function() {
  110. beforeAll(function() {
  111. d3.select('#chart').html('')
  112. args.bindto = null
  113. })
  114. it('should not be created', function() {
  115. var svg = d3.select('#chart svg')
  116. expect(svg.size()).toBe(0)
  117. })
  118. })
  119. describe('empty string', function() {
  120. beforeAll(function() {
  121. d3.select('#chart').html('')
  122. args.bindto = ''
  123. })
  124. it('should not be created', function() {
  125. var svg = d3.select('#chart svg')
  126. expect(svg.size()).toBe(0)
  127. })
  128. })
  129. describe('bind to selector with rotated axis', function() {
  130. beforeAll(function() {
  131. args.bindto = '#chart'
  132. args.axis = {
  133. rotated: true
  134. }
  135. })
  136. it('should be created', function() {
  137. var svg = d3.select('#chart svg')
  138. expect(svg.size()).toBe(1)
  139. })
  140. })
  141. })
  142. describe('empty data', function() {
  143. beforeAll(function() {
  144. args = {
  145. data: {
  146. columns: [['data1'], ['data2']]
  147. }
  148. }
  149. })
  150. it('should generate a chart', function() {
  151. var ticks = chart.internal.main.select('.c3-axis-x').selectAll('g.tick')
  152. expect(ticks.size()).toBe(0)
  153. })
  154. describe('more empty data', function() {
  155. beforeAll(function() {
  156. args = {
  157. data: {
  158. x: 'x',
  159. columns: [['x'], ['data1'], ['data2']]
  160. },
  161. axis: {
  162. x: {
  163. type: 'timeseries'
  164. }
  165. }
  166. }
  167. })
  168. it('should generate a chart', function() {
  169. var ticks = chart.internal.main.select('.c3-axis-x').selectAll('g.tick')
  170. expect(ticks.size()).toBe(0)
  171. })
  172. })
  173. })
  174. })