getMultipleTextDimensions.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 { getMultipleTextDimensions, promiseTimeout } from '@superset-ui/core';
  20. import { addDummyFill, removeDummyFill, SAMPLE_TEXT } from './getBBoxDummyFill';
  21. describe('getMultipleTextDimension(input)', () => {
  22. describe('returns dimension of the given text', () => {
  23. beforeEach(addDummyFill);
  24. afterEach(removeDummyFill);
  25. it('takes an array of text as argument', () => {
  26. expect(
  27. getMultipleTextDimensions({
  28. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[1], ''],
  29. }),
  30. ).toEqual([
  31. {
  32. height: 20,
  33. width: 200,
  34. },
  35. {
  36. height: 20,
  37. width: 300,
  38. },
  39. {
  40. height: 0,
  41. width: 0,
  42. },
  43. ]);
  44. });
  45. it('handles empty text', () => {
  46. expect(
  47. getMultipleTextDimensions({
  48. texts: ['', ''],
  49. }),
  50. ).toEqual([
  51. {
  52. height: 0,
  53. width: 0,
  54. },
  55. {
  56. height: 0,
  57. width: 0,
  58. },
  59. ]);
  60. });
  61. it('handles duplicate text', () => {
  62. expect(
  63. getMultipleTextDimensions({
  64. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[0]],
  65. }),
  66. ).toEqual([
  67. {
  68. height: 20,
  69. width: 200,
  70. },
  71. {
  72. height: 20,
  73. width: 200,
  74. },
  75. ]);
  76. });
  77. it('accepts provided class via className', () => {
  78. expect(
  79. getMultipleTextDimensions({
  80. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[1]],
  81. className: 'test-class',
  82. }),
  83. ).toEqual([
  84. {
  85. height: 20,
  86. width: 100,
  87. },
  88. {
  89. height: 20,
  90. width: 150,
  91. },
  92. ]);
  93. });
  94. it('accepts provided style.font', () => {
  95. expect(
  96. getMultipleTextDimensions({
  97. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[1]],
  98. style: {
  99. font: 'italic 700 30px Lobster',
  100. },
  101. }),
  102. ).toEqual([
  103. {
  104. height: 30, // 20 * (30/20) [fontSize=30]
  105. width: 1125, // 200 * 1.25 [fontFamily=Lobster] * (30/20) [fontSize=30] * 1.5 [fontStyle=italic] * 2 [fontWeight=700]
  106. },
  107. {
  108. height: 30,
  109. width: 1688, // 300 * 1.25 [fontFamily=Lobster] * (30/20) [fontSize=30] * 1.5 [fontStyle=italic] * 2 [fontWeight=700]
  110. },
  111. ]);
  112. });
  113. it('accepts provided style.fontFamily', () => {
  114. expect(
  115. getMultipleTextDimensions({
  116. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[1]],
  117. style: {
  118. fontFamily: 'Lobster',
  119. },
  120. }),
  121. ).toEqual([
  122. {
  123. height: 20,
  124. width: 250, // 200 * 1.25 [fontFamily=Lobster]
  125. },
  126. {
  127. height: 20,
  128. width: 375, // 300 * 1.25 [fontFamily=Lobster]
  129. },
  130. ]);
  131. });
  132. it('accepts provided style.fontSize', () => {
  133. expect(
  134. getMultipleTextDimensions({
  135. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[1]],
  136. style: {
  137. fontSize: '40px',
  138. },
  139. }),
  140. ).toEqual([
  141. {
  142. height: 40, // 20 [baseHeight] * (40/20) [fontSize=40]
  143. width: 400, // 200 [baseWidth] * (40/20) [fontSize=40]
  144. },
  145. {
  146. height: 40,
  147. width: 600, // 300 [baseWidth] * (40/20) [fontSize=40]
  148. },
  149. ]);
  150. });
  151. it('accepts provided style.fontStyle', () => {
  152. expect(
  153. getMultipleTextDimensions({
  154. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[1]],
  155. style: {
  156. fontStyle: 'italic',
  157. },
  158. }),
  159. ).toEqual([
  160. {
  161. height: 20,
  162. width: 300, // 200 [baseWidth] * 1.5 [fontStyle=italic]
  163. },
  164. {
  165. height: 20,
  166. width: 450, // 300 [baseWidth] * 1.5 [fontStyle=italic]
  167. },
  168. ]);
  169. });
  170. it('accepts provided style.fontWeight', () => {
  171. expect(
  172. getMultipleTextDimensions({
  173. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[1]],
  174. style: {
  175. fontWeight: 700,
  176. },
  177. }),
  178. ).toEqual([
  179. {
  180. height: 20,
  181. width: 400, // 200 [baseWidth] * 2 [fontWeight=700]
  182. },
  183. {
  184. height: 20,
  185. width: 600, // 300 [baseWidth] * 2 [fontWeight=700]
  186. },
  187. ]);
  188. });
  189. it('accepts provided style.letterSpacing', () => {
  190. expect(
  191. getMultipleTextDimensions({
  192. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[1]],
  193. style: {
  194. letterSpacing: '1.1',
  195. },
  196. }),
  197. ).toEqual([
  198. {
  199. height: 20,
  200. width: 221, // Ceiling(200 [baseWidth] * 1.1 [letterSpacing=1.1])
  201. },
  202. {
  203. height: 20,
  204. width: 330, // Ceiling(300 [baseWidth] * 1.1 [letterSpacing=1.1])
  205. },
  206. ]);
  207. });
  208. });
  209. it('cleans up DOM', async () => {
  210. getMultipleTextDimensions({
  211. texts: [SAMPLE_TEXT[0], SAMPLE_TEXT[1]],
  212. });
  213. expect(document.querySelectorAll('svg')).toHaveLength(1);
  214. await promiseTimeout(() => {}, 501);
  215. expect(document.querySelector('svg')).toBeNull();
  216. });
  217. });