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.

duplicate.test.ts 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { ShapeType } from 'types'
  2. import TestState from '../test-utils'
  3. describe('duplicate command', () => {
  4. const tt = new TestState()
  5. tt.resetDocumentState()
  6. .createShape(
  7. {
  8. type: ShapeType.Rectangle,
  9. point: [0, 0],
  10. size: [100, 100],
  11. },
  12. 'rectangleShape'
  13. )
  14. .createShape(
  15. {
  16. type: ShapeType.Ellipse,
  17. point: [150, 150],
  18. radiusX: 50,
  19. radiusY: 50,
  20. },
  21. 'ellipseShape'
  22. )
  23. .save()
  24. describe('when one item is selected', () => {
  25. it('does, undoes and redoes command', () => {
  26. tt.restore()
  27. const shapesBeforeDuplication = tt.getSortedPageShapeIds()
  28. tt.clickShape('rectangleShape').send('DUPLICATED')
  29. const shapesAfterDuplication = tt.getSortedPageShapeIds()
  30. const duplicatedShapeId = tt.selectedIds[0]
  31. const duplicatedShape = tt.getShape(duplicatedShapeId)
  32. expect(shapesAfterDuplication.length).toEqual(
  33. shapesBeforeDuplication.length + 1
  34. )
  35. expect(
  36. tt.assertShapeProps(duplicatedShape, {
  37. type: ShapeType.Rectangle,
  38. size: [100, 100],
  39. })
  40. )
  41. tt.undo()
  42. const shapesAfterUndo = tt.getSortedPageShapeIds()
  43. expect(shapesAfterUndo.length).toEqual(shapesBeforeDuplication.length)
  44. expect(tt.getShape(duplicatedShapeId)).toBe(undefined)
  45. expect(tt.idsAreSelected(['rectangleShape'])).toBe(true)
  46. tt.redo()
  47. expect(tt.getShape(duplicatedShapeId)).toBeTruthy()
  48. expect(tt.idsAreSelected([duplicatedShapeId])).toBe(true)
  49. })
  50. })
  51. describe('when multiple items are selected', () => {
  52. it('does, undoes and redoes command', () => {
  53. tt.restore()
  54. const shapesBeforeDuplication = tt.getSortedPageShapeIds()
  55. tt.clickShape('rectangleShape')
  56. .clickShape('ellipseShape', { shiftKey: true })
  57. .send('DUPLICATED')
  58. const shapesAfterDuplication = tt.getSortedPageShapeIds()
  59. const duplicatedShapesIds = tt.selectedIds
  60. const [duplicatedRectangle, duplicatedEllipse] = duplicatedShapesIds.map(
  61. (shapeId) => tt.getShape(shapeId)
  62. )
  63. expect(shapesAfterDuplication.length).toEqual(
  64. shapesBeforeDuplication.length * 2
  65. )
  66. expect(
  67. tt.assertShapeProps(duplicatedRectangle, {
  68. type: ShapeType.Rectangle,
  69. size: [100, 100],
  70. })
  71. )
  72. expect(
  73. tt.assertShapeProps(duplicatedEllipse, {
  74. type: ShapeType.Ellipse,
  75. radiusX: 50,
  76. radiusY: 50,
  77. })
  78. )
  79. tt.undo()
  80. const shapesAfterUndo = tt.getSortedPageShapeIds()
  81. expect(shapesAfterUndo.length).toEqual(shapesBeforeDuplication.length)
  82. duplicatedShapesIds.forEach((shapeId) => {
  83. expect(tt.getShape(shapeId)).toBe(undefined)
  84. })
  85. expect(tt.idsAreSelected(['rectangleShape', 'ellipseShape'])).toBe(true)
  86. tt.redo()
  87. duplicatedShapesIds.forEach((shapeId) => {
  88. expect(tt.getShape(shapeId)).toBeTruthy()
  89. })
  90. expect(tt.idsAreSelected(duplicatedShapesIds)).toBe(true)
  91. })
  92. })
  93. })