api.load-spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { d3, initChart } from './c3-helper'
  2. describe('c3 api load', function() {
  3. 'use strict'
  4. var chart, args
  5. beforeEach(function(done) {
  6. chart = initChart(chart, args, done)
  7. })
  8. describe('indexed data', function() {
  9. describe('as column', function() {
  10. beforeAll(function() {
  11. args = {
  12. data: {
  13. columns: [
  14. ['data1', 30, 200, 100, 400, 150, 250],
  15. ['data2', 5000, 2000, 1000, 4000, 1500, 2500]
  16. ]
  17. }
  18. }
  19. })
  20. it('should load additional data', function(done) {
  21. var main = chart.internal.main,
  22. legend = chart.internal.legend
  23. chart.load({
  24. columns: [['data3', 800, 500, 900, 500, 1000, 700]]
  25. })
  26. setTimeout(function() {
  27. var target = main.select('.c3-chart-line.c3-target.c3-target-data3'),
  28. legendItem = legend.select('.c3-legend-item.c3-legend-item-data3')
  29. expect(target.size()).toBe(1)
  30. expect(legendItem.size()).toBe(1)
  31. done()
  32. }, 500)
  33. })
  34. })
  35. })
  36. describe('category data', function() {
  37. beforeAll(function() {
  38. args = {
  39. data: {
  40. x: 'x',
  41. columns: [
  42. ['x', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6'],
  43. ['data1', 30, 200, 100, 400, 150, 250],
  44. ['data2', 5000, 2000, 1000, 4000, 1500, 2500]
  45. ]
  46. },
  47. axis: {
  48. x: {
  49. type: 'category'
  50. }
  51. }
  52. }
  53. })
  54. describe('as column', function() {
  55. it('should load additional data', function(done) {
  56. var main = chart.internal.main,
  57. legend = chart.internal.legend
  58. chart.load({
  59. columns: [['data3', 800, 500, 900, 500, 1000, 700]]
  60. })
  61. setTimeout(function() {
  62. var target = main.select('.c3-chart-line.c3-target.c3-target-data3'),
  63. legendItem = legend.select('.c3-legend-item.c3-legend-item-data3'),
  64. tickTexts = main.selectAll('.c3-axis-x g.tick text'),
  65. expected = ['cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6']
  66. expect(target.size()).toBe(1)
  67. expect(legendItem.size()).toBe(1)
  68. tickTexts.each(function(d, i) {
  69. var text = d3
  70. .select(this)
  71. .select('tspan')
  72. .text()
  73. expect(text).toBe(expected[i])
  74. })
  75. done()
  76. }, 500)
  77. })
  78. it('should load additional data', function(done) {
  79. var main = chart.internal.main,
  80. legend = chart.internal.legend
  81. chart.load({
  82. columns: [
  83. ['x', 'new1', 'new2', 'new3', 'new4', 'new5', 'new6'],
  84. ['data3', 800, 500, 900, 500, 1000, 700]
  85. ]
  86. })
  87. setTimeout(function() {
  88. var target = main.select('.c3-chart-line.c3-target.c3-target-data3'),
  89. legendItem = legend.select('.c3-legend-item.c3-legend-item-data3'),
  90. tickTexts = main.selectAll('.c3-axis-x g.tick text'),
  91. expected = ['new1', 'new2', 'new3', 'new4', 'new5', 'new6']
  92. expect(target.size()).toBe(1)
  93. expect(legendItem.size()).toBe(1)
  94. tickTexts.each(function(d, i) {
  95. var text = d3
  96. .select(this)
  97. .select('tspan')
  98. .text()
  99. expect(text).toBe(expected[i])
  100. })
  101. done()
  102. }, 500)
  103. })
  104. })
  105. })
  106. })