mergeMargin.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { mergeMargin } from '@superset-ui/core';
  20. describe('mergeMargin(margin1, margin2, mode?)', () => {
  21. it('combines two given margin', () => {
  22. expect(
  23. mergeMargin(
  24. {
  25. top: 1,
  26. left: 1,
  27. bottom: 2,
  28. right: 2,
  29. },
  30. {
  31. top: 2,
  32. left: 2,
  33. bottom: 1,
  34. right: 1,
  35. },
  36. ),
  37. ).toEqual({
  38. top: 2,
  39. left: 2,
  40. bottom: 2,
  41. right: 2,
  42. });
  43. });
  44. describe('default values', () => {
  45. it('works if margin1 is not defined', () => {
  46. expect(
  47. mergeMargin(undefined, {
  48. top: 2,
  49. left: 2,
  50. bottom: 1,
  51. right: 1,
  52. }),
  53. ).toEqual({
  54. top: 2,
  55. left: 2,
  56. bottom: 1,
  57. right: 1,
  58. });
  59. });
  60. it('works if margin2 is not defined', () => {
  61. expect(
  62. mergeMargin(
  63. {
  64. top: 1,
  65. left: 1,
  66. bottom: 2,
  67. right: 2,
  68. },
  69. undefined,
  70. ),
  71. ).toEqual({
  72. top: 1,
  73. left: 1,
  74. bottom: 2,
  75. right: 2,
  76. });
  77. });
  78. it('use 0 for the side that is not specified', () => {
  79. expect(mergeMargin({}, {})).toEqual({
  80. top: 0,
  81. left: 0,
  82. bottom: 0,
  83. right: 0,
  84. });
  85. });
  86. });
  87. describe('mode', () => {
  88. it('if mode=expand, returns the larger margin for each side', () => {
  89. expect(
  90. mergeMargin(
  91. {
  92. top: 1,
  93. left: 1,
  94. bottom: 2,
  95. right: 2,
  96. },
  97. {
  98. top: 2,
  99. left: 2,
  100. bottom: 1,
  101. right: 1,
  102. },
  103. 'expand',
  104. ),
  105. ).toEqual({
  106. top: 2,
  107. left: 2,
  108. bottom: 2,
  109. right: 2,
  110. });
  111. });
  112. it('if mode=shrink, returns the smaller margin for each side', () => {
  113. expect(
  114. mergeMargin(
  115. {
  116. top: 1,
  117. left: 1,
  118. bottom: 2,
  119. right: 2,
  120. },
  121. {
  122. top: 2,
  123. left: 2,
  124. bottom: 1,
  125. right: 1,
  126. },
  127. 'shrink',
  128. ),
  129. ).toEqual({
  130. top: 1,
  131. left: 1,
  132. bottom: 1,
  133. right: 1,
  134. });
  135. });
  136. it('expand by default', () => {
  137. expect(
  138. mergeMargin(
  139. {
  140. top: 1,
  141. left: 1,
  142. bottom: 2,
  143. right: 2,
  144. },
  145. {
  146. top: 2,
  147. left: 2,
  148. bottom: 1,
  149. right: 1,
  150. },
  151. ),
  152. ).toEqual({
  153. top: 2,
  154. left: 2,
  155. bottom: 2,
  156. right: 2,
  157. });
  158. });
  159. });
  160. it('works correctly for negative margins', () => {
  161. expect(
  162. mergeMargin(
  163. {
  164. top: -3,
  165. left: -3,
  166. bottom: -2,
  167. right: -2,
  168. },
  169. {
  170. top: -2,
  171. left: -2,
  172. bottom: 0,
  173. right: -1,
  174. },
  175. ),
  176. ).toEqual({
  177. top: -2,
  178. left: -2,
  179. bottom: 0,
  180. right: -1,
  181. });
  182. });
  183. it('if there are NaN or null, use another value', () => {
  184. expect(
  185. mergeMargin(
  186. {
  187. top: 10,
  188. // @ts-ignore to let us pass `null` for testing
  189. left: null,
  190. bottom: 20,
  191. right: NaN,
  192. },
  193. {
  194. top: NaN,
  195. left: 30,
  196. bottom: null,
  197. right: 40,
  198. },
  199. ),
  200. ).toEqual({
  201. top: 10,
  202. left: 30,
  203. bottom: 20,
  204. right: 40,
  205. });
  206. });
  207. });