You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

children.test.ts 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { MoveType, ShapeType } from 'types'
  2. import TestState from './test-utils'
  3. describe('shapes with children', () => {
  4. const tt = new TestState()
  5. tt.resetDocumentState()
  6. .createShape(
  7. {
  8. type: ShapeType.Rectangle,
  9. point: [0, 0],
  10. size: [100, 100],
  11. childIndex: 1,
  12. },
  13. 'delete-me-bottom'
  14. )
  15. .createShape(
  16. {
  17. type: ShapeType.Rectangle,
  18. point: [0, 0],
  19. size: [100, 100],
  20. childIndex: 2,
  21. },
  22. '1'
  23. )
  24. .createShape(
  25. {
  26. type: ShapeType.Rectangle,
  27. point: [300, 0],
  28. size: [100, 100],
  29. childIndex: 3,
  30. },
  31. '2'
  32. )
  33. .createShape(
  34. {
  35. type: ShapeType.Rectangle,
  36. point: [0, 300],
  37. size: [100, 100],
  38. childIndex: 4,
  39. },
  40. 'delete-me-middle'
  41. )
  42. .createShape(
  43. {
  44. type: ShapeType.Rectangle,
  45. point: [0, 300],
  46. size: [100, 100],
  47. childIndex: 5,
  48. },
  49. '3'
  50. )
  51. .createShape(
  52. {
  53. type: ShapeType.Rectangle,
  54. point: [300, 300],
  55. size: [100, 100],
  56. childIndex: 6,
  57. },
  58. '4'
  59. )
  60. // Delete shapes at the start and in the middle of the list
  61. tt.clickShape('delete-me-bottom')
  62. .send('DELETED')
  63. .clickShape('delete-me-middle')
  64. .send('DELETED')
  65. it('has shapes in order', () => {
  66. expect(
  67. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  68. .sort((a, b) => a.childIndex - b.childIndex)
  69. .map((shape) => shape.childIndex)
  70. ).toStrictEqual([2, 3, 5, 6])
  71. })
  72. it('moves a shape to back', () => {
  73. tt.clickShape('3').send('MOVED', {
  74. type: MoveType.ToBack,
  75. })
  76. expect(
  77. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  78. .sort((a, b) => a.childIndex - b.childIndex)
  79. .map((shape) => shape.id)
  80. ).toStrictEqual(['3', '1', '2', '4'])
  81. })
  82. it('moves two adjacent siblings to back', () => {
  83. tt.clickShape('4').clickShape('2', { shiftKey: true }).send('MOVED', {
  84. type: MoveType.ToBack,
  85. })
  86. expect(
  87. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  88. .sort((a, b) => a.childIndex - b.childIndex)
  89. .map((shape) => shape.id)
  90. ).toStrictEqual(['2', '4', '3', '1'])
  91. })
  92. it('moves two non-adjacent siblings to back', () => {
  93. tt.clickShape('4').clickShape('1', { shiftKey: true }).send('MOVED', {
  94. type: MoveType.ToBack,
  95. })
  96. expect(
  97. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  98. .sort((a, b) => a.childIndex - b.childIndex)
  99. .map((shape) => shape.id)
  100. ).toStrictEqual(['4', '1', '2', '3'])
  101. })
  102. it('moves a shape backward', () => {
  103. tt.clickShape('3').send('MOVED', {
  104. type: MoveType.Backward,
  105. })
  106. expect(
  107. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  108. .sort((a, b) => a.childIndex - b.childIndex)
  109. .map((shape) => shape.id)
  110. ).toStrictEqual(['4', '1', '3', '2'])
  111. })
  112. it('moves a shape at first index backward', () => {
  113. tt.clickShape('4').send('MOVED', {
  114. type: MoveType.Backward,
  115. })
  116. expect(
  117. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  118. .sort((a, b) => a.childIndex - b.childIndex)
  119. .map((shape) => shape.id)
  120. ).toStrictEqual(['4', '1', '3', '2'])
  121. })
  122. it('moves two adjacent siblings backward', () => {
  123. tt.clickShape('3').clickShape('2', { shiftKey: true }).send('MOVED', {
  124. type: MoveType.Backward,
  125. })
  126. expect(
  127. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  128. .sort((a, b) => a.childIndex - b.childIndex)
  129. .map((shape) => shape.id)
  130. ).toStrictEqual(['4', '3', '2', '1'])
  131. })
  132. it('moves two non-adjacent siblings backward', () => {
  133. tt.clickShape('3').clickShape('1', { shiftKey: true }).send('MOVED', {
  134. type: MoveType.Backward,
  135. })
  136. expect(
  137. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  138. .sort((a, b) => a.childIndex - b.childIndex)
  139. .map((shape) => shape.id)
  140. ).toStrictEqual(['3', '4', '1', '2'])
  141. })
  142. it('moves two adjacent siblings backward at zero index', () => {
  143. tt.clickShape('3').clickShape('4', { shiftKey: true }).send('MOVED', {
  144. type: MoveType.Backward,
  145. })
  146. expect(
  147. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  148. .sort((a, b) => a.childIndex - b.childIndex)
  149. .map((shape) => shape.id)
  150. ).toStrictEqual(['3', '4', '1', '2'])
  151. })
  152. it('moves a shape forward', () => {
  153. tt.clickShape('4').send('MOVED', {
  154. type: MoveType.Forward,
  155. })
  156. expect(
  157. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  158. .sort((a, b) => a.childIndex - b.childIndex)
  159. .map((shape) => shape.id)
  160. ).toStrictEqual(['3', '1', '4', '2'])
  161. })
  162. it('moves a shape forward at the top index', () => {
  163. tt.clickShape('2').send('MOVED', {
  164. type: MoveType.Forward,
  165. })
  166. expect(
  167. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  168. .sort((a, b) => a.childIndex - b.childIndex)
  169. .map((shape) => shape.id)
  170. ).toStrictEqual(['3', '1', '4', '2'])
  171. })
  172. it('moves two adjacent siblings forward', () => {
  173. tt.deselectAll()
  174. .clickShape('4')
  175. .clickShape('1', { shiftKey: true })
  176. .send('MOVED', {
  177. type: MoveType.Forward,
  178. })
  179. expect(tt.idsAreSelected(['1', '4'])).toBe(true)
  180. expect(
  181. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  182. .sort((a, b) => a.childIndex - b.childIndex)
  183. .map((shape) => shape.id)
  184. ).toStrictEqual(['3', '2', '1', '4'])
  185. })
  186. it('moves two non-adjacent siblings forward', () => {
  187. tt.deselectAll()
  188. .clickShape('3')
  189. .clickShape('1', { shiftKey: true })
  190. .send('MOVED', {
  191. type: MoveType.Forward,
  192. })
  193. expect(
  194. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  195. .sort((a, b) => a.childIndex - b.childIndex)
  196. .map((shape) => shape.id)
  197. ).toStrictEqual(['2', '3', '4', '1'])
  198. })
  199. it('moves two adjacent siblings forward at top index', () => {
  200. tt.deselectAll()
  201. .clickShape('3')
  202. .clickShape('1', { shiftKey: true })
  203. .send('MOVED', {
  204. type: MoveType.Forward,
  205. })
  206. expect(
  207. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  208. .sort((a, b) => a.childIndex - b.childIndex)
  209. .map((shape) => shape.id)
  210. ).toStrictEqual(['2', '4', '3', '1'])
  211. })
  212. it('moves a shape to front', () => {
  213. tt.deselectAll().clickShape('2').send('MOVED', {
  214. type: MoveType.ToFront,
  215. })
  216. expect(
  217. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  218. .sort((a, b) => a.childIndex - b.childIndex)
  219. .map((shape) => shape.id)
  220. ).toStrictEqual(['4', '3', '1', '2'])
  221. })
  222. it('moves two adjacent siblings to front', () => {
  223. tt.deselectAll()
  224. .clickShape('3')
  225. .clickShape('1', { shiftKey: true })
  226. .send('MOVED', {
  227. type: MoveType.ToFront,
  228. })
  229. expect(
  230. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  231. .sort((a, b) => a.childIndex - b.childIndex)
  232. .map((shape) => shape.id)
  233. ).toStrictEqual(['4', '2', '3', '1'])
  234. })
  235. it('moves two non-adjacent siblings to front', () => {
  236. tt.deselectAll()
  237. .clickShape('4')
  238. .clickShape('3', { shiftKey: true })
  239. .send('MOVED', {
  240. type: MoveType.ToFront,
  241. })
  242. expect(
  243. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  244. .sort((a, b) => a.childIndex - b.childIndex)
  245. .map((shape) => shape.id)
  246. ).toStrictEqual(['2', '1', '4', '3'])
  247. })
  248. it('moves siblings already at front to front', () => {
  249. tt.deselectAll()
  250. .clickShape('4')
  251. .clickShape('3', { shiftKey: true })
  252. .send('MOVED', {
  253. type: MoveType.ToFront,
  254. })
  255. expect(
  256. Object.values(tt.data.document.pages[tt.data.currentParentId].shapes)
  257. .sort((a, b) => a.childIndex - b.childIndex)
  258. .map((shape) => shape.id)
  259. ).toStrictEqual(['2', '1', '4', '3'])
  260. })
  261. })