|
| 1 | +/* eslint-disable no-unused-expressions */ |
| 2 | +/** |
| 3 | + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + */ |
| 6 | +// dist file might not be built when running eslint only |
| 7 | +// eslint-disable-next-line import/no-unresolved,n/no-missing-import |
| 8 | +import { Folder, Permission } from '@nextcloud/files' |
| 9 | +import { generateRemoteUrl } from '@nextcloud/router' |
| 10 | +import { getUploader, UploadPicker } from '../../../lib/index.ts' |
| 11 | + |
| 12 | +let state: string | undefined |
| 13 | +before(() => { |
| 14 | + cy.window().then((win) => { |
| 15 | + state = win.document.body.innerHTML |
| 16 | + }) |
| 17 | +}) |
| 18 | + |
| 19 | +const resetDocument = () => { |
| 20 | + if (state) { |
| 21 | + cy.window().then((win) => { |
| 22 | + win.document.body.innerHTML = state! |
| 23 | + }) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +describe('UploadPicker: status testing', () => { |
| 28 | + beforeEach(() => { |
| 29 | + // Make sure we reset the destination |
| 30 | + // so other tests do not interfere |
| 31 | + const propsData = { |
| 32 | + destination: new Folder({ |
| 33 | + id: 56, |
| 34 | + owner: 'user', |
| 35 | + source: generateRemoteUrl('dav/files/user'), |
| 36 | + permissions: Permission.ALL, |
| 37 | + root: '/files/user', |
| 38 | + }), |
| 39 | + } |
| 40 | + |
| 41 | + // Mount picker |
| 42 | + const onPause = cy.spy().as('pausedListener') |
| 43 | + const onResume = cy.spy().as('resumedListener') |
| 44 | + cy.mount(UploadPicker, { |
| 45 | + propsData, |
| 46 | + listeners: { |
| 47 | + paused: onPause, |
| 48 | + resumed: onResume, |
| 49 | + }, |
| 50 | + }).as('uploadPicker') |
| 51 | + |
| 52 | + // Check and init aliases |
| 53 | + cy.get('[data-cy-upload-picker] [data-cy-upload-picker-input]').as('input').should('exist') |
| 54 | + cy.get('[data-cy-upload-picker] .upload-picker__progress').as('progress').should('exist') |
| 55 | + }) |
| 56 | + |
| 57 | + afterEach(() => resetDocument()) |
| 58 | + |
| 59 | + it('shows paused status on pause', () => { |
| 60 | + // Intercept tmp upload chunks folder creation |
| 61 | + cy.intercept('MKCOL', '/remote.php/dav/uploads/*/web-file-upload*', { |
| 62 | + statusCode: 201, |
| 63 | + }).as('init') |
| 64 | + |
| 65 | + // Intercept chunks upload |
| 66 | + cy.intercept({ |
| 67 | + method: 'PUT', |
| 68 | + url: '/remote.php/dav/uploads/*/web-file-upload*/*', |
| 69 | + }, (req) => { |
| 70 | + req.reply({ |
| 71 | + statusCode: 201, |
| 72 | + }) |
| 73 | + }).as('chunks') |
| 74 | + |
| 75 | + // Intercept final assembly request |
| 76 | + const assemblyStartStub = cy.stub().as('assemblyStart') |
| 77 | + cy.intercept('MOVE', '/remote.php/dav/uploads/*/web-file-upload*/.file', (req) => { |
| 78 | + assemblyStartStub() |
| 79 | + req.reply({ |
| 80 | + statusCode: 204, |
| 81 | + // Fake assembling chunks |
| 82 | + delay: 5000, |
| 83 | + }) |
| 84 | + }).as('assemblyEnd') |
| 85 | + |
| 86 | + // Start upload |
| 87 | + cy.get('@input').attachFile({ |
| 88 | + // Fake file of 256MB |
| 89 | + fileContent: new Blob([new ArrayBuffer(256 * 1024 * 1024)]), |
| 90 | + fileName: 'photos.zip', |
| 91 | + mimeType: 'application/zip', |
| 92 | + encoding: 'utf8', |
| 93 | + lastModified: new Date().getTime(), |
| 94 | + }) |
| 95 | + |
| 96 | + cy.wait('@init').then(() => { |
| 97 | + cy.get('[data-cy-upload-picker] .upload-picker__progress') |
| 98 | + .as('progress') |
| 99 | + .should('be.visible') |
| 100 | + }) |
| 101 | + |
| 102 | + cy.wait('@chunks').then(() => { |
| 103 | + cy.get('[data-cy-upload-picker] .upload-picker__progress') |
| 104 | + .as('progress') |
| 105 | + .should('be.visible') |
| 106 | + cy.get('@progress') |
| 107 | + .children('progress') |
| 108 | + .should('not.have.value', '0') |
| 109 | + cy.get('[data-cy-upload-picker-progress-label]').should('not.contain', 'estimating time left') |
| 110 | + cy.get('[data-cy-upload-picker-progress-label]').should('not.contain', 'paused') |
| 111 | + |
| 112 | + cy.wait(1000).then(() => { |
| 113 | + getUploader().pause() |
| 114 | + }) |
| 115 | + |
| 116 | + cy.get('[data-cy-upload-picker-progress-label]').should('contain', 'paused') |
| 117 | + cy.get('@pausedListener').should('have.been.calledOnce') |
| 118 | + |
| 119 | + cy.wait(1000).then(() => { |
| 120 | + getUploader().start() |
| 121 | + }) |
| 122 | + |
| 123 | + cy.get('[data-cy-upload-picker-progress-label]').should('not.contain', 'paused') |
| 124 | + cy.get('@resumedListener').should('have.been.calledOnce') |
| 125 | + }) |
| 126 | + |
| 127 | + // Should will retry until success or timeout |
| 128 | + cy.get('@assemblyStart', { timeout: 30000 }).should('have.been.calledOnce').then(() => { |
| 129 | + cy.get('[data-cy-upload-picker] .upload-picker__progress') |
| 130 | + .as('progress') |
| 131 | + .should('be.visible') |
| 132 | + |
| 133 | + cy.get('[data-cy-upload-picker-progress-label]').should('not.contain', 'paused') |
| 134 | + cy.get('[data-cy-upload-picker-progress-label]').should('contain', 'assembling') |
| 135 | + }) |
| 136 | + |
| 137 | + cy.wait('@assemblyEnd', { timeout: 60000 }).then(() => { |
| 138 | + cy.get('[data-cy-upload-picker] .upload-picker__progress') |
| 139 | + .as('progress') |
| 140 | + .should('not.be.visible') |
| 141 | + }) |
| 142 | + }) |
| 143 | +}) |
0 commit comments