List.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. describe('List', function () {
  2. var utHelper = window.utHelper;
  3. var testCase = utHelper.prepare(['echarts/data/List']);
  4. describe('Data Manipulation', function () {
  5. testCase('initData 1d', function (List) {
  6. var list = new List(['x', 'y']);
  7. list.initData([10, 20, 30]);
  8. expect(list.get('x', 0)).toEqual(10);
  9. expect(list.get('x', 1)).toEqual(20);
  10. expect(list.get('x', 2)).toEqual(30);
  11. expect(list.get('y', 1)).toEqual(20);
  12. });
  13. testCase('initData 2d', function (List) {
  14. var list = new List(['x', 'y']);
  15. list.initData([[10, 15], [20, 25], [30, 35]]);
  16. expect(list.get('x', 1)).toEqual(20);
  17. expect(list.get('y', 1)).toEqual(25);
  18. });
  19. testCase('initData 2d yx', function (List) {
  20. var list = new List(['y', 'x']);
  21. list.initData([[10, 15], [20, 25], [30, 35]]);
  22. expect(list.get('x', 1)).toEqual(25);
  23. expect(list.get('y', 1)).toEqual(20);
  24. });
  25. testCase('Data with option 1d', function (List) {
  26. var list = new List(['x', 'y']);
  27. list.initData([1, {
  28. value: 2,
  29. somProp: 'foo'
  30. }]);
  31. expect(list.getItemModel(1).get('somProp')).toEqual('foo');
  32. expect(list.getItemModel(0).get('somProp')).toBeNull();
  33. });
  34. testCase('Empty data', function (List) {
  35. var list = new List(['x', 'y']);
  36. list.initData([1, '-']);
  37. expect(list.get('y', 1)).toBeNaN();
  38. });
  39. testCase('Stacked data', function (List) {
  40. var list1 = new List(['x', {
  41. name: 'y',
  42. stackable: true
  43. }]);
  44. var list2 = new List(['x', {
  45. name: 'y',
  46. stackable: true
  47. }]);
  48. list1.initData([1, '-', 2, -2]);
  49. list2.initData([1, 2, 3, 2]);
  50. list2.stackedOn = list1;
  51. expect(list2.get('y', 1, true)).toEqual(2);
  52. expect(list2.get('y', 2, true)).toEqual(5);
  53. expect(list2.get('y', 3, true)).toEqual(2);
  54. });
  55. testCase('getRawValue', function (List) {
  56. var list = new List(['x', 'y']);
  57. list.initData([1, 2, 3]);
  58. expect(list.getItemModel(1).option).toEqual(2);
  59. list.initData([[10, 15], [20, 25], [30, 35]]);
  60. expect(list.getItemModel(1).option).toEqual([20, 25]);
  61. });
  62. testCase('getDataExtent', function (List) {
  63. var list = new List(['x', 'y']);
  64. list.initData([1, 2, 3]);
  65. expect(list.getDataExtent('x')).toEqual([1, 3]);
  66. expect(list.getDataExtent('y')).toEqual([1, 3]);
  67. });
  68. testCase('Data types', function (List) {
  69. var list = new List([{
  70. name: 'x',
  71. type: 'int'
  72. }, {
  73. name: 'y',
  74. type: 'float'
  75. }]);
  76. list.initData([[1.1, 1.1]]);
  77. expect(list.get('x', 0)).toEqual(1);
  78. expect(list.get('y', 0)).toBeCloseTo(1.1, 5);
  79. });
  80. testCase('map', function (List) {
  81. var list = new List(['x', 'y']);
  82. list.initData([[10, 15], [20, 25], [30, 35]]);
  83. expect(list.map(['x', 'y'], function (x, y) {
  84. return [x + 2, y + 2];
  85. }).mapArray('x', function (x) {
  86. return x;
  87. })).toEqual([12, 22, 32]);
  88. });
  89. testCase('mapArray', function (List) {
  90. var list = new List(['x', 'y']);
  91. list.initData([[10, 15], [20, 25], [30, 35]]);
  92. expect(list.mapArray(['x', 'y'], function (x, y) {
  93. return [x, y];
  94. })).toEqual([[10, 15], [20, 25], [30, 35]]);
  95. });
  96. testCase('filterSelf', function (List) {
  97. var list = new List(['x', 'y']);
  98. list.initData([[10, 15], [20, 25], [30, 35]]);
  99. expect(list.filterSelf(['x', 'y'], function (x, y) {
  100. return x < 30 && x > 10;
  101. }).mapArray('x', function (x) {
  102. return x;
  103. })).toEqual([20]);
  104. });
  105. });
  106. });