filesVersionsUtils.ts 3.2 KB

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