FilesUtils.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. export const getRowForFileId = (fileid: number) => cy.get(`[data-cy-files-list-row-fileid="${fileid}"]`)
  6. export const getRowForFile = (filename: string) => cy.get(`[data-cy-files-list-row-name="${CSS.escape(filename)}"]`)
  7. export const getActionsForFileId = (fileid: number) => getRowForFileId(fileid).find('[data-cy-files-list-row-actions]')
  8. export const getActionsForFile = (filename: string) => getRowForFile(filename).find('[data-cy-files-list-row-actions]')
  9. export const getActionButtonForFileId = (fileid: number) => getActionsForFileId(fileid).findByRole('button', { name: 'Actions' })
  10. export const getActionButtonForFile = (filename: string) => getActionsForFile(filename).findByRole('button', { name: 'Actions' })
  11. export const triggerActionForFileId = (fileid: number, actionId: string) => {
  12. getActionButtonForFileId(fileid).click()
  13. cy.get(`[data-cy-files-list-row-action="${CSS.escape(actionId)}"] > button`).should('exist').click()
  14. }
  15. export const triggerActionForFile = (filename: string, actionId: string) => {
  16. getActionButtonForFile(filename).click()
  17. cy.get(`[data-cy-files-list-row-action="${CSS.escape(actionId)}"] > button`).should('exist').click()
  18. }
  19. export const triggerInlineActionForFileId = (fileid: number, actionId: string) => {
  20. getActionsForFileId(fileid).find(`button[data-cy-files-list-row-action="${CSS.escape(actionId)}"]`).should('exist').click()
  21. }
  22. export const triggerInlineActionForFile = (filename: string, actionId: string) => {
  23. getActionsForFile(filename).get(`button[data-cy-files-list-row-action="${CSS.escape(actionId)}"]`).should('exist').click()
  24. }
  25. export const moveFile = (fileName: string, dirPath: string) => {
  26. getRowForFile(fileName).should('be.visible')
  27. triggerActionForFile(fileName, 'move-copy')
  28. cy.get('.file-picker').within(() => {
  29. // intercept the copy so we can wait for it
  30. cy.intercept('MOVE', /\/(remote|public)\.php\/dav\/files\//).as('moveFile')
  31. if (dirPath === '/') {
  32. // select home folder
  33. cy.get('button[title="Home"]').should('be.visible').click()
  34. // click move
  35. cy.contains('button', 'Move').should('be.visible').click()
  36. } else if (dirPath === '.') {
  37. // click move
  38. cy.contains('button', 'Copy').should('be.visible').click()
  39. } else {
  40. const directories = dirPath.split('/')
  41. directories.forEach((directory) => {
  42. // select the folder
  43. cy.get(`[data-filename="${directory}"]`).should('be.visible').click()
  44. })
  45. // click move
  46. cy.contains('button', `Move to ${directories.at(-1)}`).should('be.visible').click()
  47. }
  48. cy.wait('@moveFile')
  49. })
  50. }
  51. export const copyFile = (fileName: string, dirPath: string) => {
  52. getRowForFile(fileName).should('be.visible')
  53. triggerActionForFile(fileName, 'move-copy')
  54. cy.get('.file-picker').within(() => {
  55. // intercept the copy so we can wait for it
  56. cy.intercept('COPY', /\/(remote|public)\.php\/dav\/files\//).as('copyFile')
  57. if (dirPath === '/') {
  58. // select home folder
  59. cy.get('button[title="Home"]').should('be.visible').click()
  60. // click copy
  61. cy.contains('button', 'Copy').should('be.visible').click()
  62. } else if (dirPath === '.') {
  63. // click copy
  64. cy.contains('button', 'Copy').should('be.visible').click()
  65. } else {
  66. const directories = dirPath.split('/')
  67. directories.forEach((directory) => {
  68. // select the folder
  69. cy.get(`[data-filename="${CSS.escape(directory)}"]`).should('be.visible').click()
  70. })
  71. // click copy
  72. cy.contains('button', `Copy to ${directories.at(-1)}`).should('be.visible').click()
  73. }
  74. cy.wait('@copyFile')
  75. })
  76. }
  77. export const renameFile = (fileName: string, newFileName: string) => {
  78. getRowForFile(fileName)
  79. triggerActionForFile(fileName, 'rename')
  80. // intercept the move so we can wait for it
  81. cy.intercept('MOVE', /\/(remote|public)\.php\/dav\/files\//).as('moveFile')
  82. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').clear()
  83. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').type(`${newFileName}{enter}`)
  84. cy.wait('@moveFile')
  85. }
  86. export const navigateToFolder = (dirPath: string) => {
  87. const directories = dirPath.split('/')
  88. directories.forEach((directory) => {
  89. getRowForFile(directory).should('be.visible').find('[data-cy-files-list-row-name-link]').click()
  90. })
  91. }
  92. export const closeSidebar = () => {
  93. // {force: true} as it might be hidden behind toasts
  94. cy.get('[data-cy-sidebar] .app-sidebar__close').click({ force: true })
  95. }
  96. export const clickOnBreadcrumbs = (label: string) => {
  97. cy.intercept('PROPFIND', /\/remote.php\/dav\//).as('propfind')
  98. cy.get('[data-cy-files-content-breadcrumbs]').contains(label).click()
  99. cy.wait('@propfind')
  100. }
  101. export const createFolder = (folderName: string) => {
  102. cy.intercept('MKCOL', /\/remote.php\/dav\/files\//).as('createFolder')
  103. // TODO: replace by proper data-cy selectors
  104. cy.get('[data-cy-upload-picker] .action-item__menutoggle').first().click()
  105. cy.contains('.upload-picker__menu-entry button', 'New folder').click()
  106. cy.get('[data-cy-files-new-node-dialog]').should('be.visible')
  107. cy.get('[data-cy-files-new-node-dialog-input]').type(`{selectall}${folderName}`)
  108. cy.get('[data-cy-files-new-node-dialog-submit]').click()
  109. cy.wait('@createFolder')
  110. getRowForFile(folderName).should('be.visible')
  111. }
  112. /**
  113. * Check validity of an input element
  114. * @param validity The expected validity message (empty string means it is valid)
  115. * @example
  116. * ```js
  117. * cy.findByRole('textbox')
  118. * .should(haveValidity(/must not be empty/i))
  119. * ```
  120. */
  121. export const haveValidity = (validity: string | RegExp) => {
  122. if (typeof validity === 'string') {
  123. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.equal(validity)
  124. }
  125. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.match(validity)
  126. }