1
0

files-renaming.cy.ts 4.5 KB

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