FilesUtils.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, dirName: 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 (dirName === '/') {
  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 (dirName === '.') {
  54. // click move
  55. cy.contains('button', 'Copy').should('be.visible').click()
  56. } else {
  57. // select the folder
  58. cy.get(`[data-filename="${dirName}"]`).should('be.visible').click()
  59. // click move
  60. cy.contains('button', `Move to ${dirName}`).should('be.visible').click()
  61. }
  62. cy.wait('@moveFile')
  63. })
  64. }
  65. export const copyFile = (fileName: string, dirName: string) => {
  66. getRowForFile(fileName).should('be.visible')
  67. triggerActionForFile(fileName, 'move-copy')
  68. cy.get('.file-picker').within(() => {
  69. // intercept the copy so we can wait for it
  70. cy.intercept('COPY', /\/remote.php\/dav\/files\//).as('copyFile')
  71. if (dirName === '/') {
  72. // select home folder
  73. cy.get('button[title="Home"]').should('be.visible').click()
  74. // click copy
  75. cy.contains('button', 'Copy').should('be.visible').click()
  76. } else if (dirName === '.') {
  77. // click copy
  78. cy.contains('button', 'Copy').should('be.visible').click()
  79. } else {
  80. // select folder
  81. cy.get(`[data-filename="${CSS.escape(dirName)}"]`).should('be.visible').click()
  82. // click copy
  83. cy.contains('button', `Copy to ${dirName}`).should('be.visible').click()
  84. }
  85. cy.wait('@copyFile')
  86. })
  87. }
  88. export const renameFile = (fileName: string, newFileName: string) => {
  89. getRowForFile(fileName)
  90. triggerActionForFile(fileName, 'rename')
  91. // intercept the move so we can wait for it
  92. cy.intercept('MOVE', /\/remote.php\/dav\/files\//).as('moveFile')
  93. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').clear()
  94. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').type(`${newFileName}{enter}`)
  95. cy.wait('@moveFile')
  96. }
  97. export const navigateToFolder = (folderName: string) => {
  98. getRowForFile(folderName).should('be.visible').find('[data-cy-files-list-row-name-link]').click()
  99. }
  100. export const closeSidebar = () => {
  101. cy.get('[cy-data-sidebar] .app-sidebar__close').click()
  102. }