|
| 1 | +import Uppy, { type UppyEventMap } from '@uppy/core' |
| 2 | +import Dashboard from '@uppy/dashboard' |
| 3 | +import { page, userEvent } from '@vitest/browser/context' |
| 4 | +import { HttpResponse, http } from 'msw' |
| 5 | +import { afterEach, beforeEach, describe, expect } from 'vitest' |
| 6 | +import '@uppy/core/css/style.css' |
| 7 | +import '@uppy/dashboard/css/style.css' |
| 8 | +import XHRUpload from './index.js' |
| 9 | +import { test } from './test-extend.js' |
| 10 | + |
| 11 | +type UploadCompleteResult = Parameters<UppyEventMap<any, any>['complete']>[0] |
| 12 | + |
| 13 | +type MockUploadRequest = { |
| 14 | + fieldNames: string[] |
| 15 | + fileNames: string[] |
| 16 | +} |
| 17 | + |
| 18 | +let uppy: Uppy | undefined |
| 19 | + |
| 20 | +function createUppy({ bundle = false }: { bundle?: boolean } = {}) { |
| 21 | + const target = document.createElement('div') |
| 22 | + document.body.appendChild(target) |
| 23 | + |
| 24 | + uppy = new Uppy({ debug: true }) |
| 25 | + .use(Dashboard, { |
| 26 | + target, |
| 27 | + inline: true, |
| 28 | + }) |
| 29 | + .use(XHRUpload, { |
| 30 | + endpoint: 'http://localhost/upload', |
| 31 | + bundle, |
| 32 | + }) |
| 33 | + |
| 34 | + return uppy |
| 35 | +} |
| 36 | + |
| 37 | +function createMockFile(name: string, size: number = 16) { |
| 38 | + return new File(['a'.repeat(size)], name, { type: 'text/plain' }) |
| 39 | +} |
| 40 | + |
| 41 | +function waitForUploadComplete(uppy: Uppy<any, any>) { |
| 42 | + return new Promise<UploadCompleteResult>((resolve) => |
| 43 | + uppy.once('complete', resolve), |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +beforeEach(() => { |
| 48 | + document.body.innerHTML = '' |
| 49 | +}) |
| 50 | + |
| 51 | +afterEach(() => { |
| 52 | + uppy?.destroy() |
| 53 | + uppy = undefined |
| 54 | +}) |
| 55 | + |
| 56 | +describe('XHRUpload browser mode', () => { |
| 57 | + test('uploads a file in non-bundle mode', async ({ worker }) => { |
| 58 | + const requests: MockUploadRequest[] = [] |
| 59 | + worker.use( |
| 60 | + http.post('http://localhost/upload', async ({ request }) => { |
| 61 | + const formData = await request.formData() |
| 62 | + const uploadedFiles = Array.from(formData.entries()).filter( |
| 63 | + (entry): entry is [string, File] => entry[1] instanceof File, |
| 64 | + ) |
| 65 | + |
| 66 | + requests.push({ |
| 67 | + fieldNames: uploadedFiles.map(([fieldName]) => fieldName), |
| 68 | + fileNames: uploadedFiles.map(([, file]) => file.name), |
| 69 | + }) |
| 70 | + |
| 71 | + return HttpResponse.json({ url: 'http://localhost/uploads/regular' }) |
| 72 | + }), |
| 73 | + ) |
| 74 | + |
| 75 | + const uppy = createUppy() |
| 76 | + const completePromise = waitForUploadComplete(uppy) |
| 77 | + const fileInput = document.querySelector('.uppy-Dashboard-input')! |
| 78 | + |
| 79 | + await userEvent.upload(fileInput, createMockFile('regular.txt')) |
| 80 | + await page.getByRole('button', { name: 'Upload 1 file' }).click() |
| 81 | + |
| 82 | + const result = await completePromise |
| 83 | + |
| 84 | + expect(result.failed).toHaveLength(0) |
| 85 | + expect(result.successful).toHaveLength(1) |
| 86 | + expect(requests).toHaveLength(1) |
| 87 | + expect(requests[0]).toEqual({ |
| 88 | + fieldNames: ['file'], |
| 89 | + fileNames: ['regular.txt'], |
| 90 | + }) |
| 91 | + expect(uppy.getFiles()[0].response?.uploadURL).toBe( |
| 92 | + 'http://localhost/uploads/regular', |
| 93 | + ) |
| 94 | + await expect |
| 95 | + .element(page.getByText('Complete', { exact: true })) |
| 96 | + .toBeVisible() |
| 97 | + }) |
| 98 | + |
| 99 | + test('uploads files in a single request with bundle: true', async ({ |
| 100 | + worker, |
| 101 | + }) => { |
| 102 | + const requests: MockUploadRequest[] = [] |
| 103 | + worker.use( |
| 104 | + http.post('http://localhost/upload', async ({ request }) => { |
| 105 | + const formData = await request.formData() |
| 106 | + const uploadedFiles = Array.from(formData.entries()).filter( |
| 107 | + (entry): entry is [string, File] => entry[1] instanceof File, |
| 108 | + ) |
| 109 | + |
| 110 | + requests.push({ |
| 111 | + fieldNames: uploadedFiles.map(([fieldName]) => fieldName), |
| 112 | + fileNames: uploadedFiles.map(([, file]) => file.name), |
| 113 | + }) |
| 114 | + |
| 115 | + return HttpResponse.json({ url: 'http://localhost/uploads/bundled' }) |
| 116 | + }), |
| 117 | + ) |
| 118 | + |
| 119 | + const uppy = createUppy({ bundle: true }) |
| 120 | + const completePromise = waitForUploadComplete(uppy) |
| 121 | + const fileInput = document.querySelector('.uppy-Dashboard-input')! |
| 122 | + |
| 123 | + await userEvent.upload(fileInput, [ |
| 124 | + createMockFile('bundle-a.txt'), |
| 125 | + createMockFile('bundle-b.txt'), |
| 126 | + ]) |
| 127 | + await page.getByRole('button', { name: 'Upload 2 files' }).click() |
| 128 | + |
| 129 | + const result = await completePromise |
| 130 | + |
| 131 | + expect(result.failed).toHaveLength(0) |
| 132 | + expect(result.successful).toHaveLength(2) |
| 133 | + expect(requests).toHaveLength(1) |
| 134 | + const sortedFileNames = [...requests[0].fileNames].sort() |
| 135 | + expect(requests[0].fieldNames).toEqual(['files[]', 'files[]']) |
| 136 | + expect(sortedFileNames).toEqual(['bundle-a.txt', 'bundle-b.txt']) |
| 137 | + expect(uppy.getFiles().every((file) => file.response?.uploadURL)).toBe(true) |
| 138 | + await expect |
| 139 | + .element(page.getByText('Complete', { exact: true })) |
| 140 | + .toBeVisible() |
| 141 | + }) |
| 142 | +}) |
0 commit comments