textElement.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { BOUND_TEXT_PADDING } from "../constants";
  2. import { API } from "../tests/helpers/api";
  3. import {
  4. computeContainerHeightForBoundText,
  5. getContainerCoords,
  6. getMaxContainerWidth,
  7. getMaxContainerHeight,
  8. wrapText,
  9. } from "./textElement";
  10. import { FontString } from "./types";
  11. describe("Test wrapText", () => {
  12. const font = "20px Cascadia, width: Segoe UI Emoji" as FontString;
  13. it("shouldn't add new lines for trailing spaces", () => {
  14. const text = "Hello whats up ";
  15. const maxWidth = 200 - BOUND_TEXT_PADDING * 2;
  16. const res = wrapText(text, font, maxWidth);
  17. expect(res).toBe("Hello whats up ");
  18. });
  19. it("should work with emojis", () => {
  20. const text = "😀";
  21. const maxWidth = 1;
  22. const res = wrapText(text, font, maxWidth);
  23. expect(res).toBe("😀");
  24. });
  25. it("should show the text correctly when min width reached", () => {
  26. const text = "Hello😀";
  27. const maxWidth = 10;
  28. const res = wrapText(text, font, maxWidth);
  29. expect(res).toBe("H\ne\nl\nl\no\n😀");
  30. });
  31. describe("When text doesn't contain new lines", () => {
  32. const text = "Hello whats up";
  33. [
  34. {
  35. desc: "break all words when width of each word is less than container width",
  36. width: 90,
  37. res: `Hello
  38. whats
  39. up`,
  40. },
  41. {
  42. desc: "break all characters when width of each character is less than container width",
  43. width: 25,
  44. res: `H
  45. e
  46. l
  47. l
  48. o
  49. w
  50. h
  51. a
  52. t
  53. s
  54. u
  55. p`,
  56. },
  57. {
  58. desc: "break words as per the width",
  59. width: 150,
  60. res: `Hello whats
  61. up`,
  62. },
  63. {
  64. desc: "fit the container",
  65. width: 250,
  66. res: "Hello whats up",
  67. },
  68. {
  69. desc: "should push the word if its equal to max width",
  70. width: 60,
  71. res: `Hello
  72. whats
  73. up`,
  74. },
  75. ].forEach((data) => {
  76. it(`should ${data.desc}`, () => {
  77. const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
  78. expect(res).toEqual(data.res);
  79. });
  80. });
  81. });
  82. describe("When text contain new lines", () => {
  83. const text = `Hello
  84. whats up`;
  85. [
  86. {
  87. desc: "break all words when width of each word is less than container width",
  88. width: 90,
  89. res: `Hello
  90. whats
  91. up`,
  92. },
  93. {
  94. desc: "break all characters when width of each character is less than container width",
  95. width: 25,
  96. res: `H
  97. e
  98. l
  99. l
  100. o
  101. w
  102. h
  103. a
  104. t
  105. s
  106. u
  107. p`,
  108. },
  109. {
  110. desc: "break words as per the width",
  111. width: 150,
  112. res: `Hello
  113. whats up`,
  114. },
  115. {
  116. desc: "fit the container",
  117. width: 250,
  118. res: `Hello
  119. whats up`,
  120. },
  121. ].forEach((data) => {
  122. it(`should respect new lines and ${data.desc}`, () => {
  123. const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
  124. expect(res).toEqual(data.res);
  125. });
  126. });
  127. });
  128. describe("When text is long", () => {
  129. const text = `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg break it now`;
  130. [
  131. {
  132. desc: "fit characters of long string as per container width",
  133. width: 170,
  134. res: `hellolongtextth
  135. isiswhatsupwith
  136. youIamtypingggg
  137. gandtypinggg
  138. break it now`,
  139. },
  140. {
  141. desc: "fit characters of long string as per container width and break words as per the width",
  142. width: 130,
  143. res: `hellolongte
  144. xtthisiswha
  145. tsupwithyou
  146. Iamtypinggg
  147. ggandtyping
  148. gg break it
  149. now`,
  150. },
  151. {
  152. desc: "fit the long text when container width is greater than text length and move the rest to next line",
  153. width: 600,
  154. res: `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg
  155. break it now`,
  156. },
  157. ].forEach((data) => {
  158. it(`should ${data.desc}`, () => {
  159. const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
  160. expect(res).toEqual(data.res);
  161. });
  162. });
  163. });
  164. });
  165. describe("Test measureText", () => {
  166. describe("Test getContainerCoords", () => {
  167. const params = { width: 200, height: 100, x: 10, y: 20 };
  168. it("should compute coords correctly when ellipse", () => {
  169. const element = API.createElement({
  170. type: "ellipse",
  171. ...params,
  172. });
  173. expect(getContainerCoords(element)).toEqual({
  174. x: 44.2893218813452455,
  175. y: 39.64466094067262,
  176. });
  177. });
  178. it("should compute coords correctly when rectangle", () => {
  179. const element = API.createElement({
  180. type: "rectangle",
  181. ...params,
  182. });
  183. expect(getContainerCoords(element)).toEqual({
  184. x: 15,
  185. y: 25,
  186. });
  187. });
  188. it("should compute coords correctly when diamond", () => {
  189. const element = API.createElement({
  190. type: "diamond",
  191. ...params,
  192. });
  193. expect(getContainerCoords(element)).toEqual({
  194. x: 65,
  195. y: 50,
  196. });
  197. });
  198. });
  199. describe("Test computeContainerHeightForBoundText", () => {
  200. const params = {
  201. width: 178,
  202. height: 194,
  203. };
  204. it("should compute container height correctly for rectangle", () => {
  205. const element = API.createElement({
  206. type: "rectangle",
  207. ...params,
  208. });
  209. expect(computeContainerHeightForBoundText(element, 150)).toEqual(160);
  210. });
  211. it("should compute container height correctly for ellipse", () => {
  212. const element = API.createElement({
  213. type: "ellipse",
  214. ...params,
  215. });
  216. expect(computeContainerHeightForBoundText(element, 150)).toEqual(226);
  217. });
  218. it("should compute container height correctly for diamond", () => {
  219. const element = API.createElement({
  220. type: "diamond",
  221. ...params,
  222. });
  223. expect(computeContainerHeightForBoundText(element, 150)).toEqual(320);
  224. });
  225. });
  226. describe("Test getMaxContainerWidth", () => {
  227. const params = {
  228. width: 178,
  229. height: 194,
  230. };
  231. it("should return max width when container is rectangle", () => {
  232. const container = API.createElement({ type: "rectangle", ...params });
  233. expect(getMaxContainerWidth(container)).toBe(168);
  234. });
  235. it("should return max width when container is ellipse", () => {
  236. const container = API.createElement({ type: "ellipse", ...params });
  237. expect(getMaxContainerWidth(container)).toBe(116);
  238. });
  239. it("should return max width when container is diamond", () => {
  240. const container = API.createElement({ type: "diamond", ...params });
  241. expect(getMaxContainerWidth(container)).toBe(79);
  242. });
  243. });
  244. describe("Test getMaxContainerHeight", () => {
  245. const params = {
  246. width: 178,
  247. height: 194,
  248. };
  249. it("should return max height when container is rectangle", () => {
  250. const container = API.createElement({ type: "rectangle", ...params });
  251. expect(getMaxContainerHeight(container)).toBe(184);
  252. });
  253. it("should return max height when container is ellipse", () => {
  254. const container = API.createElement({ type: "ellipse", ...params });
  255. expect(getMaxContainerHeight(container)).toBe(127);
  256. });
  257. it("should return max height when container is diamond", () => {
  258. const container = API.createElement({ type: "diamond", ...params });
  259. expect(getMaxContainerHeight(container)).toBe(87);
  260. });
  261. });
  262. });