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.

toggle.test.ts 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import { ShapeType } from 'types'
  2. import TestState from '../test-utils'
  3. describe('toggle 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. childIndex: 1,
  12. isLocked: false,
  13. isHidden: false,
  14. isAspectRatioLocked: false,
  15. },
  16. 'rect1'
  17. )
  18. .createShape(
  19. {
  20. type: ShapeType.Rectangle,
  21. point: [400, 0],
  22. size: [100, 100],
  23. childIndex: 2,
  24. isHidden: false,
  25. isLocked: false,
  26. isAspectRatioLocked: false,
  27. },
  28. 'rect2'
  29. )
  30. .createShape(
  31. {
  32. type: ShapeType.Rectangle,
  33. point: [800, 0],
  34. size: [100, 100],
  35. childIndex: 3,
  36. isHidden: true,
  37. isLocked: true,
  38. isAspectRatioLocked: true,
  39. },
  40. 'rect3'
  41. )
  42. .createShape(
  43. {
  44. type: ShapeType.Rectangle,
  45. point: [1000, 0],
  46. size: [100, 100],
  47. childIndex: 4,
  48. isHidden: true,
  49. isLocked: true,
  50. isAspectRatioLocked: true,
  51. },
  52. 'rect4'
  53. )
  54. .save()
  55. describe('toggles properties on single shape', () => {
  56. it('does command', () => {
  57. tt.restore().clickShape('rect1')
  58. tt.send('TOGGLED_SHAPE_LOCK')
  59. expect(tt.getShape('rect1').isLocked).toBe(true)
  60. tt.send('TOGGLED_SHAPE_LOCK')
  61. expect(tt.getShape('rect1').isLocked).toBe(false)
  62. })
  63. it('undoes and redoes command', () => {
  64. // Restore the saved data state, with the initial selection
  65. tt.restore().clickShape('rect1')
  66. tt.send('TOGGLED_SHAPE_LOCK')
  67. tt.send('UNDO')
  68. expect(tt.getShape('rect1').isLocked).toBe(false)
  69. tt.send('REDO')
  70. expect(tt.getShape('rect1').isLocked).toBe(true)
  71. })
  72. })
  73. describe('toggles properties on shapes with matching properties, starting from false', () => {
  74. it('does command', () => {
  75. // Restore the saved data state, with the initial selection
  76. tt.restore().clickShape('rect1').clickShape('rect2', { shiftKey: true })
  77. tt.send('TOGGLED_SHAPE_LOCK')
  78. expect(tt.getShape('rect1').isLocked).toBe(true)
  79. expect(tt.getShape('rect2').isLocked).toBe(true)
  80. tt.send('TOGGLED_SHAPE_LOCK')
  81. expect(tt.getShape('rect1').isLocked).toBe(false)
  82. expect(tt.getShape('rect2').isLocked).toBe(false)
  83. tt.send('TOGGLED_SHAPE_LOCK')
  84. expect(tt.getShape('rect1').isLocked).toBe(true)
  85. expect(tt.getShape('rect2').isLocked).toBe(true)
  86. })
  87. it('undoes and redoes command', () => {
  88. // Restore the saved data state, with the initial selection
  89. tt.restore().clickShape('rect1').clickShape('rect2', { shiftKey: true })
  90. tt.send('TOGGLED_SHAPE_LOCK').undo()
  91. expect(tt.getShape('rect1').isLocked).toBe(false)
  92. expect(tt.getShape('rect2').isLocked).toBe(false)
  93. tt.redo()
  94. expect(tt.getShape('rect1').isLocked).toBe(true)
  95. expect(tt.getShape('rect2').isLocked).toBe(true)
  96. })
  97. })
  98. describe('toggles properties on shapes with matching properties, starting from true', () => {
  99. it('does command', () => {
  100. // Restore the saved data state, with the initial selection
  101. tt.restore().clickShape('rect3').clickShape('rect4', { shiftKey: true })
  102. tt.send('TOGGLED_SHAPE_LOCK')
  103. expect(tt.getShape('rect3').isLocked).toBe(false)
  104. expect(tt.getShape('rect4').isLocked).toBe(false)
  105. tt.send('TOGGLED_SHAPE_LOCK')
  106. expect(tt.getShape('rect3').isLocked).toBe(true)
  107. expect(tt.getShape('rect4').isLocked).toBe(true)
  108. })
  109. it('undoes and redoes command', () => {
  110. // Restore the saved data state, with the initial selection
  111. tt.restore().clickShape('rect3').clickShape('rect4', { shiftKey: true })
  112. tt.send('TOGGLED_SHAPE_LOCK').undo()
  113. expect(tt.getShape('rect3').isLocked).toBe(true)
  114. expect(tt.getShape('rect4').isLocked).toBe(true)
  115. tt.redo()
  116. expect(tt.getShape('rect3').isLocked).toBe(false)
  117. expect(tt.getShape('rect4').isLocked).toBe(false)
  118. })
  119. })
  120. describe('toggles properties on shapes with different properties', () => {
  121. it('does command', () => {
  122. // Restore the saved data state, with the initial selection
  123. tt.restore()
  124. .clickShape('rect1')
  125. .clickShape('rect2', { shiftKey: true })
  126. .clickShape('rect3', { shiftKey: true })
  127. tt.send('TOGGLED_SHAPE_LOCK')
  128. expect(tt.getShape('rect1').isLocked).toBe(true)
  129. expect(tt.getShape('rect2').isLocked).toBe(true)
  130. expect(tt.getShape('rect3').isLocked).toBe(true)
  131. tt.send('TOGGLED_SHAPE_LOCK')
  132. expect(tt.getShape('rect1').isLocked).toBe(false)
  133. expect(tt.getShape('rect2').isLocked).toBe(false)
  134. expect(tt.getShape('rect3').isLocked).toBe(false)
  135. })
  136. it('undoes and redoes command', () => {
  137. tt.restore()
  138. .clickShape('rect1')
  139. .clickShape('rect2', { shiftKey: true })
  140. .clickShape('rect3', { shiftKey: true })
  141. tt.send('TOGGLED_SHAPE_LOCK').undo()
  142. expect(tt.getShape('rect1').isLocked).toBe(false)
  143. expect(tt.getShape('rect2').isLocked).toBe(false)
  144. expect(tt.getShape('rect3').isLocked).toBe(true)
  145. tt.redo()
  146. expect(tt.getShape('rect1').isLocked).toBe(true)
  147. expect(tt.getShape('rect2').isLocked).toBe(true)
  148. expect(tt.getShape('rect3').isLocked).toBe(true)
  149. })
  150. })
  151. describe('catch all for other toggle props', () => {
  152. const eventPropertyPairs = {
  153. TOGGLED_SHAPE_LOCK: 'isLocked',
  154. TOGGLED_SHAPE_HIDE: 'isHidden',
  155. TOGGLED_SHAPE_ASPECT_LOCK: 'isAspectRatioLocked',
  156. }
  157. it('toggles all event property pairs', () => {
  158. Object.entries(eventPropertyPairs).forEach(([eventName, propName]) => {
  159. // Restore the saved data state, with the initial selection
  160. tt.restore()
  161. .clickShape('rect1')
  162. .clickShape('rect2', { shiftKey: true })
  163. .clickShape('rect3', { shiftKey: true })
  164. tt.send(eventName)
  165. expect(tt.getShape('rect1')[propName]).toBe(true)
  166. expect(tt.getShape('rect2')[propName]).toBe(true)
  167. expect(tt.getShape('rect3')[propName]).toBe(true)
  168. tt.undo()
  169. expect(tt.getShape('rect1')[propName]).toBe(false)
  170. expect(tt.getShape('rect2')[propName]).toBe(false)
  171. expect(tt.getShape('rect3')[propName]).toBe(true)
  172. tt.redo()
  173. expect(tt.getShape('rect1')[propName]).toBe(true)
  174. expect(tt.getShape('rect2')[propName]).toBe(true)
  175. expect(tt.getShape('rect3')[propName]).toBe(true)
  176. tt.send(eventName)
  177. expect(tt.getShape('rect1')[propName]).toBe(false)
  178. expect(tt.getShape('rect2')[propName]).toBe(false)
  179. expect(tt.getShape('rect3')[propName]).toBe(false)
  180. })
  181. })
  182. })
  183. })