您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

duplicate-page.test.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { ShapeType } from 'types'
  2. import TestState from '../test-utils'
  3. describe('duplicate page 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. },
  13. 'rect1'
  14. )
  15. .save()
  16. describe('duplicates a page', () => {
  17. it('does, undoes, and redoes command', () => {
  18. tt.reset().restore()
  19. expect(Object.keys(tt.data.document.pages).length).toBe(1)
  20. const pageId = Object.keys(tt.data.document.pages)[0]
  21. expect(tt.getShape('rect1').parentId).toBe(pageId)
  22. tt.send('DUPLICATED_PAGE', { id: pageId })
  23. expect(Object.keys(tt.data.document.pages).length).toBe(2)
  24. const newPageId = Object.keys(tt.data.document.pages)[1]
  25. expect(tt.data.currentPageId).toBe(newPageId)
  26. expect(tt.getShape('rect1').parentId).toBe(newPageId)
  27. tt.undo()
  28. expect(Object.keys(tt.data.document.pages).length).toBe(1)
  29. expect(tt.data.currentPageId).toBe(Object.keys(tt.data.document.pages)[0])
  30. expect(tt.getShape('rect1').parentId).toBe(pageId)
  31. tt.redo()
  32. expect(Object.keys(tt.data.document.pages).length).toBe(2)
  33. expect(tt.data.currentPageId).toBe(Object.keys(tt.data.document.pages)[1])
  34. expect(tt.getShape('rect1').parentId).toBe(newPageId)
  35. })
  36. })
  37. describe('duplicates a page other than the current page', () => {
  38. tt.restore()
  39. .reset()
  40. .send('CREATED_PAGE')
  41. .createShape(
  42. {
  43. type: ShapeType.Rectangle,
  44. point: [0, 0],
  45. size: [100, 100],
  46. childIndex: 1,
  47. },
  48. 'rect2'
  49. )
  50. .send('CHANGED_PAGE', { id: 'page1' })
  51. const firstPageId = Object.keys(tt.data.document.pages)[0]
  52. // We should be back on the first page
  53. expect(tt.data.currentPageId).toBe(firstPageId)
  54. // But we should have two pages
  55. expect(Object.keys(tt.data.document.pages).length).toBe(2)
  56. const secondPageId = Object.keys(tt.data.document.pages)[1]
  57. // Now we duplicate the second page
  58. tt.send('DUPLICATED_PAGE', { id: secondPageId })
  59. // We should now have three pages
  60. expect(Object.keys(tt.data.document.pages).length).toBe(3)
  61. // The third page should also have a shape named rect2
  62. const thirdPageId = Object.keys(tt.data.document.pages)[2]
  63. // We should have changed pages to the third page
  64. expect(tt.data.currentPageId).toBe(thirdPageId)
  65. // And it should be the parent of the third page
  66. expect(tt.getShape('rect2').parentId).toBe(thirdPageId)
  67. tt.undo()
  68. // We should still be on the first page, but we should
  69. // have only two pages; the third page should be deleted
  70. expect(Object.keys(tt.data.document.pages).length).toBe(2)
  71. expect(tt.data.document.pages[thirdPageId]).toBe(undefined)
  72. expect(tt.data.currentPageId).toBe(firstPageId)
  73. tt.redo()
  74. // We should be back on the third page
  75. expect(Object.keys(tt.data.document.pages).length).toBe(3)
  76. expect(tt.data.document.pages[thirdPageId]).toBeTruthy()
  77. expect(tt.data.currentPageId).toBe(Object.keys(tt.data.document.pages)[2])
  78. expect(tt.getShape('rect2').parentId).toBe(thirdPageId)
  79. })
  80. })