flip.test.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import { render } from "./test-utils";
  4. import App from "../components/App";
  5. import { defaultLang, setLanguage } from "../i18n";
  6. import { UI, Pointer } from "./helpers/ui";
  7. import { API } from "./helpers/api";
  8. import { actionFlipHorizontal, actionFlipVertical } from "../actions";
  9. const { h } = window;
  10. const mouse = new Pointer("mouse");
  11. beforeEach(async () => {
  12. // Unmount ReactDOM from root
  13. ReactDOM.unmountComponentAtNode(document.getElementById("root")!);
  14. mouse.reset();
  15. await setLanguage(defaultLang);
  16. await render(<App />);
  17. });
  18. const createAndSelectOneRectangle = (angle: number = 0) => {
  19. UI.createElement("rectangle", {
  20. x: 0,
  21. y: 0,
  22. width: 100,
  23. height: 50,
  24. angle,
  25. });
  26. };
  27. const createAndSelectOneDiamond = (angle: number = 0) => {
  28. UI.createElement("diamond", {
  29. x: 0,
  30. y: 0,
  31. width: 100,
  32. height: 50,
  33. angle,
  34. });
  35. };
  36. const createAndSelectOneEllipse = (angle: number = 0) => {
  37. UI.createElement("ellipse", {
  38. x: 0,
  39. y: 0,
  40. width: 100,
  41. height: 50,
  42. angle,
  43. });
  44. };
  45. const createAndSelectOneArrow = (angle: number = 0) => {
  46. UI.createElement("arrow", {
  47. x: 0,
  48. y: 0,
  49. width: 100,
  50. height: 50,
  51. angle,
  52. });
  53. };
  54. const createAndSelectOneLine = (angle: number = 0) => {
  55. UI.createElement("line", {
  56. x: 0,
  57. y: 0,
  58. width: 100,
  59. height: 50,
  60. angle,
  61. });
  62. };
  63. const createAndReturnOneDraw = (angle: number = 0) => {
  64. return UI.createElement("freedraw", {
  65. x: 0,
  66. y: 0,
  67. width: 50,
  68. height: 100,
  69. angle,
  70. });
  71. };
  72. // Rectangle element
  73. it("flips an unrotated rectangle horizontally correctly", () => {
  74. createAndSelectOneRectangle();
  75. expect(API.getSelectedElements()[0].x).toEqual(0);
  76. expect(API.getSelectedElements()[0].y).toEqual(0);
  77. const originalWidth = API.getSelectedElements()[0].width;
  78. const originalHeight = API.getSelectedElements()[0].height;
  79. h.app.actionManager.executeAction(actionFlipHorizontal);
  80. // Check if x position did not change
  81. expect(API.getSelectedElements()[0].x).toEqual(0);
  82. expect(API.getSelectedElements()[0].y).toEqual(0);
  83. // Check if width and height did not change
  84. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  85. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  86. });
  87. it("flips an unrotated rectangle vertically correctly", () => {
  88. createAndSelectOneRectangle();
  89. expect(API.getSelectedElements()[0].x).toEqual(0);
  90. expect(API.getSelectedElements()[0].y).toEqual(0);
  91. const originalWidth = API.getSelectedElements()[0].width;
  92. const originalHeight = API.getSelectedElements()[0].height;
  93. h.app.actionManager.executeAction(actionFlipVertical);
  94. // Check if x position did not change
  95. expect(API.getSelectedElements()[0].x).toEqual(0);
  96. expect(API.getSelectedElements()[0].y).toEqual(0);
  97. // Check if width and height did not change
  98. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  99. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  100. });
  101. it("flips a rotated rectangle horizontally correctly", () => {
  102. const originalAngle = (3 * Math.PI) / 4;
  103. const expectedAngle = (5 * Math.PI) / 4;
  104. createAndSelectOneRectangle(originalAngle);
  105. expect(API.getSelectedElements()[0].x).toEqual(0);
  106. expect(API.getSelectedElements()[0].y).toEqual(0);
  107. const originalWidth = API.getSelectedElements()[0].width;
  108. const originalHeight = API.getSelectedElements()[0].height;
  109. h.app.actionManager.executeAction(actionFlipHorizontal);
  110. // Check if x position did not change
  111. expect(API.getSelectedElements()[0].x).toEqual(0);
  112. expect(API.getSelectedElements()[0].y).toEqual(0);
  113. // Check if width and height did not change
  114. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  115. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  116. // Check angle
  117. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAngle);
  118. });
  119. it("flips a rotated rectangle vertically correctly", () => {
  120. const originalAngle = (3 * Math.PI) / 4;
  121. const expectedAgnle = Math.PI / 4;
  122. createAndSelectOneRectangle(originalAngle);
  123. expect(API.getSelectedElements()[0].x).toEqual(0);
  124. expect(API.getSelectedElements()[0].y).toEqual(0);
  125. const originalWidth = API.getSelectedElements()[0].width;
  126. const originalHeight = API.getSelectedElements()[0].height;
  127. h.app.actionManager.executeAction(actionFlipVertical);
  128. // Check if x position did not change
  129. expect(API.getSelectedElements()[0].x).toEqual(0);
  130. expect(API.getSelectedElements()[0].y).toEqual(0);
  131. // Check if width and height did not change
  132. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  133. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  134. // Check angle
  135. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAgnle);
  136. });
  137. // Diamond element
  138. it("flips an unrotated diamond horizontally correctly", () => {
  139. createAndSelectOneDiamond();
  140. expect(API.getSelectedElements()[0].x).toEqual(0);
  141. expect(API.getSelectedElements()[0].y).toEqual(0);
  142. const originalWidth = API.getSelectedElements()[0].width;
  143. const originalHeight = API.getSelectedElements()[0].height;
  144. h.app.actionManager.executeAction(actionFlipHorizontal);
  145. // Check if x position did not change
  146. expect(API.getSelectedElements()[0].x).toEqual(0);
  147. expect(API.getSelectedElements()[0].y).toEqual(0);
  148. // Check if width and height did not change
  149. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  150. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  151. });
  152. it("flips an unrotated diamond vertically correctly", () => {
  153. createAndSelectOneDiamond();
  154. expect(API.getSelectedElements()[0].x).toEqual(0);
  155. expect(API.getSelectedElements()[0].y).toEqual(0);
  156. const originalWidth = API.getSelectedElements()[0].width;
  157. const originalHeight = API.getSelectedElements()[0].height;
  158. h.app.actionManager.executeAction(actionFlipVertical);
  159. // Check if x position did not change
  160. expect(API.getSelectedElements()[0].x).toEqual(0);
  161. expect(API.getSelectedElements()[0].y).toEqual(0);
  162. // Check if width and height did not change
  163. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  164. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  165. });
  166. it("flips a rotated diamond horizontally correctly", () => {
  167. const originalAngle = (5 * Math.PI) / 4;
  168. const expectedAngle = (3 * Math.PI) / 4;
  169. createAndSelectOneDiamond(originalAngle);
  170. expect(API.getSelectedElements()[0].x).toEqual(0);
  171. expect(API.getSelectedElements()[0].y).toEqual(0);
  172. const originalWidth = API.getSelectedElements()[0].width;
  173. const originalHeight = API.getSelectedElements()[0].height;
  174. h.app.actionManager.executeAction(actionFlipHorizontal);
  175. // Check if x position did not change
  176. expect(API.getSelectedElements()[0].x).toEqual(0);
  177. expect(API.getSelectedElements()[0].y).toEqual(0);
  178. // Check if width and height did not change
  179. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  180. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  181. // Check angle
  182. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAngle);
  183. });
  184. it("flips a rotated diamond vertically correctly", () => {
  185. const originalAngle = (5 * Math.PI) / 4;
  186. const expectedAngle = (7 * Math.PI) / 4;
  187. createAndSelectOneDiamond(originalAngle);
  188. expect(API.getSelectedElements()[0].x).toEqual(0);
  189. expect(API.getSelectedElements()[0].y).toEqual(0);
  190. const originalWidth = API.getSelectedElements()[0].width;
  191. const originalHeight = API.getSelectedElements()[0].height;
  192. h.app.actionManager.executeAction(actionFlipVertical);
  193. // Check if x position did not change
  194. expect(API.getSelectedElements()[0].x).toEqual(0);
  195. expect(API.getSelectedElements()[0].y).toEqual(0);
  196. // Check if width and height did not change
  197. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  198. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  199. // Check angle
  200. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAngle);
  201. });
  202. // Ellipse element
  203. it("flips an unrotated ellipse horizontally correctly", () => {
  204. createAndSelectOneEllipse();
  205. expect(API.getSelectedElements()[0].x).toEqual(0);
  206. expect(API.getSelectedElements()[0].y).toEqual(0);
  207. const originalWidth = API.getSelectedElements()[0].width;
  208. const originalHeight = API.getSelectedElements()[0].height;
  209. h.app.actionManager.executeAction(actionFlipHorizontal);
  210. // Check if x position did not change
  211. expect(API.getSelectedElements()[0].x).toEqual(0);
  212. expect(API.getSelectedElements()[0].y).toEqual(0);
  213. // Check if width and height did not change
  214. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  215. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  216. });
  217. it("flips an unrotated ellipse vertically correctly", () => {
  218. createAndSelectOneEllipse();
  219. expect(API.getSelectedElements()[0].x).toEqual(0);
  220. expect(API.getSelectedElements()[0].y).toEqual(0);
  221. const originalWidth = API.getSelectedElements()[0].width;
  222. const originalHeight = API.getSelectedElements()[0].height;
  223. h.app.actionManager.executeAction(actionFlipVertical);
  224. // Check if x position did not change
  225. expect(API.getSelectedElements()[0].x).toEqual(0);
  226. expect(API.getSelectedElements()[0].y).toEqual(0);
  227. // Check if width and height did not change
  228. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  229. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  230. });
  231. it("flips a rotated ellipse horizontally correctly", () => {
  232. const originalAngle = (7 * Math.PI) / 4;
  233. const expectedAngle = Math.PI / 4;
  234. createAndSelectOneEllipse(originalAngle);
  235. expect(API.getSelectedElements()[0].x).toEqual(0);
  236. expect(API.getSelectedElements()[0].y).toEqual(0);
  237. const originalWidth = API.getSelectedElements()[0].width;
  238. const originalHeight = API.getSelectedElements()[0].height;
  239. h.app.actionManager.executeAction(actionFlipHorizontal);
  240. // Check if x position did not change
  241. expect(API.getSelectedElements()[0].x).toEqual(0);
  242. expect(API.getSelectedElements()[0].y).toEqual(0);
  243. // Check if width and height did not change
  244. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  245. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  246. // Check angle
  247. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAngle);
  248. });
  249. it("flips a rotated ellipse vertically correctly", () => {
  250. const originalAngle = (7 * Math.PI) / 4;
  251. const expectedAngle = (5 * Math.PI) / 4;
  252. createAndSelectOneEllipse(originalAngle);
  253. expect(API.getSelectedElements()[0].x).toEqual(0);
  254. expect(API.getSelectedElements()[0].y).toEqual(0);
  255. const originalWidth = API.getSelectedElements()[0].width;
  256. const originalHeight = API.getSelectedElements()[0].height;
  257. h.app.actionManager.executeAction(actionFlipVertical);
  258. // Check if x position did not change
  259. expect(API.getSelectedElements()[0].x).toEqual(0);
  260. expect(API.getSelectedElements()[0].y).toEqual(0);
  261. // Check if width and height did not change
  262. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  263. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  264. // Check angle
  265. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAngle);
  266. });
  267. // Arrow element
  268. it("flips an unrotated arrow horizontally correctly", () => {
  269. createAndSelectOneArrow();
  270. const originalWidth = API.getSelectedElements()[0].width;
  271. const originalHeight = API.getSelectedElements()[0].height;
  272. h.app.actionManager.executeAction(actionFlipHorizontal);
  273. // Check if width and height did not change
  274. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  275. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  276. });
  277. it("flips an unrotated arrow vertically correctly", () => {
  278. createAndSelectOneArrow();
  279. const originalWidth = API.getSelectedElements()[0].width;
  280. const originalHeight = API.getSelectedElements()[0].height;
  281. h.app.actionManager.executeAction(actionFlipVertical);
  282. // Check if width and height did not change
  283. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  284. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  285. });
  286. it("flips a rotated arrow horizontally correctly", () => {
  287. const originalAngle = Math.PI / 4;
  288. const expectedAngle = (7 * Math.PI) / 4;
  289. createAndSelectOneArrow(originalAngle);
  290. const originalWidth = API.getSelectedElements()[0].width;
  291. const originalHeight = API.getSelectedElements()[0].height;
  292. h.app.actionManager.executeAction(actionFlipHorizontal);
  293. // Check if width and height did not change
  294. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  295. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  296. // Check angle
  297. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAngle);
  298. });
  299. it("flips a rotated arrow vertically correctly", () => {
  300. const originalAngle = Math.PI / 4;
  301. const expectedAngle = (3 * Math.PI) / 4;
  302. createAndSelectOneArrow(originalAngle);
  303. const originalWidth = API.getSelectedElements()[0].width;
  304. const originalHeight = API.getSelectedElements()[0].height;
  305. h.app.actionManager.executeAction(actionFlipVertical);
  306. // Check if width and height did not change
  307. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  308. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  309. // Check angle
  310. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAngle);
  311. });
  312. // Line element
  313. it("flips an unrotated line horizontally correctly", () => {
  314. createAndSelectOneLine();
  315. const originalWidth = API.getSelectedElements()[0].width;
  316. const originalHeight = API.getSelectedElements()[0].height;
  317. h.app.actionManager.executeAction(actionFlipHorizontal);
  318. // Check if width and height did not change
  319. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  320. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  321. });
  322. it("flips an unrotated line vertically correctly", () => {
  323. createAndSelectOneLine();
  324. const originalWidth = API.getSelectedElements()[0].width;
  325. const originalHeight = API.getSelectedElements()[0].height;
  326. h.app.actionManager.executeAction(actionFlipVertical);
  327. // Check if width and height did not change
  328. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  329. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  330. });
  331. it("flips a rotated line horizontally correctly", () => {
  332. const originalAngle = Math.PI / 4;
  333. const expectedAngle = (7 * Math.PI) / 4;
  334. createAndSelectOneLine(originalAngle);
  335. const originalWidth = API.getSelectedElements()[0].width;
  336. const originalHeight = API.getSelectedElements()[0].height;
  337. h.app.actionManager.executeAction(actionFlipHorizontal);
  338. // Check if width and height did not change
  339. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  340. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  341. // Check angle
  342. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAngle);
  343. });
  344. it("flips a rotated line vertically correctly", () => {
  345. const originalAngle = Math.PI / 4;
  346. const expectedAngle = (3 * Math.PI) / 4;
  347. createAndSelectOneLine(originalAngle);
  348. const originalWidth = API.getSelectedElements()[0].width;
  349. const originalHeight = API.getSelectedElements()[0].height;
  350. h.app.actionManager.executeAction(actionFlipVertical);
  351. // Check if width and height did not change
  352. expect(API.getSelectedElements()[0].width).toEqual(originalWidth);
  353. expect(API.getSelectedElements()[0].height).toEqual(originalHeight);
  354. // Check angle
  355. expect(API.getSelectedElements()[0].angle).toBeCloseTo(expectedAngle);
  356. });
  357. // Draw element
  358. it("flips an unrotated drawing horizontally correctly", () => {
  359. const draw = createAndReturnOneDraw();
  360. // select draw, since not done automatically
  361. h.state.selectedElementIds[draw.id] = true;
  362. const originalWidth = draw.width;
  363. const originalHeight = draw.height;
  364. h.app.actionManager.executeAction(actionFlipHorizontal);
  365. // Check if width and height did not change
  366. expect(draw.width).toEqual(originalWidth);
  367. expect(draw.height).toEqual(originalHeight);
  368. });
  369. it("flips an unrotated drawing vertically correctly", () => {
  370. const draw = createAndReturnOneDraw();
  371. // select draw, since not done automatically
  372. h.state.selectedElementIds[draw.id] = true;
  373. const originalWidth = draw.width;
  374. const originalHeight = draw.height;
  375. h.app.actionManager.executeAction(actionFlipVertical);
  376. // Check if width and height did not change
  377. expect(draw.width).toEqual(originalWidth);
  378. expect(draw.height).toEqual(originalHeight);
  379. });
  380. it("flips a rotated drawing horizontally correctly", () => {
  381. const originalAngle = Math.PI / 4;
  382. const expectedAngle = (7 * Math.PI) / 4;
  383. const draw = createAndReturnOneDraw(originalAngle);
  384. // select draw, since not done automatically
  385. h.state.selectedElementIds[draw.id] = true;
  386. const originalWidth = draw.width;
  387. const originalHeight = draw.height;
  388. h.app.actionManager.executeAction(actionFlipHorizontal);
  389. // Check if width and height did not change
  390. expect(draw.width).toEqual(originalWidth);
  391. expect(draw.height).toEqual(originalHeight);
  392. // Check angle
  393. expect(draw.angle).toBeCloseTo(expectedAngle);
  394. });
  395. it("flips a rotated drawing vertically correctly", () => {
  396. const originalAngle = Math.PI / 4;
  397. const expectedAngle = (3 * Math.PI) / 4;
  398. const draw = createAndReturnOneDraw(originalAngle);
  399. // select draw, since not done automatically
  400. h.state.selectedElementIds[draw.id] = true;
  401. const originalWidth = draw.width;
  402. const originalHeight = draw.height;
  403. h.app.actionManager.executeAction(actionFlipVertical);
  404. // Check if width and height did not change
  405. expect(API.getSelectedElement().width).toEqual(originalWidth);
  406. expect(API.getSelectedElement().height).toEqual(originalHeight);
  407. // Check angle
  408. expect(API.getSelectedElement().angle).toBeCloseTo(expectedAngle);
  409. });