-
-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9730dfe
commit 5d01b40
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { AxiosResponse } from 'axios' | ||
import { test, expect } from '../playwright.extend' | ||
|
||
declare namespace window { | ||
export let upload: () => Promise< | ||
AxiosResponse<{ message: string; content: string }> | ||
> | ||
export let progressEvents: Array<ProgressEvent> | ||
} | ||
|
||
test('responds with a mocked response to an upload request', async ({ | ||
loadExample, | ||
page, | ||
}) => { | ||
await loadExample(require.resolve('./axios-upload.runtime.js')) | ||
|
||
const uploadResult = await page.evaluate(() => { | ||
return window.upload().then((response) => response.data) | ||
}) | ||
|
||
expect(uploadResult).toEqual({ | ||
message: 'Successfully uploaded "doc.txt"!', | ||
content: 'Helloworld', | ||
}) | ||
|
||
const progressEvents = await page.evaluate(() => { | ||
return window.progressEvents | ||
}) | ||
|
||
expect(progressEvents.length).toBeGreaterThan(0) | ||
expect(progressEvents[0]).toMatchObject({ | ||
bytes: expect.any(Number), | ||
loaded: expect.any(Number), | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import axios from 'axios' | ||
import { http, HttpResponse } from 'msw' | ||
import { setupWorker } from 'msw/browser' | ||
|
||
const worker = setupWorker( | ||
http.post('/upload', async ({ request }) => { | ||
const data = await request.formData() | ||
const file = data.get('file') | ||
|
||
if (!file) { | ||
return new HttpResponse('Missing document upload', { status: 400 }) | ||
} | ||
|
||
if (!(file instanceof File)) { | ||
return new HttpResponse('Uploaded document is not a File', { | ||
status: 400, | ||
}) | ||
} | ||
|
||
return HttpResponse.json({ | ||
message: `Successfully uploaded "${file.name}"!`, | ||
content: await file.text(), | ||
}) | ||
}), | ||
) | ||
worker.start() | ||
|
||
const progressEvents = [] | ||
const request = axios.create({ | ||
baseURL: '/', | ||
onDownloadProgress(event) { | ||
progressEvents.push(event) | ||
}, | ||
}) | ||
|
||
window.progressEvents = progressEvents | ||
window.upload = async function () { | ||
const formData = new FormData() | ||
const file = new Blob(['Hello', 'world'], { type: 'text/plain' }) | ||
formData.set('file', file, 'doc.txt') | ||
|
||
return request.post('/upload', formData) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// @vitest-environment node | ||
import { File } from 'node:buffer' | ||
import axios from 'axios' | ||
import { http, HttpResponse } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
|
||
const server = setupServer( | ||
http.post('https://example.com/upload', async ({ request }) => { | ||
const data = await request.formData() | ||
const file = data.get('file') | ||
|
||
if (!file) { | ||
return new HttpResponse('Missing document upload', { status: 400 }) | ||
} | ||
|
||
if (!(file instanceof File)) { | ||
return new HttpResponse('Uploaded document is not a File', { | ||
status: 400, | ||
}) | ||
} | ||
|
||
return HttpResponse.json({ | ||
message: `Successfully uploaded "${file.name}"!`, | ||
content: await file.text(), | ||
}) | ||
}), | ||
) | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
it('responds with a mocked response to an upload request', async () => { | ||
const onUploadProgress = vi.fn() | ||
const request = axios.create({ | ||
baseURL: 'https://example.com', | ||
onUploadProgress, | ||
}) | ||
|
||
const formData = new FormData() | ||
const file = new Blob(['Hello', 'world'], { type: 'text/plain' }) | ||
formData.set('file', file, 'doc.txt') | ||
|
||
const response = await request.post('/upload', formData) | ||
|
||
expect(response.data).toEqual({ | ||
message: 'Successfully uploaded "doc.txt"!', | ||
content: 'Helloworld', | ||
}) | ||
expect(onUploadProgress.mock.calls.length).toBeGreaterThan(0) | ||
expect(onUploadProgress).toHaveBeenNthCalledWith( | ||
1, | ||
expect.objectContaining({ | ||
loaded: expect.any(Number), | ||
total: expect.any(Number), | ||
bytes: expect.any(Number), | ||
}), | ||
) | ||
}) |