123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- import { ShapeType } from 'types'
- import TestState from '../test-utils'
-
- describe('toggle command', () => {
- const tt = new TestState()
- tt.resetDocumentState()
- .createShape(
- {
- type: ShapeType.Rectangle,
- point: [0, 0],
- size: [100, 100],
- childIndex: 1,
- isLocked: false,
- isHidden: false,
- isAspectRatioLocked: false,
- },
- 'rect1'
- )
- .createShape(
- {
- type: ShapeType.Rectangle,
- point: [400, 0],
- size: [100, 100],
- childIndex: 2,
- isHidden: false,
- isLocked: false,
- isAspectRatioLocked: false,
- },
- 'rect2'
- )
- .createShape(
- {
- type: ShapeType.Rectangle,
- point: [800, 0],
- size: [100, 100],
- childIndex: 3,
- isHidden: true,
- isLocked: true,
- isAspectRatioLocked: true,
- },
- 'rect3'
- )
- .createShape(
- {
- type: ShapeType.Rectangle,
- point: [1000, 0],
- size: [100, 100],
- childIndex: 4,
- isHidden: true,
- isLocked: true,
- isAspectRatioLocked: true,
- },
- 'rect4'
- )
- .save()
-
- describe('toggles properties on single shape', () => {
- it('does command', () => {
- tt.restore().clickShape('rect1')
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- expect(tt.getShape('rect1').isLocked).toBe(true)
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- expect(tt.getShape('rect1').isLocked).toBe(false)
- })
-
- it('undoes and redoes command', () => {
- // Restore the saved data state, with the initial selection
- tt.restore().clickShape('rect1')
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- tt.send('UNDO')
-
- expect(tt.getShape('rect1').isLocked).toBe(false)
-
- tt.send('REDO')
-
- expect(tt.getShape('rect1').isLocked).toBe(true)
- })
- })
-
- describe('toggles properties on shapes with matching properties, starting from false', () => {
- it('does command', () => {
- // Restore the saved data state, with the initial selection
- tt.restore().clickShape('rect1').clickShape('rect2', { shiftKey: true })
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- expect(tt.getShape('rect1').isLocked).toBe(true)
- expect(tt.getShape('rect2').isLocked).toBe(true)
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- expect(tt.getShape('rect1').isLocked).toBe(false)
- expect(tt.getShape('rect2').isLocked).toBe(false)
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- expect(tt.getShape('rect1').isLocked).toBe(true)
- expect(tt.getShape('rect2').isLocked).toBe(true)
- })
-
- it('undoes and redoes command', () => {
- // Restore the saved data state, with the initial selection
- tt.restore().clickShape('rect1').clickShape('rect2', { shiftKey: true })
-
- tt.send('TOGGLED_SHAPE_LOCK').undo()
-
- expect(tt.getShape('rect1').isLocked).toBe(false)
- expect(tt.getShape('rect2').isLocked).toBe(false)
-
- tt.redo()
-
- expect(tt.getShape('rect1').isLocked).toBe(true)
- expect(tt.getShape('rect2').isLocked).toBe(true)
- })
- })
-
- describe('toggles properties on shapes with matching properties, starting from true', () => {
- it('does command', () => {
- // Restore the saved data state, with the initial selection
- tt.restore().clickShape('rect3').clickShape('rect4', { shiftKey: true })
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- expect(tt.getShape('rect3').isLocked).toBe(false)
- expect(tt.getShape('rect4').isLocked).toBe(false)
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- expect(tt.getShape('rect3').isLocked).toBe(true)
- expect(tt.getShape('rect4').isLocked).toBe(true)
- })
-
- it('undoes and redoes command', () => {
- // Restore the saved data state, with the initial selection
- tt.restore().clickShape('rect3').clickShape('rect4', { shiftKey: true })
-
- tt.send('TOGGLED_SHAPE_LOCK').undo()
-
- expect(tt.getShape('rect3').isLocked).toBe(true)
- expect(tt.getShape('rect4').isLocked).toBe(true)
-
- tt.redo()
-
- expect(tt.getShape('rect3').isLocked).toBe(false)
- expect(tt.getShape('rect4').isLocked).toBe(false)
- })
- })
-
- describe('toggles properties on shapes with different properties', () => {
- it('does command', () => {
- // Restore the saved data state, with the initial selection
- tt.restore()
- .clickShape('rect1')
- .clickShape('rect2', { shiftKey: true })
- .clickShape('rect3', { shiftKey: true })
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- expect(tt.getShape('rect1').isLocked).toBe(true)
- expect(tt.getShape('rect2').isLocked).toBe(true)
- expect(tt.getShape('rect3').isLocked).toBe(true)
-
- tt.send('TOGGLED_SHAPE_LOCK')
-
- expect(tt.getShape('rect1').isLocked).toBe(false)
- expect(tt.getShape('rect2').isLocked).toBe(false)
- expect(tt.getShape('rect3').isLocked).toBe(false)
- })
-
- it('undoes and redoes command', () => {
- tt.restore()
- .clickShape('rect1')
- .clickShape('rect2', { shiftKey: true })
- .clickShape('rect3', { shiftKey: true })
-
- tt.send('TOGGLED_SHAPE_LOCK').undo()
-
- expect(tt.getShape('rect1').isLocked).toBe(false)
- expect(tt.getShape('rect2').isLocked).toBe(false)
- expect(tt.getShape('rect3').isLocked).toBe(true)
-
- tt.redo()
-
- expect(tt.getShape('rect1').isLocked).toBe(true)
- expect(tt.getShape('rect2').isLocked).toBe(true)
- expect(tt.getShape('rect3').isLocked).toBe(true)
- })
- })
-
- describe('catch all for other toggle props', () => {
- const eventPropertyPairs = {
- TOGGLED_SHAPE_LOCK: 'isLocked',
- TOGGLED_SHAPE_HIDE: 'isHidden',
- TOGGLED_SHAPE_ASPECT_LOCK: 'isAspectRatioLocked',
- }
-
- it('toggles all event property pairs', () => {
- Object.entries(eventPropertyPairs).forEach(([eventName, propName]) => {
- // Restore the saved data state, with the initial selection
- tt.restore()
- .clickShape('rect1')
- .clickShape('rect2', { shiftKey: true })
- .clickShape('rect3', { shiftKey: true })
-
- tt.send(eventName)
-
- expect(tt.getShape('rect1')[propName]).toBe(true)
- expect(tt.getShape('rect2')[propName]).toBe(true)
- expect(tt.getShape('rect3')[propName]).toBe(true)
-
- tt.undo()
-
- expect(tt.getShape('rect1')[propName]).toBe(false)
- expect(tt.getShape('rect2')[propName]).toBe(false)
- expect(tt.getShape('rect3')[propName]).toBe(true)
-
- tt.redo()
-
- expect(tt.getShape('rect1')[propName]).toBe(true)
- expect(tt.getShape('rect2')[propName]).toBe(true)
- expect(tt.getShape('rect3')[propName]).toBe(true)
-
- tt.send(eventName)
-
- expect(tt.getShape('rect1')[propName]).toBe(false)
- expect(tt.getShape('rect2')[propName]).toBe(false)
- expect(tt.getShape('rect3')[propName]).toBe(false)
- })
- })
- })
- })
|