files-renaming.cy.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import type { User } from '@nextcloud/cypress'
  6. import { getRowForFile, renameFile, triggerActionForFile } from './FilesUtils'
  7. import { assertNotExistOrNotVisible } from '../settings/usersUtils'
  8. const haveValidity = (validity: string | RegExp) => {
  9. if (typeof validity === 'string') {
  10. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.equal(validity)
  11. }
  12. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.match(validity)
  13. }
  14. describe('files: Rename nodes', { testIsolation: true }, () => {
  15. let user: User
  16. beforeEach(() => cy.createRandomUser().then(($user) => {
  17. user = $user
  18. cy.uploadContent(user, new Blob([]), 'text/plain', '/file.txt')
  19. cy.login(user)
  20. cy.visit('/apps/files')
  21. }))
  22. it('can rename a file', () => {
  23. // All are visible by default
  24. getRowForFile('file.txt').should('be.visible')
  25. triggerActionForFile('file.txt', 'rename')
  26. getRowForFile('file.txt')
  27. .findByRole('textbox', { name: 'Filename' })
  28. .should('be.visible')
  29. .type('{selectAll}other.txt')
  30. .should(haveValidity(''))
  31. .type('{enter}')
  32. // See it is renamed
  33. getRowForFile('other.txt').should('be.visible')
  34. })
  35. /**
  36. * If this test gets flaky than we have a problem:
  37. * It means that the selection is not reliable set to the basename
  38. */
  39. it('only selects basename of file', () => {
  40. // All are visible by default
  41. getRowForFile('file.txt').should('be.visible')
  42. triggerActionForFile('file.txt', 'rename')
  43. getRowForFile('file.txt')
  44. .findByRole('textbox', { name: 'Filename' })
  45. .should('be.visible')
  46. .should((el) => {
  47. const input = el.get(0) as HTMLInputElement
  48. expect(input.selectionStart).to.equal(0)
  49. expect(input.selectionEnd).to.equal('file'.length)
  50. })
  51. })
  52. it('show validation error on file rename', () => {
  53. // All are visible by default
  54. getRowForFile('file.txt').should('be.visible')
  55. triggerActionForFile('file.txt', 'rename')
  56. getRowForFile('file.txt')
  57. .findByRole('textbox', { name: 'Filename' })
  58. .should('be.visible')
  59. .type('{selectAll}.htaccess')
  60. // See validity
  61. .should(haveValidity(/reserved name/i))
  62. })
  63. it('shows accessible loading information', () => {
  64. const { resolve, promise } = Promise.withResolvers()
  65. getRowForFile('file.txt').should('be.visible')
  66. // intercept the rename (MOVE)
  67. // the callback will wait until the promise resolve (so we have time to check the loading state)
  68. cy.intercept(
  69. 'MOVE',
  70. /\/remote.php\/dav\/files\//,
  71. (request) => {
  72. // we need to wait in the onResponse handler as the intercept handler times out otherwise
  73. request.on('response', async () => { await promise })
  74. },
  75. ).as('moveFile')
  76. // Start the renaming
  77. triggerActionForFile('file.txt', 'rename')
  78. getRowForFile('file.txt')
  79. .findByRole('textbox', { name: 'Filename' })
  80. .should('be.visible')
  81. .type('{selectAll}new-name.txt{enter}')
  82. // Loading state is visible
  83. getRowForFile('new-name.txt')
  84. .findByRole('img', { name: 'File is loading' })
  85. .should('be.visible')
  86. // checkbox is not visible
  87. getRowForFile('new-name.txt')
  88. .findByRole('checkbox', { name: /^Toggle selection/ })
  89. .should('not.exist')
  90. cy.log('Resolve promise to preoceed with MOVE request')
  91. .then(() => resolve(null))
  92. // Ensure the request is done (file renamed)
  93. cy.wait('@moveFile')
  94. // checkbox visible again
  95. getRowForFile('new-name.txt')
  96. .findByRole('checkbox', { name: /^Toggle selection/ })
  97. .should('exist')
  98. // see the loading state is gone
  99. getRowForFile('new-name.txt')
  100. .findByRole('img', { name: 'File is loading' })
  101. .should('not.exist')
  102. })
  103. /**
  104. * This is a regression test of: https://github.com/nextcloud/server/issues/47438
  105. * The issue was that the renaming state was not reset when the new name moved the file out of the view of the current files list
  106. * due to virtual scrolling the renaming state was not changed then by the UI events (as the component was taken out of DOM before any event handling).
  107. */
  108. it('correctly resets renaming state', () => {
  109. for (let i = 1; i <= 20; i++) {
  110. cy.uploadContent(user, new Blob([]), 'text/plain', `/file${i}.txt`)
  111. }
  112. cy.viewport(1200, 500) // 500px is smaller then 20 * 50 which is the place that the files take up
  113. cy.login(user)
  114. cy.visit('/apps/files')
  115. getRowForFile('file.txt').should('be.visible')
  116. // Z so it is shown last
  117. renameFile('file.txt', 'zzz.txt')
  118. // not visible any longer
  119. getRowForFile('zzz.txt')
  120. .should(assertNotExistOrNotVisible)
  121. // scroll file list to bottom
  122. cy.get('[data-cy-files-list]').scrollTo('bottom')
  123. cy.screenshot()
  124. // The file is no longer in rename state
  125. getRowForFile('zzz.txt')
  126. .should('be.visible')
  127. .findByRole('textbox', { name: 'Filename' })
  128. .should('not.exist')
  129. })
  130. })