textElement.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { BOUND_TEXT_PADDING } from "../constants";
  2. import { measureText, wrapText } from "./textElement";
  3. import { FontString } from "./types";
  4. describe("Test wrapText", () => {
  5. const font = "20px Cascadia, width: Segoe UI Emoji" as FontString;
  6. it("shouldn't add new lines for trailing spaces", () => {
  7. const text = "Hello whats up ";
  8. const maxWidth = 200 - BOUND_TEXT_PADDING * 2;
  9. const res = wrapText(text, font, maxWidth);
  10. expect(res).toBe("Hello whats up ");
  11. });
  12. describe("When text doesn't contain new lines", () => {
  13. const text = "Hello whats up";
  14. [
  15. {
  16. desc: "break all words when width of each word is less than container width",
  17. width: 90,
  18. res: `Hello
  19. whats
  20. up`,
  21. },
  22. {
  23. desc: "break all characters when width of each character is less than container width",
  24. width: 25,
  25. res: `H
  26. e
  27. l
  28. l
  29. o
  30. w
  31. h
  32. a
  33. t
  34. s
  35. u
  36. p`,
  37. },
  38. {
  39. desc: "break words as per the width",
  40. width: 150,
  41. res: `Hello whats
  42. up`,
  43. },
  44. {
  45. desc: "fit the container",
  46. width: 250,
  47. res: "Hello whats up",
  48. },
  49. ].forEach((data) => {
  50. it(`should ${data.desc}`, () => {
  51. const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
  52. expect(res).toEqual(data.res);
  53. });
  54. });
  55. });
  56. describe("When text contain new lines", () => {
  57. const text = `Hello
  58. whats up`;
  59. [
  60. {
  61. desc: "break all words when width of each word is less than container width",
  62. width: 90,
  63. res: `Hello
  64. whats
  65. up`,
  66. },
  67. {
  68. desc: "break all characters when width of each character is less than container width",
  69. width: 25,
  70. res: `H
  71. e
  72. l
  73. l
  74. o
  75. w
  76. h
  77. a
  78. t
  79. s
  80. u
  81. p`,
  82. },
  83. {
  84. desc: "break words as per the width",
  85. width: 150,
  86. res: `Hello
  87. whats up`,
  88. },
  89. {
  90. desc: "fit the container",
  91. width: 250,
  92. res: `Hello
  93. whats up`,
  94. },
  95. ].forEach((data) => {
  96. it(`should respect new lines and ${data.desc}`, () => {
  97. const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
  98. expect(res).toEqual(data.res);
  99. });
  100. });
  101. });
  102. describe("When text is long", () => {
  103. const text = `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg break it now`;
  104. [
  105. {
  106. desc: "fit characters of long string as per container width",
  107. width: 170,
  108. res: `hellolongtextth
  109. isiswhatsupwith
  110. youIamtypingggg
  111. gandtypinggg
  112. break it now`,
  113. },
  114. {
  115. desc: "fit characters of long string as per container width and break words as per the width",
  116. width: 130,
  117. res: `hellolongte
  118. xtthisiswha
  119. tsupwithyou
  120. Iamtypinggg
  121. ggandtyping
  122. gg break it
  123. now`,
  124. },
  125. {
  126. desc: "fit the long text when container width is greater than text length and move the rest to next line",
  127. width: 600,
  128. res: `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg
  129. break it now`,
  130. },
  131. ].forEach((data) => {
  132. it(`should ${data.desc}`, () => {
  133. const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
  134. expect(res).toEqual(data.res);
  135. });
  136. });
  137. });
  138. });
  139. describe("Test measureText", () => {
  140. const font = "20px Cascadia, width: Segoe UI Emoji" as FontString;
  141. const text = "Hello World";
  142. it("should add correct attributes when maxWidth is passed", () => {
  143. const maxWidth = 200 - BOUND_TEXT_PADDING * 2;
  144. const res = measureText(text, font, maxWidth);
  145. expect(res.container).toMatchInlineSnapshot(`
  146. <div
  147. style="position: absolute; white-space: pre-wrap; font: Emoji 20px 20px; min-height: 1em; width: 111px; overflow: hidden; word-break: break-word; line-height: 0px;"
  148. >
  149. <span
  150. style="display: inline-block; overflow: hidden; width: 1px; height: 1px;"
  151. />
  152. </div>
  153. `);
  154. });
  155. it("should add correct attributes when maxWidth is not passed", () => {
  156. const res = measureText(text, font);
  157. expect(res.container).toMatchInlineSnapshot(`
  158. <div
  159. style="position: absolute; white-space: pre; font: Emoji 20px 20px; min-height: 1em;"
  160. >
  161. <span
  162. style="display: inline-block; overflow: hidden; width: 1px; height: 1px;"
  163. />
  164. </div>
  165. `);
  166. });
  167. });