api.load-spec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. describe('c3 api load', function () {
  2. 'use strict';
  3. var chart, args;
  4. beforeEach(function (done) {
  5. chart = window.initChart(chart, args, done);
  6. });
  7. describe('indexed data', function () {
  8. describe('as column', function () {
  9. beforeAll(function () {
  10. args = {
  11. data: {
  12. columns: [
  13. ['data1', 30, 200, 100, 400, 150, 250],
  14. ['data2', 5000, 2000, 1000, 4000, 1500, 2500]
  15. ]
  16. }
  17. };
  18. });
  19. it('should load additional data', function (done) {
  20. var main = chart.internal.main,
  21. legend = chart.internal.legend;
  22. chart.load({
  23. columns: [
  24. ['data3', 800, 500, 900, 500, 1000, 700]
  25. ]
  26. });
  27. setTimeout(function () {
  28. var target = main.select('.c3-chart-line.c3-target.c3-target-data3'),
  29. legendItem = legend.select('.c3-legend-item.c3-legend-item-data3');
  30. expect(target.size()).toBe(1);
  31. expect(legendItem.size()).toBe(1);
  32. done();
  33. }, 500);
  34. });
  35. });
  36. });
  37. describe('category data', function () {
  38. beforeAll(function () {
  39. args = {
  40. data: {
  41. x: 'x',
  42. columns: [
  43. ['x', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6'],
  44. ['data1', 30, 200, 100, 400, 150, 250],
  45. ['data2', 5000, 2000, 1000, 4000, 1500, 2500]
  46. ]
  47. },
  48. axis: {
  49. x: {
  50. type: 'category'
  51. }
  52. }
  53. };
  54. });
  55. describe('as column', function () {
  56. it('should load additional data', function (done) {
  57. var main = chart.internal.main,
  58. legend = chart.internal.legend;
  59. chart.load({
  60. columns: [
  61. ['data3', 800, 500, 900, 500, 1000, 700]
  62. ]
  63. });
  64. setTimeout(function () {
  65. var target = main.select('.c3-chart-line.c3-target.c3-target-data3'),
  66. legendItem = legend.select('.c3-legend-item.c3-legend-item-data3'),
  67. tickTexts = main.selectAll('.c3-axis-x g.tick text'),
  68. expected = ['cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6'];
  69. expect(target.size()).toBe(1);
  70. expect(legendItem.size()).toBe(1);
  71. tickTexts.each(function (d, i) {
  72. var text = d3.select(this).select('tspan').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.select(this).select('tspan').text();
  96. expect(text).toBe(expected[i]);
  97. });
  98. done();
  99. }, 500);
  100. });
  101. });
  102. });
  103. });