files-renaming.cy.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, triggerActionForFile } from './FilesUtils'
  7. const haveValidity = (validity: string | RegExp) => {
  8. if (typeof validity === 'string') {
  9. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.equal(validity)
  10. }
  11. return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.match(validity)
  12. }
  13. describe('files: Rename nodes', { testIsolation: true }, () => {
  14. let user: User
  15. beforeEach(() => cy.createRandomUser().then(($user) => {
  16. user = $user
  17. cy.uploadContent(user, new Blob([]), 'text/plain', '/file.txt')
  18. cy.login(user)
  19. cy.visit('/apps/files')
  20. }))
  21. it('can rename a file', () => {
  22. // All are visible by default
  23. getRowForFile('file.txt').should('be.visible')
  24. triggerActionForFile('file.txt', 'rename')
  25. getRowForFile('file.txt')
  26. .findByRole('textbox', { name: 'Filename' })
  27. .should('be.visible')
  28. .type('{selectAll}other.txt')
  29. .should(haveValidity(''))
  30. .type('{enter}')
  31. // See it is renamed
  32. getRowForFile('other.txt').should('be.visible')
  33. })
  34. /**
  35. * If this test gets flaky than we have a problem:
  36. * It means that the selection is not reliable set to the basename
  37. */
  38. it('only selects basename of file', () => {
  39. // All are visible by default
  40. getRowForFile('file.txt').should('be.visible')
  41. triggerActionForFile('file.txt', 'rename')
  42. getRowForFile('file.txt')
  43. .findByRole('textbox', { name: 'Filename' })
  44. .should('be.visible')
  45. .should((el) => {
  46. const input = el.get(0) as HTMLInputElement
  47. expect(input.selectionStart).to.equal(0)
  48. expect(input.selectionEnd).to.equal('file'.length)
  49. })
  50. })
  51. it('show validation error on file rename', () => {
  52. // All are visible by default
  53. getRowForFile('file.txt').should('be.visible')
  54. triggerActionForFile('file.txt', 'rename')
  55. getRowForFile('file.txt')
  56. .findByRole('textbox', { name: 'Filename' })
  57. .should('be.visible')
  58. .type('{selectAll}.htaccess')
  59. // See validity
  60. .should(haveValidity(/reserved name/i))
  61. })
  62. })