textElement.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { wrapText } from "./textElement";
  2. import { FontString } from "./types";
  3. describe("Test wrapText", () => {
  4. const font = "20px Cascadia, width: Segoe UI Emoji" as FontString;
  5. describe("When text doesn't contain new lines", () => {
  6. const text = "Hello whats up";
  7. [
  8. {
  9. desc: "break all words when width of each word is less than container width",
  10. width: 140,
  11. res: `Hello
  12. whats
  13. up`,
  14. },
  15. {
  16. desc: "break all characters when width of each character is less than container width",
  17. width: 75,
  18. res: `H
  19. e
  20. l
  21. l
  22. o
  23. w
  24. h
  25. a
  26. t
  27. s
  28. u
  29. p`,
  30. },
  31. {
  32. desc: "break words as per the width",
  33. width: 200,
  34. res: `Hello whats
  35. up`,
  36. },
  37. {
  38. desc: "fit the container",
  39. width: 250,
  40. res: "Hello whats up",
  41. },
  42. ].forEach((data) => {
  43. it(`should ${data.desc}`, () => {
  44. const res = wrapText(text, font, data.width);
  45. expect(res).toEqual(data.res);
  46. });
  47. });
  48. });
  49. describe("When text contain new lines", () => {
  50. const text = `Hello
  51. whats up`;
  52. [
  53. {
  54. desc: "break all words when width of each word is less than container width",
  55. width: 140,
  56. res: `Hello
  57. whats
  58. up`,
  59. },
  60. {
  61. desc: "break all characters when width of each character is less than container width",
  62. width: 75,
  63. res: `H
  64. e
  65. l
  66. l
  67. o
  68. w
  69. h
  70. a
  71. t
  72. s
  73. u
  74. p`,
  75. },
  76. {
  77. desc: "break words as per the width",
  78. width: 200,
  79. res: `Hello
  80. whats up`,
  81. },
  82. {
  83. desc: "fit the container",
  84. width: 250,
  85. res: `Hello
  86. whats up`,
  87. },
  88. ].forEach((data) => {
  89. it(`should respect new lines and ${data.desc}`, () => {
  90. const res = wrapText(text, font, data.width);
  91. expect(res).toEqual(data.res);
  92. });
  93. });
  94. });
  95. describe("When text is long", () => {
  96. const text = `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg break it now`;
  97. [
  98. {
  99. desc: "fit characters of long string as per container width",
  100. width: 220,
  101. res: `hellolongtextth
  102. isiswhatsupwith
  103. youIamtypingggg
  104. gandtypinggg
  105. break it now`,
  106. },
  107. {
  108. desc: "fit characters of long string as per container width and break words as per the width",
  109. width: 180,
  110. res: `hellolongte
  111. xtthisiswha
  112. tsupwithyou
  113. Iamtypinggg
  114. ggandtyping
  115. gg break it
  116. now`,
  117. },
  118. {
  119. desc: "fit the long text when container width is greater than text length and move the rest to next line",
  120. width: 650,
  121. res: `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg
  122. break it now`,
  123. },
  124. ].forEach((data) => {
  125. it(`should ${data.desc}`, () => {
  126. const res = wrapText(text, font, data.width);
  127. expect(res).toEqual(data.res);
  128. });
  129. });
  130. });
  131. });