css-layout.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import Element from "../src/element";
  2. test("layout", () => {
  3. const container = new Element({
  4. width: 100,
  5. height: 100,
  6. padding: 10,
  7. borderWidth: 2
  8. })
  9. const div1 = new Element({
  10. left: 5,
  11. top: 5,
  12. width: 14,
  13. height: 14
  14. })
  15. container.add(div1);
  16. container.layout();
  17. // css-layout 是 border-box
  18. expect(container.layoutBox.left).toBe(0);
  19. expect(container.layoutBox.top).toBe(0);
  20. expect(container.layoutBox.width).toBe(100);
  21. expect(container.layoutBox.height).toBe(100);
  22. expect(div1.layoutBox.left).toBe(10 + 2 + 5);
  23. expect(div1.layoutBox.top).toBe(10 + 2 + 5);
  24. expect(div1.layoutBox.width).toBe(14);
  25. expect(div1.layoutBox.height).toBe(14);
  26. });
  27. test("overflow", () => {
  28. const container = new Element({
  29. width: 100,
  30. height: 100,
  31. padding: 10,
  32. borderWidth: 2
  33. })
  34. const div1 = new Element({
  35. width: 114,
  36. height: 114,
  37. })
  38. container.add(div1);
  39. container.layout();
  40. // 写死尺寸的情况下子元素不收缩父元素不撑开
  41. expect(container.layoutBox.width).toBe(100);
  42. expect(container.layoutBox.height).toBe(100);
  43. expect(div1.layoutBox.left).toBe(10 + 2);
  44. expect(div1.layoutBox.top).toBe(10 + 2);
  45. expect(div1.layoutBox.width).toBe(114);
  46. expect(div1.layoutBox.height).toBe(114);
  47. });
  48. test("right bottom", () => {
  49. const container = new Element({
  50. width: 100,
  51. height: 100,
  52. padding: 10,
  53. borderWidth: 2
  54. })
  55. const div1 = new Element({
  56. width: 14,
  57. height: 14,
  58. right: 13,
  59. bottom: 9,
  60. position: "absolute"
  61. })
  62. container.add(div1);
  63. container.layout();
  64. // right bottom 只有在 position 为 absolute 的情况下才有用
  65. expect(container.layoutBox.width).toBe(100);
  66. expect(container.layoutBox.height).toBe(100);
  67. // 但这时就是以整个父元素为边界,而不是 border + padding 后的边界
  68. expect(div1.layoutBox.left).toBe(100 - 13 - 14);
  69. expect(div1.layoutBox.top).toBe(100 - 9 - 14);
  70. });
  71. test("flex center", () => {
  72. const container = new Element({
  73. width: 100,
  74. height: 100,
  75. padding: 10,
  76. borderWidth: 2,
  77. flexDirection: "row",
  78. justifyContent: "center",
  79. alignItems: "center"
  80. })
  81. const div1 = new Element({
  82. width: 14,
  83. height: 14
  84. })
  85. container.add(div1);
  86. container.layout();
  87. // 使用 flex 水平垂直居中
  88. expect(div1.layoutBox.left).toBe((100 - 14)/2);
  89. expect(div1.layoutBox.top).toBe((100 - 14)/2);
  90. })
  91. test("flex top bottom", () => {
  92. const container = new Element({
  93. width: 100,
  94. height: 100,
  95. padding: 10,
  96. borderWidth: 2,
  97. flexDirection: "column",
  98. justifyContent: "space-between",
  99. alignItems: "stretch"
  100. })
  101. // flex 实现一上一下两行水平填满
  102. const div1 = new Element({
  103. height: 10
  104. })
  105. const div2 = new Element({
  106. height: 20
  107. })
  108. container.add(div1);
  109. container.add(div2);
  110. container.layout();
  111. expect(div1.layoutBox.left).toBe(10 + 2);
  112. expect(div1.layoutBox.top).toBe(10 + 2);
  113. expect(div1.layoutBox.width).toBe(100 - 10*2 - 2*2);
  114. expect(div2.layoutBox.left).toBe(10 + 2);
  115. expect(div2.layoutBox.top).toBe(100 - 10 - 2 - 20);
  116. expect(div2.layoutBox.width).toBe(100 - 10*2 - 2*2);
  117. })
  118. test("rewrite uuid", () => {
  119. // 小程序为了保证 webview 和 service 侧的 coverview 不冲突,所以设置了不同的自增起点
  120. // uuid 静态方法就是为了根据不同的需求去覆写
  121. let uuid = 79648527;
  122. Element.uuid = () => uuid++;
  123. const container = new Element();
  124. expect(container.id).toEqual(79648527);
  125. const div = new Element();
  126. expect(div.id).toEqual(79648528);
  127. });
  128. test("absolute left top", () => {
  129. const container = new Element({
  130. width: 300,
  131. height: 200,
  132. flexDirection: 'row',
  133. justifyContent: 'center',
  134. alignItems: 'center'
  135. })
  136. const div1 = new Element({
  137. width: 80,
  138. height: 60
  139. })
  140. const div2 = new Element({
  141. width: 40,
  142. height: 30
  143. })
  144. div1.add(div2)
  145. container.add(div1)
  146. container.layout()
  147. expect(div1.layoutBox.left).toBe(110)
  148. expect(div1.layoutBox.top).toBe(70)
  149. expect(div2.layoutBox.left).toBe(110)
  150. expect(div2.layoutBox.top).toBe(70)
  151. })