|
20 | 20 | * |
21 | 21 | */ |
22 | 22 |
|
| 23 | +import type { User } from '@nextcloud/cypress' |
| 24 | + |
| 25 | +export const selectAllFiles = () => { |
| 26 | + cy.get('[data-cy-files-list-selection-checkbox]') |
| 27 | + .findByRole('checkbox', { checked: false }) |
| 28 | + .click({ force: true }) |
| 29 | +} |
| 30 | +export const deselectAllFiles = () => { |
| 31 | + cy.get('[data-cy-files-list-selection-checkbox]') |
| 32 | + .findByRole('checkbox', { checked: true }) |
| 33 | + .click({ force: true }) |
| 34 | +} |
| 35 | + |
23 | 36 | export const getRowForFileId = (fileid: number) => cy.get(`[data-cy-files-list-row-fileid="${fileid}"]`) |
24 | 37 | export const getRowForFile = (filename: string) => cy.get(`[data-cy-files-list-row-name="${CSS.escape(filename)}"]`) |
25 | 38 |
|
@@ -181,3 +194,93 @@ export const clickOnBreadcrumbs = (label: string) => { |
181 | 194 | cy.get('[data-cy-files-content-breadcrumbs]').contains(label).click() |
182 | 195 | cy.wait('@propfind') |
183 | 196 | } |
| 197 | + |
| 198 | +/** |
| 199 | + * Check validity of an input element |
| 200 | + * @param validity The expected validity message (empty string means it is valid) |
| 201 | + * @example |
| 202 | + * ```js |
| 203 | + * cy.findByRole('textbox') |
| 204 | + * .should(haveValidity(/must not be empty/i)) |
| 205 | + * ``` |
| 206 | + */ |
| 207 | +export const haveValidity = (validity: string | RegExp) => { |
| 208 | + if (typeof validity === 'string') { |
| 209 | + return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.equal(validity) |
| 210 | + } |
| 211 | + return (el: JQuery<HTMLElement>) => expect((el.get(0) as HTMLInputElement).validationMessage).to.match(validity) |
| 212 | +} |
| 213 | + |
| 214 | +export const deleteFileWithRequest = (user: User, path: string) => { |
| 215 | + // Ensure path starts with a slash and has no double slashes |
| 216 | + path = `/${path}`.replace(/\/+/g, '/') |
| 217 | + |
| 218 | + cy.request('/csrftoken').then(({ body }) => { |
| 219 | + const requestToken = body.token |
| 220 | + cy.request({ |
| 221 | + method: 'DELETE', |
| 222 | + url: `${Cypress.env('baseUrl')}/remote.php/dav/files/${user.userId}${path}`, |
| 223 | + auth: { |
| 224 | + user: user.userId, |
| 225 | + password: user.password, |
| 226 | + }, |
| 227 | + headers: { |
| 228 | + requestToken, |
| 229 | + }, |
| 230 | + retryOnStatusCodeFailure: true, |
| 231 | + }) |
| 232 | + }) |
| 233 | +} |
| 234 | + |
| 235 | +export const triggerFileListAction = (actionId: string) => { |
| 236 | + cy.get(`button[data-cy-files-list-action="${CSS.escape(actionId)}"]`).last() |
| 237 | + .should('exist').click({ force: true }) |
| 238 | +} |
| 239 | + |
| 240 | +export const reloadCurrentFolder = () => { |
| 241 | + cy.intercept('PROPFIND', /\/remote.php\/dav\//).as('propfind') |
| 242 | + cy.get('[data-cy-files-content-breadcrumbs]').findByRole('button', { description: 'Reload current directory' }).click() |
| 243 | + cy.wait('@propfind') |
| 244 | +} |
| 245 | + |
| 246 | +/** |
| 247 | + * Enable the grid mode for the files list. |
| 248 | + * Will fail if already enabled! |
| 249 | + */ |
| 250 | +export function enableGridMode() { |
| 251 | + cy.intercept('**/apps/files/api/v1/config/grid_view').as('setGridMode') |
| 252 | + cy.findByRole('button', { name: 'Switch to grid view' }) |
| 253 | + .should('be.visible') |
| 254 | + .click() |
| 255 | + cy.wait('@setGridMode') |
| 256 | +} |
| 257 | + |
| 258 | +/** |
| 259 | + * Calculate the needed viewport height to limit the visible rows of the file list. |
| 260 | + * Requires a logged in user. |
| 261 | + * |
| 262 | + * @param rows The number of rows that should be displayed at the same time |
| 263 | + */ |
| 264 | +export function calculateViewportHeight(rows: number): Cypress.Chainable<number> { |
| 265 | + cy.visit('/apps/files') |
| 266 | + |
| 267 | + return cy.get('[data-cy-files-list]') |
| 268 | + .should('be.visible') |
| 269 | + .then((filesList) => { |
| 270 | + const windowHeight = Cypress.$('body').outerHeight()! |
| 271 | + // Size of other page elements |
| 272 | + const outerHeight = Math.ceil(windowHeight - filesList.outerHeight()!) |
| 273 | + // Size of before and filters |
| 274 | + const beforeHeight = Math.ceil(Cypress.$('.files-list__before').outerHeight()!) |
| 275 | + const filterHeight = Math.ceil(Cypress.$('.files-list__filters').outerHeight()!) |
| 276 | + // Size of the table header |
| 277 | + const tableHeaderHeight = Math.ceil(Cypress.$('[data-cy-files-list-thead]').outerHeight()!) |
| 278 | + // table row height |
| 279 | + const rowHeight = Math.ceil(Cypress.$('[data-cy-files-list-tbody] tr').outerHeight()!) |
| 280 | + |
| 281 | + // sum it up |
| 282 | + const viewportHeight = outerHeight + beforeHeight + filterHeight + tableHeaderHeight + rows * rowHeight |
| 283 | + cy.log(`Calculated viewport height: ${viewportHeight} (${outerHeight} + ${beforeHeight} + ${filterHeight} + ${tableHeaderHeight} + ${rows} * ${rowHeight})`) |
| 284 | + return cy.wrap(viewportHeight) |
| 285 | + }) |
| 286 | +} |
0 commit comments