1
0

FilesUtils.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
  3. *
  4. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. export const getRowForFileId = (fileid: number) => cy.get(`[data-cy-files-list-row-fileid="${fileid}"]`)
  23. export const getRowForFile = (filename: string) => cy.get(`[data-cy-files-list-row-name="${CSS.escape(filename)}"]`)
  24. export const getActionsForFileId = (fileid: number) => getRowForFileId(fileid).find('[data-cy-files-list-row-actions]')
  25. export const getActionsForFile = (filename: string) => getRowForFile(filename).find('[data-cy-files-list-row-actions]')
  26. export const getActionButtonForFileId = (fileid: number) => getActionsForFileId(fileid).find('button[aria-label="Actions"]')
  27. export const getActionButtonForFile = (filename: string) => getActionsForFile(filename).find('button[aria-label="Actions"]')
  28. export const triggerActionForFileId = (fileid: number, actionId: string) => {
  29. getActionButtonForFileId(fileid).click()
  30. cy.get(`[data-cy-files-list-row-action="${CSS.escape(actionId)}"] > button`).should('exist').click()
  31. }
  32. export const triggerActionForFile = (filename: string, actionId: string) => {
  33. getActionButtonForFile(filename).click()
  34. cy.get(`[data-cy-files-list-row-action="${CSS.escape(actionId)}"] > button`).should('exist').click()
  35. }
  36. export const triggerInlineActionForFileId = (fileid: number, actionId: string) => {
  37. getActionsForFileId(fileid).find(`button[data-cy-files-list-row-action="${CSS.escape(actionId)}"]`).should('exist').click()
  38. }
  39. export const triggerInlineActionForFile = (filename: string, actionId: string) => {
  40. getActionsForFile(filename).get(`button[data-cy-files-list-row-action="${CSS.escape(actionId)}"]`).should('exist').click()
  41. }
  42. export const createFolder = (folderName: string) => {
  43. cy.intercept('MKCOL', /\/remote.php\/dav\/files\//).as('createFolder')
  44. // TODO: replace by proper data-cy selectors
  45. cy.get('[data-cy-upload-picker] .action-item__menutoggle').first().click()
  46. cy.contains('.upload-picker__menu-entry button', 'New folder').click()
  47. cy.get('[data-cy-files-new-node-dialog]').should('be.visible')
  48. cy.get('[data-cy-files-new-node-dialog-input]').type(`{selectall}${folderName}`)
  49. cy.get('[data-cy-files-new-node-dialog-submit]').click()
  50. cy.wait('@createFolder')
  51. getRowForFile(folderName).should('be.visible')
  52. }
  53. export const moveFile = (fileName: string, dirPath: string) => {
  54. getRowForFile(fileName).should('be.visible')
  55. triggerActionForFile(fileName, 'move-copy')
  56. cy.get('.file-picker').within(() => {
  57. // intercept the copy so we can wait for it
  58. cy.intercept('MOVE', /\/remote.php\/dav\/files\//).as('moveFile')
  59. if (dirPath === '/') {
  60. // select home folder
  61. cy.get('button[title="Home"]').should('be.visible').click()
  62. // click move
  63. cy.contains('button', 'Move').should('be.visible').click()
  64. } else if (dirPath === '.') {
  65. // click move
  66. cy.contains('button', 'Copy').should('be.visible').click()
  67. } else {
  68. const directories = dirPath.split('/')
  69. directories.forEach((directory) => {
  70. // select the folder
  71. cy.get(`[data-filename="${directory}"]`).should('be.visible').click()
  72. })
  73. // click move
  74. cy.contains('button', `Move to ${directories.at(-1)}`).should('be.visible').click()
  75. }
  76. cy.wait('@moveFile')
  77. })
  78. }
  79. export const copyFile = (fileName: string, dirPath: string) => {
  80. getRowForFile(fileName).should('be.visible')
  81. triggerActionForFile(fileName, 'move-copy')
  82. cy.get('.file-picker').within(() => {
  83. // intercept the copy so we can wait for it
  84. cy.intercept('COPY', /\/remote.php\/dav\/files\//).as('copyFile')
  85. if (dirPath === '/') {
  86. // select home folder
  87. cy.get('button[title="Home"]').should('be.visible').click()
  88. // click copy
  89. cy.contains('button', 'Copy').should('be.visible').click()
  90. } else if (dirPath === '.') {
  91. // click copy
  92. cy.contains('button', 'Copy').should('be.visible').click()
  93. } else {
  94. const directories = dirPath.split('/')
  95. directories.forEach((directory) => {
  96. // select the folder
  97. cy.get(`[data-filename="${CSS.escape(directory)}"]`).should('be.visible').click()
  98. })
  99. // click copy
  100. cy.contains('button', `Copy to ${directories.at(-1)}`).should('be.visible').click()
  101. }
  102. cy.wait('@copyFile')
  103. })
  104. }
  105. export const renameFile = (fileName: string, newFileName: string) => {
  106. getRowForFile(fileName)
  107. triggerActionForFile(fileName, 'rename')
  108. // intercept the move so we can wait for it
  109. cy.intercept('MOVE', /\/remote.php\/dav\/files\//).as('moveFile')
  110. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').clear()
  111. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').type(`${newFileName}{enter}`)
  112. cy.wait('@moveFile')
  113. }
  114. export const navigateToFolder = (dirPath: string) => {
  115. const directories = dirPath.split('/')
  116. directories.forEach((directory) => {
  117. getRowForFile(directory).should('be.visible').find('[data-cy-files-list-row-name-link]').click()
  118. })
  119. }
  120. export const closeSidebar = () => {
  121. // {force: true} as it might be hidden behind toasts
  122. cy.get('[data-cy-sidebar] .app-sidebar__close').click({ force: true })
  123. }
  124. export const clickOnBreadcrumbs = (label: string) => {
  125. cy.intercept('PROPFIND', /\/remote.php\/dav\//).as('propfind')
  126. cy.get('[data-cy-files-content-breadcrumbs]').contains(label).click()
  127. cy.wait('@propfind')
  128. }