FilesUtils.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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]')
  31. .findByRole('checkbox', { checked: false })
  32. .click({ force: true })
  33. }
  34. export const deselectAllFiles = () => {
  35. cy.get('[data-cy-files-list-selection-checkbox]')
  36. .findByRole('checkbox', { checked: true })
  37. .click({ force: true })
  38. }
  39. export const selectRowForFile = (filename: string, options: Partial<Cypress.ClickOptions> = {}) => {
  40. getRowForFile(filename)
  41. .find('[data-cy-files-list-row-checkbox]')
  42. .findByRole('checkbox')
  43. // don't use click to avoid triggering side effects events
  44. .trigger('change', { ...options, force: true })
  45. .should('be.checked')
  46. cy.get('[data-cy-files-list-selection-checkbox]').findByRole('checkbox').should('satisfy', (elements) => {
  47. return elements.length === 1 && (elements[0].checked === true || elements[0].indeterminate === true)
  48. })
  49. }
  50. export const triggerSelectionAction = (actionId: string) => {
  51. cy.get(`button[data-cy-files-list-selection-action="${CSS.escape(actionId)}"]`).should('exist').click()
  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|public)\.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|public)\.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|public)\.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. }
  129. export const createFolder = (folderName: string) => {
  130. cy.intercept('MKCOL', /\/remote.php\/dav\/files\//).as('createFolder')
  131. // TODO: replace by proper data-cy selectors
  132. cy.get('[data-cy-upload-picker] .action-item__menutoggle').first().click()
  133. cy.get('[data-cy-upload-picker-menu-entry="newFolder"] button').click()
  134. cy.get('[data-cy-files-new-node-dialog]').should('be.visible')
  135. cy.get('[data-cy-files-new-node-dialog-input]').type(`{selectall}${folderName}`)
  136. cy.get('[data-cy-files-new-node-dialog-submit]').click()
  137. cy.wait('@createFolder')
  138. getRowForFile(folderName).should('be.visible')
  139. }
  140. /**
  141. * Check validity of an input element
  142. * @param validity The expected validity message (empty string means it is valid)
  143. * @example
  144. * ```js
  145. * cy.findByRole('textbox')
  146. * .should(haveValidity(/must not be empty/i))
  147. * ```
  148. */
  149. export const haveValidity = (validity: string | RegExp) => {
  150. if (typeof validity === 'string') {
  151. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.equal(validity)
  152. }
  153. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.match(validity)
  154. }