layout.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. describe('util/number', function () {
  2. var utHelper = window.utHelper;
  3. var testCase = utHelper.prepare(['echarts/src/util/layout']);
  4. describe('mergeLayoutParam', function () {
  5. // The given obj has exactly the given props, has no other props.
  6. function expectPropsEqual(obj, props) {
  7. expect(propContain(obj, props) && propContain(props, obj)).toEqual(true);
  8. }
  9. function propContain(more, less) {
  10. for (var key in more) {
  11. if (more.hasOwnProperty(key)) {
  12. if (more[key] !== less[key]
  13. && !(more[key] == null && less[key] == null)
  14. ) {
  15. return false;
  16. }
  17. }
  18. }
  19. return true;
  20. }
  21. function shadowClone(obj) {
  22. var newObj = {};
  23. for (var key in obj) {
  24. if (obj.hasOwnProperty(key)) {
  25. newObj[key] = obj[key];
  26. }
  27. }
  28. return newObj;
  29. }
  30. testCase('all', function (layoutUtil) {
  31. function testMerge(targetOption, newOption, result, resultIgnoreSize) {
  32. var t1 = shadowClone(targetOption);
  33. var t2 = shadowClone(targetOption);
  34. var n1 = shadowClone(newOption);
  35. var n2 = shadowClone(newOption);
  36. layoutUtil.mergeLayoutParam(t1, n1);
  37. layoutUtil.mergeLayoutParam(t2, n2, {ignoreSize: true});
  38. expectPropsEqual(t1, result);
  39. expectPropsEqual(t2, resultIgnoreSize || result);
  40. }
  41. function singleValueAdd(val) {
  42. testMerge({}, {width: val}, {width: val});
  43. testMerge({}, {left: val}, {left: val});
  44. testMerge({}, {right: val}, {right: val});
  45. testMerge({}, {height: val}, {height: val});
  46. testMerge({}, {top: val}, {top: val});
  47. testMerge({}, {bottom: val}, {bottom: val});
  48. }
  49. singleValueAdd(10);
  50. singleValueAdd('30%');
  51. singleValueAdd('left');
  52. singleValueAdd('right');
  53. singleValueAdd('center');
  54. function singleValueReplace(val) {
  55. testMerge({width: -999}, {width: val}, {width: val});
  56. testMerge({left: -999}, {left: val}, {left: val});
  57. testMerge({right: -999}, {right: val}, {right: val});
  58. testMerge({height: -999}, {height: val}, {height: val});
  59. testMerge({top: -999}, {top: val}, {top: val});
  60. testMerge({bottom: -999}, {bottom: val}, {bottom: val});
  61. }
  62. singleValueReplace(10);
  63. singleValueReplace('30%');
  64. singleValueReplace('left');
  65. singleValueReplace('right');
  66. singleValueReplace('center');
  67. testMerge(
  68. {}, {width: 10, left: 20, right: 30},
  69. {width: 10, left: 20, right: 30},
  70. {width: 10, left: 20}
  71. );
  72. testMerge(
  73. {}, {height: 10, top: 20, bottom: 30},
  74. {height: 10, top: 20, bottom: 30},
  75. {height: 10, top: 20}
  76. );
  77. testMerge(
  78. {}, {width: 10, left: 20, right: 30, height: 10, top: 20, bottom: 30},
  79. {width: 10, left: 20, right: 30, height: 10, top: 20, bottom: 30},
  80. {width: 10, left: 20, height: 10, top: 20}
  81. );
  82. testMerge(
  83. {width: 111, top: 555}, {width: 10, left: 20, right: 30},
  84. {width: 10, left: 20, right: 30, top: 555},
  85. {width: 10, left: 20, top: 555}
  86. );
  87. testMerge(
  88. {width: 111, left: 222, top: 'bottom'}, {right: 30},
  89. {width: 111, right: 30, top: 'bottom'}
  90. );
  91. testMerge(
  92. {width: 111, right: 222, top: 'bottom'}, {left: 30},
  93. {width: 111, left: 30, top: 'bottom'}
  94. );
  95. testMerge(
  96. {height: 111, top: 222, left: 'right'}, {bottom: 30},
  97. {height: 111, bottom: 30, left: 'right'}
  98. );
  99. testMerge(
  100. {height: 111, bottom: 222, left: 'right'}, {top: 30},
  101. {height: 111, top: 30, left: 'right'}
  102. );
  103. testMerge(
  104. {left: 222, top: 'bottom'}, {width: '33%', right: 30},
  105. {width: '33%', right: 30, top: 'bottom'}
  106. );
  107. testMerge(
  108. {right: 222, top: 'bottom'}, {width: '33%', left: 30},
  109. {width: '33%', left: 30, top: 'bottom'}
  110. );
  111. testMerge(
  112. {top: 222, left: 'right'}, {height: '33%', bottom: 30},
  113. {height: '33%', bottom: 30, left: 'right'}
  114. );
  115. testMerge(
  116. {bottom: 222, left: 'right'}, {height: '33%', top: 30},
  117. {height: '33%', top: 30, left: 'right'}
  118. );
  119. testMerge(
  120. {left: 222, top: 'center'}, {width: '33%'},
  121. {width: '33%', left: 222, top: 'center'}
  122. );
  123. testMerge(
  124. {right: 222, top: 'center'}, {width: '33%'},
  125. {width: '33%', right: 222, top: 'center'}
  126. );
  127. testMerge(
  128. {top: 222, left: 'center'}, {height: '33%'},
  129. {height: '33%', top: 222, left: 'center'}
  130. );
  131. testMerge(
  132. {bottom: 222, left: 'center'}, {height: '33%'},
  133. {height: '33%', bottom: 222, left: 'center'}
  134. );
  135. testMerge(
  136. {width: 222, top: 'center'}, {left: '33%', right: 55, bottom: 3},
  137. {left: '33%', right: 55, top: 'center', bottom: 3},
  138. {width: 222, left: '33%', bottom: 3}
  139. );
  140. testMerge(
  141. {height: 222, left: 'center'}, {top: '33%', bottom: 55, right: 3},
  142. {top: '33%', bottom: 55, left: 'center', right: 3},
  143. {height: 222, top: '33%', right: 3}
  144. );
  145. testMerge(
  146. {left: 222, top: 999}, {right: '33%', bottom: 3},
  147. {left: 222, top: 999, right: '33%', bottom: 3},
  148. {right: '33%', bottom: 3}
  149. );
  150. testMerge(
  151. {right: 222, bottom: 999}, {left: '33%', top: 3},
  152. {right: 222, bottom: 999, left: '33%', top: 3},
  153. {left: '33%', top: 3}
  154. );
  155. });
  156. });
  157. });