nextcloud/cypress/e2e/files_versions/filesVersionsUtils.ts
Ferdinand Thiessen fd96a32dda
test: adjust cypress tests
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
2026-01-05 02:23:30 +01:00

97 lines
3.3 KiB
TypeScript

/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { User } from '@nextcloud/e2e-test-server/cypress'
import type { ShareSetting } from '../files_sharing/FilesSharingUtils.ts'
import { basename } from '@nextcloud/paths'
import { triggerActionForFile } from '../files/FilesUtils.ts'
import { createShare } from '../files_sharing/FilesSharingUtils.ts'
export function uploadThreeVersions(user: User, fileName: string) {
// A new version will not be created if the changes occur
// within less than one second of each other.
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.uploadContent(user, new Blob(['v1'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
.wait(1100)
.uploadContent(user, new Blob(['v2'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
.wait(1100)
.uploadContent(user, new Blob(['v3'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
cy.login(user)
}
export function openVersionsPanel(fileName: string) {
// Detect the versions list fetch
cy.intercept('PROPFIND', '**/dav/versions/*/versions/**').as('getVersions')
triggerActionForFile(basename(fileName), 'details')
cy.get('[data-cy-sidebar]')
.as('sidebar')
.should('be.visible')
cy.get('@sidebar')
.find('[aria-controls="tab-files_versions"]')
.click()
// Wait for the versions list to be fetched
cy.wait('@getVersions')
cy.get('#tab-files_versions').should('be.visible', { timeout: 10000 })
}
export function toggleVersionMenu(index: number) {
cy.get('#tab-files_versions [data-files-versions-version]')
.eq(index)
.find('button')
.click()
}
export function triggerVersionAction(index: number, actionName: string) {
toggleVersionMenu(index)
cy.get(`[data-cy-files-versions-version-action="${actionName}"]`).filter(':visible').click()
}
export function nameVersion(index: number, name: string) {
cy.intercept('PROPPATCH', '**/dav/versions/*/versions/**').as('labelVersion')
triggerVersionAction(index, 'label')
cy.get(':focused').type(`${name}{enter}`)
cy.wait('@labelVersion')
}
export function restoreVersion(index: number) {
cy.intercept('MOVE', '**/dav/versions/*/versions/**').as('restoreVersion')
triggerVersionAction(index, 'restore')
cy.wait('@restoreVersion')
}
export function deleteVersion(index: number) {
cy.intercept('DELETE', '**/dav/versions/*/versions/**').as('deleteVersion')
triggerVersionAction(index, 'delete')
cy.wait('@deleteVersion')
}
export function doesNotHaveAction(index: number, actionName: string) {
toggleVersionMenu(index)
cy.get(`[data-cy-files-versions-version-action="${actionName}"]`).should('not.exist')
toggleVersionMenu(index)
}
export function assertVersionContent(index: number, expectedContent: string) {
cy.intercept({ method: 'GET', times: 1, url: 'remote.php/**' }).as('downloadVersion')
triggerVersionAction(index, 'download')
cy.wait('@downloadVersion')
.then(({ response }) => expect(response?.body).to.equal(expectedContent))
}
export function setupTestSharedFileFromUser(owner: User, randomFileName: string, shareOptions: Partial<ShareSetting>) {
return cy.createRandomUser()
.then((recipient) => {
cy.login(owner)
cy.visit('/apps/files')
createShare(randomFileName, recipient.userId, shareOptions)
cy.login(recipient)
cy.visit('/apps/files')
return cy.wrap(recipient)
})
}