1
0

files-renaming.cy.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, 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. })