FilesUtils.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // Getting the last button to avoid the one from popup fading out
  14. cy.get(`[data-cy-files-list-row-action="${CSS.escape(actionId)}"] > button`).last()
  15. .should('exist').click()
  16. }
  17. export const triggerActionForFile = (filename: string, actionId: string) => {
  18. getActionButtonForFile(filename).click()
  19. // Getting the last button to avoid the one from popup fading out
  20. cy.get(`[data-cy-files-list-row-action="${CSS.escape(actionId)}"] > button`).last()
  21. .should('exist').click()
  22. }
  23. export const triggerInlineActionForFileId = (fileid: number, actionId: string) => {
  24. getActionsForFileId(fileid).find(`button[data-cy-files-list-row-action="${CSS.escape(actionId)}"]`).should('exist').click()
  25. }
  26. export const triggerInlineActionForFile = (filename: string, actionId: string) => {
  27. getActionsForFile(filename).get(`button[data-cy-files-list-row-action="${CSS.escape(actionId)}"]`).should('exist').click()
  28. }
  29. export const selectAllFiles = () => {
  30. cy.get('[data-cy-files-list-selection-checkbox]').findByRole('checkbox').click({ force: true })
  31. }
  32. export const selectRowForFile = (filename: string) => {
  33. getRowForFile(filename)
  34. .find('[data-cy-files-list-row-checkbox]')
  35. .findByRole('checkbox')
  36. .click({ force: true })
  37. .should('be.checked')
  38. cy.get('[data-cy-files-list-selection-checkbox]').findByRole('checkbox').should('satisfy', (elements) => {
  39. return elements.length === 1 && (elements[0].checked === true || elements[0].indeterminate === true)
  40. })
  41. }
  42. export const triggerSelectionAction = (actionId: string) => {
  43. cy.get(`button[data-cy-files-list-selection-action="${CSS.escape(actionId)}"]`).should('exist').click()
  44. }
  45. export const moveFile = (fileName: string, dirPath: string) => {
  46. getRowForFile(fileName).should('be.visible')
  47. triggerActionForFile(fileName, 'move-copy')
  48. cy.get('.file-picker').within(() => {
  49. // intercept the copy so we can wait for it
  50. cy.intercept('MOVE', /\/(remote|public)\.php\/dav\/files\//).as('moveFile')
  51. if (dirPath === '/') {
  52. // select home folder
  53. cy.get('button[title="Home"]').should('be.visible').click()
  54. // click move
  55. cy.contains('button', 'Move').should('be.visible').click()
  56. } else if (dirPath === '.') {
  57. // click move
  58. cy.contains('button', 'Copy').should('be.visible').click()
  59. } else {
  60. const directories = dirPath.split('/')
  61. directories.forEach((directory) => {
  62. // select the folder
  63. cy.get(`[data-filename="${directory}"]`).should('be.visible').click()
  64. })
  65. // click move
  66. cy.contains('button', `Move to ${directories.at(-1)}`).should('be.visible').click()
  67. }
  68. cy.wait('@moveFile')
  69. })
  70. }
  71. export const copyFile = (fileName: string, dirPath: string) => {
  72. getRowForFile(fileName).should('be.visible')
  73. triggerActionForFile(fileName, 'move-copy')
  74. cy.get('.file-picker').within(() => {
  75. // intercept the copy so we can wait for it
  76. cy.intercept('COPY', /\/(remote|public)\.php\/dav\/files\//).as('copyFile')
  77. if (dirPath === '/') {
  78. // select home folder
  79. cy.get('button[title="Home"]').should('be.visible').click()
  80. // click copy
  81. cy.contains('button', 'Copy').should('be.visible').click()
  82. } else if (dirPath === '.') {
  83. // click copy
  84. cy.contains('button', 'Copy').should('be.visible').click()
  85. } else {
  86. const directories = dirPath.split('/')
  87. directories.forEach((directory) => {
  88. // select the folder
  89. cy.get(`[data-filename="${CSS.escape(directory)}"]`).should('be.visible').click()
  90. })
  91. // click copy
  92. cy.contains('button', `Copy to ${directories.at(-1)}`).should('be.visible').click()
  93. }
  94. cy.wait('@copyFile')
  95. })
  96. }
  97. export const renameFile = (fileName: string, newFileName: string) => {
  98. getRowForFile(fileName)
  99. triggerActionForFile(fileName, 'rename')
  100. // intercept the move so we can wait for it
  101. cy.intercept('MOVE', /\/(remote|public)\.php\/dav\/files\//).as('moveFile')
  102. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').clear()
  103. getRowForFile(fileName).find('[data-cy-files-list-row-name] input').type(`${newFileName}{enter}`)
  104. cy.wait('@moveFile')
  105. }
  106. export const navigateToFolder = (dirPath: string) => {
  107. const directories = dirPath.split('/')
  108. directories.forEach((directory) => {
  109. getRowForFile(directory).should('be.visible').find('[data-cy-files-list-row-name-link]').click()
  110. })
  111. }
  112. export const closeSidebar = () => {
  113. // {force: true} as it might be hidden behind toasts
  114. cy.get('[data-cy-sidebar] .app-sidebar__close').click({ force: true })
  115. }
  116. export const clickOnBreadcrumbs = (label: string) => {
  117. cy.intercept('PROPFIND', /\/remote.php\/dav\//).as('propfind')
  118. cy.get('[data-cy-files-content-breadcrumbs]').contains(label).click()
  119. cy.wait('@propfind')
  120. }
  121. export const createFolder = (folderName: string) => {
  122. cy.intercept('MKCOL', /\/remote.php\/dav\/files\//).as('createFolder')
  123. // TODO: replace by proper data-cy selectors
  124. cy.get('[data-cy-upload-picker] .action-item__menutoggle').first().click()
  125. cy.contains('.upload-picker__menu-entry button', 'New folder').click()
  126. cy.get('[data-cy-files-new-node-dialog]').should('be.visible')
  127. cy.get('[data-cy-files-new-node-dialog-input]').type(`{selectall}${folderName}`)
  128. cy.get('[data-cy-files-new-node-dialog-submit]').click()
  129. cy.wait('@createFolder')
  130. getRowForFile(folderName).should('be.visible')
  131. }
  132. /**
  133. * Check validity of an input element
  134. * @param validity The expected validity message (empty string means it is valid)
  135. * @example
  136. * ```js
  137. * cy.findByRole('textbox')
  138. * .should(haveValidity(/must not be empty/i))
  139. * ```
  140. */
  141. export const haveValidity = (validity: string | RegExp) => {
  142. if (typeof validity === 'string') {
  143. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.equal(validity)
  144. }
  145. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.match(validity)
  146. }