api.data-spec.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. describe('c3 api data', function () {
  2. 'use strict';
  3. var chart;
  4. var args = {
  5. data: {
  6. columns: [
  7. ['data1', 30, 200, 100, 400, 150, 250],
  8. ['data2', 5000, 2000, 1000, 4000, 1500, 2500]
  9. ],
  10. names: {
  11. data1: 'Data Name 1',
  12. data2: 'Data Name 2'
  13. },
  14. colors: {
  15. data1: '#FF0000',
  16. data2: '#00FF00'
  17. },
  18. axes: {
  19. data1: 'y',
  20. data2: 'y2'
  21. }
  22. },
  23. axis: {
  24. y2: {
  25. show: true
  26. }
  27. }
  28. };
  29. beforeEach(function (done) {
  30. jasmine.addMatchers(customMatchers);
  31. chart = window.initChart(chart, args, done);
  32. });
  33. describe('data()', function () {
  34. it('should return all of data if no argument given', function () {
  35. var results = chart.data(),
  36. expected = ['data1', 'data2'];
  37. results.forEach(function (result, i) {
  38. expect(result.id).toBe(expected[i]);
  39. });
  40. });
  41. it('should return specifid data if string argument given', function () {
  42. var results = chart.data('data1');
  43. expect(results.length).toBe(1);
  44. expect(results[0].id).toBe('data1');
  45. });
  46. it('should return specifid data if array argument given', function () {
  47. var results = chart.data(['data1', 'data2']);
  48. expect(results.length).toBe(2);
  49. expect(results[0].id).toBe('data1');
  50. expect(results[1].id).toBe('data2');
  51. });
  52. });
  53. describe('data.shown()', function () {
  54. it('should return only shown targets', function () {
  55. var results;
  56. chart.hide('data1');
  57. results = chart.data.shown();
  58. expect(results.length).toBe(1);
  59. expect(results[0].id).toBe('data2');
  60. });
  61. });
  62. describe('data.values()', function () {
  63. it('should return values for specified target', function () {
  64. var values = chart.data.values('data1'),
  65. expectedValues = [30, 200, 100, 400, 150, 250];
  66. expect(values.length).toBe(6);
  67. values.forEach(function (v, i) {
  68. expect(v).toBe(expectedValues[i]);
  69. });
  70. });
  71. it('should return null when no args', function () {
  72. var values = chart.data.values();
  73. expect(values).toBeNull();
  74. });
  75. });
  76. describe('data.names()', function () {
  77. it('should return data.names specified as argument', function () {
  78. var results = chart.data.names();
  79. expect(results.data1).toBe('Data Name 1');
  80. expect(results.data2).toBe('Data Name 2');
  81. });
  82. it('should return data.names specified as api', function () {
  83. var results = chart.data.names({
  84. data1: 'New Data Name 1',
  85. data2: 'New Data Name 2'
  86. });
  87. expect(results.data1).toBe('New Data Name 1');
  88. expect(results.data2).toBe('New Data Name 2');
  89. });
  90. it('should set data.names specified as api', function () {
  91. expect(d3.select('.c3-legend-item-data1 text').text()).toBe("New Data Name 1");
  92. expect(d3.select('.c3-legend-item-data2 text').text()).toBe("New Data Name 2");
  93. });
  94. });
  95. describe('data.colors()', function () {
  96. it('should return data.colors specified as argument', function () {
  97. var results = chart.data.colors();
  98. expect(results.data1).toBeHexOrRGB('#FF0000');
  99. expect(results.data2).toBeHexOrRGB('#00FF00');
  100. });
  101. it('should return data.colors specified as api', function () {
  102. var results = chart.data.colors({
  103. data1: '#00FF00',
  104. data2: '#FF0000'
  105. });
  106. expect(results.data1).toBeHexOrRGB('#00FF00');
  107. expect(results.data2).toBeHexOrRGB('#FF0000');
  108. });
  109. it('should set data.colors specified as api', function () {
  110. expect(d3.select('.c3-line-data1').style('stroke')).toBeHexOrRGB("#00ff00");
  111. expect(d3.select('.c3-line-data2').style('stroke')).toBeHexOrRGB("#ff0000");
  112. expect(d3.select('.c3-legend-item-data1 .c3-legend-item-tile').style('stroke')).toBeHexOrRGB("#00ff00");
  113. expect(d3.select('.c3-legend-item-data2 .c3-legend-item-tile').style('stroke')).toBeHexOrRGB("#ff0000");
  114. });
  115. });
  116. describe('data.axes()', function () {
  117. it('should return data.axes specified as argument', function () {
  118. var results = chart.data.axes();
  119. expect(results.data1).toBe('y');
  120. expect(results.data2).toBe('y2');
  121. expect(d3.select('.c3-axis-y g.tick text').text()).toBe('0');
  122. expect(d3.select('.c3-axis-y2 g.tick text').text()).toBe('1000');
  123. });
  124. it('should return data.axes specified as api', function () {
  125. var results = chart.data.axes({
  126. data1: 'y2',
  127. data2: 'y'
  128. });
  129. expect(results.data1).toBe('y2');
  130. expect(results.data2).toBe('y');
  131. expect(d3.select('.c3-axis-y g.tick text').text()).toBe('1000');
  132. expect(d3.select('.c3-axis-y2 g.tick text').text()).toBe('0');
  133. });
  134. });
  135. });
  136. var customMatchers = {
  137. toBeHexOrRGB: function(util, customEqualityTesters) {
  138. 'use strict';
  139. function rgb2hex(rgb){
  140. rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
  141. return (rgb && rgb.length === 4) ? "#" +
  142. ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
  143. ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
  144. ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
  145. }
  146. return {
  147. compare: function(actual, expected){
  148. if (expected === undefined) {
  149. expected = '';
  150. }
  151. var result = {};
  152. actual = actual.match('rgb') ? rgb2hex(actual) : actual;
  153. expected = expected.match('rgb') ? rgb2hex(expected) : expected;
  154. result.pass = util.equals(actual, expected, customEqualityTesters);
  155. if (result.pass) {
  156. result.message = "Expected " + actual + " not to be quite so goofy";
  157. } else {
  158. result.message = "Expected " + actual + " to be goofy, but it was not very goofy";
  159. }
  160. return result;
  161. }
  162. };
  163. }
  164. };