textElement.test.ts 4.6 KB

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