FilesUtils.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 moveFile = (fileName: string, dirPath: string) => {
  43. getRowForFile(fileName).should('be.visible')
  44. triggerActionForFile(fileName, 'move-copy')
  45. cy.get('.file-picker').within(() => {
  46. // intercept the copy so we can wait for it
  47. cy.intercept('MOVE', /\/remote.php\/dav\/files\//).as('moveFile')
  48. if (dirPath === '/') {
  49. // select home folder
  50. cy.get('button[title="Home"]').should('be.visible').click()
  51. // click move
  52. cy.contains('button', 'Move').should('be.visible').click()
  53. } else if (dirPath === '.') {
  54. // click move
  55. cy.contains('button', 'Copy').should('be.visible').click()
  56. } else {
  57. const directories = dirPath.split('/')
  58. directories.forEach((directory) => {
  59. // select the folder
  60. cy.get(`[data-filename="${directory}"]`).should('be.visible').click()
  61. })
  62. // click move
  63. cy.contains('button', `Move to ${directories.at(-1)}`).should('be.visible').click()
  64. }
  65. cy.wait('@moveFile')
  66. })
  67. }
  68. export const copyFile = (fileName: string, dirPath: string) => {
  69. getRowForFile(fileName).should('be.visible')
  70. triggerActionForFile(fileName, 'move-copy')
  71. cy.get('.file-picker').within(() => {
  72. // intercept the copy so we can wait for it
  73. cy.intercept('COPY', /\/remote.php\/dav\/files\//).as('copyFile')
  74. if (dirPath === '/') {
  75. // select home folder
  76. cy.get('button[title="Home"]').should('be.visible').click()
  77. // click copy
  78. cy.contains('button', 'Copy').should('be.visible').click()
  79. } else if (dirPath === '.') {
  80. // click copy
  81. cy.contains('button', 'Copy').should('be.visible').click()
  82. } else {
  83. const directories = dirPath.split('/')
  84. directories.forEach((directory) => {
  85. // select the folder
  86. cy.get(`[data-filename="${CSS.escape(directory)}"]`).should('be.visible').click()
  87. })
  88. // click copy
  89. cy.contains('button', `Copy to ${directories.at(-1)}`).should('be.visible').click()
  90. }
  91. cy.wait('@copyFile')
  92. })
  93. }
  94. export const renameFile = (fileName: string, newFileName: string) => {
  95. getRowForFile(fileName)
  96. triggerActionForFile(fileName, 'rename')
  97. // intercept the move so we can wait for it
  98. cy.intercept('MOVE', /\/remote.php\/dav\/files\//).as('moveFile')
  99. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').clear()
  100. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').type(`${newFileName}{enter}`)
  101. cy.wait('@moveFile')
  102. }
  103. export const navigateToFolder = (dirPath: string) => {
  104. const directories = dirPath.split('/')
  105. directories.forEach((directory) => {
  106. getRowForFile(directory).should('be.visible').find('[data-cy-files-list-row-name-link]').click()
  107. })
  108. }
  109. export const closeSidebar = () => {
  110. // {force: true} as it might be hidden behind toasts
  111. cy.get('[cy-data-sidebar] .app-sidebar__close').click({ force: true })
  112. }
  113. export const clickOnBreadcrumbs = (label: string) => {
  114. cy.intercept('PROPFIND', /\/remote.php\/dav\//).as('propfind')
  115. cy.get('[data-cy-files-content-breadcrumbs]').contains(label).click()
  116. cy.wait('@propfind')
  117. }