filesVersionsUtils.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. /* eslint-disable jsdoc/require-jsdoc */
  6. import type { User } from '@nextcloud/cypress'
  7. import { createShare, type ShareSetting } from '../files_sharing/FilesSharingUtils'
  8. export const uploadThreeVersions = (user: User, fileName: string) => {
  9. // A new version will not be created if the changes occur
  10. // within less than one second of each other.
  11. // eslint-disable-next-line cypress/no-unnecessary-waiting
  12. cy.uploadContent(user, new Blob(['v1'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
  13. .wait(1100)
  14. .uploadContent(user, new Blob(['v2'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
  15. .wait(1100)
  16. .uploadContent(user, new Blob(['v3'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
  17. cy.login(user)
  18. }
  19. export function openVersionsPanel(fileName: string) {
  20. // Detect the versions list fetch
  21. cy.intercept('PROPFIND', '**/dav/versions/*/versions/**').as('getVersions')
  22. // Open the versions tab
  23. cy.window().then(win => {
  24. win.OCA.Files.Sidebar.setActiveTab('version_vue')
  25. win.OCA.Files.Sidebar.open(`/${fileName}`)
  26. })
  27. // Wait for the versions list to be fetched
  28. cy.wait('@getVersions')
  29. cy.get('#tab-version_vue').should('be.visible', { timeout: 10000 })
  30. }
  31. export function toggleVersionMenu(index: number) {
  32. cy.get('#tab-version_vue [data-files-versions-version]')
  33. .eq(index)
  34. .find('button')
  35. .click()
  36. }
  37. export function triggerVersionAction(index: number, actionName: string) {
  38. toggleVersionMenu(index)
  39. cy.get(`[data-cy-files-versions-version-action="${actionName}"]`).filter(':visible').click()
  40. }
  41. export function nameVersion(index: number, name: string) {
  42. cy.intercept('PROPPATCH', '**/dav/versions/*/versions/**').as('labelVersion')
  43. triggerVersionAction(index, 'label')
  44. cy.get(':focused').type(`${name}{enter}`)
  45. cy.wait('@labelVersion')
  46. }
  47. export function restoreVersion(index: number) {
  48. cy.intercept('MOVE', '**/dav/versions/*/versions/**').as('restoreVersion')
  49. triggerVersionAction(index, 'restore')
  50. cy.wait('@restoreVersion')
  51. }
  52. export function deleteVersion(index: number) {
  53. cy.intercept('DELETE', '**/dav/versions/*/versions/**').as('deleteVersion')
  54. triggerVersionAction(index, 'delete')
  55. cy.wait('@deleteVersion')
  56. }
  57. export function doesNotHaveAction(index: number, actionName: string) {
  58. toggleVersionMenu(index)
  59. cy.get(`[data-cy-files-versions-version-action="${actionName}"]`).should('not.exist')
  60. toggleVersionMenu(index)
  61. }
  62. export function assertVersionContent(index: number, expectedContent: string) {
  63. cy.intercept({ method: 'GET', times: 1, url: 'remote.php/**' }).as('downloadVersion')
  64. triggerVersionAction(index, 'download')
  65. cy.wait('@downloadVersion')
  66. .then(({ response }) => expect(response?.body).to.equal(expectedContent))
  67. }
  68. export function setupTestSharedFileFromUser(owner: User, randomFileName: string, shareOptions: Partial<ShareSetting>) {
  69. return cy.createRandomUser()
  70. .then((recipient) => {
  71. cy.login(owner)
  72. cy.visit('/apps/files')
  73. createShare(randomFileName, recipient.userId, shareOptions)
  74. cy.login(recipient)
  75. cy.visit('/apps/files')
  76. return cy.wrap(recipient)
  77. })
  78. }