Skip to content

Commit e049b0e

Browse files
authored
test(uploader): optimize async test cases stability (#3104)
1 parent 2cf72cc commit e049b0e

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/packages/uploader/__tests__/uploader.spec.tsx

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { render, fireEvent } from '@testing-library/react'
33
import '@testing-library/jest-dom'
44

55
import { Uploader } from '../uploader'
6-
import { FileItem } from '../types'
6+
import { UploaderFileItem as FileItem } from '@/types/spec/uploader/base'
77
import { Preview } from '../preview'
88
import Button from '@/packages/button'
99

@@ -221,15 +221,17 @@ test('should render progress', () => {
221221
const textElement = container.querySelector('span')
222222
expect(textElement).toHaveTextContent('文件444.png')
223223
})
224-
test('simulates single file upload', () => {
224+
test('simulates single file upload', async () => {
225225
const handleUpload: any = vi.fn()
226226
const { container } = render(
227227
<Uploader upload={(file: File) => handleUpload(file)} />
228228
)
229-
const file = new File(['hello'], 'hello.png', { type: 'image/png' })
229+
const file = new File([new ArrayBuffer(10000)], 'hello.png', {
230+
type: 'image/png',
231+
})
230232
const input: any = container.querySelector('input')
231233

232-
fireEvent.change(input, { target: { files: [file] } })
234+
await fireEvent.change(input, { target: { files: [file] } })
233235

234236
expect(handleUpload).toHaveBeenCalledTimes(1)
235237
expect(handleUpload).toHaveBeenCalledWith(file)
@@ -241,15 +243,18 @@ test('simulates single file upload fail', async () => {
241243
const { container } = render(
242244
<Uploader upload={(file: File) => handleUpload(file)} />
243245
)
244-
const file = new File(['hello'], 'hello.png', { type: 'image/png' })
246+
const file = new File([new ArrayBuffer(10000)], 'hello.png', {
247+
type: 'image/png',
248+
})
245249
const input: any = container.querySelector('input')
246250

247-
fireEvent.change(input, { target: { files: [file] } })
251+
await fireEvent.change(input, { target: { files: [file] } })
248252

249253
expect(handleUpload).toHaveBeenCalledTimes(1)
250254
expect(handleUpload).toHaveBeenCalledWith(file)
255+
await expect(handleUpload()).rejects.toThrow('Upload failed')
251256
})
252-
test('simulates multiple file upload', () => {
257+
test('simulates multiple file upload', async () => {
253258
const handleUpload: any = vi.fn()
254259
const handleOverCount: any = vi.fn()
255260
const { container } = render(
@@ -260,19 +265,25 @@ test('simulates multiple file upload', () => {
260265
onOverCount={handleOverCount}
261266
/>
262267
)
263-
const file1 = new File(['file1'], 'file1.txt', { type: 'text/plain' })
264-
const file2 = new File(['file2'], 'file2.txt', { type: 'text/plain' })
265-
const file3 = new File(['file3'], 'file3.txt', { type: 'text/plain' })
268+
const file1 = new File([new ArrayBuffer(10000)], 'file1.txt', {
269+
type: 'text/plain',
270+
})
271+
const file2 = new File([new ArrayBuffer(10000)], 'file2.txt', {
272+
type: 'text/plain',
273+
})
274+
const file3 = new File([new ArrayBuffer(10000)], 'file3.txt', {
275+
type: 'text/plain',
276+
})
266277
const files = [file1, file2, file3]
267278
const input: any = container.querySelector('input')
268279

269-
fireEvent.change(input, { target: { files } })
280+
await fireEvent.change(input, { target: { files } })
270281

271282
expect(handleUpload).toHaveBeenCalledTimes(2)
272283
expect(handleOverCount).toHaveBeenCalledTimes(1)
273284
expect(handleOverCount).toHaveBeenCalledWith(3)
274285
})
275-
test('simulates file upload when autoupload is false', () => {
286+
test('simulates file upload when autoupload is false', async () => {
276287
const handleUpload: any = vi.fn()
277288
const handleOverCount: any = vi.fn()
278289
const { container } = render(
@@ -284,13 +295,21 @@ test('simulates file upload when autoupload is false', () => {
284295
onOverCount={handleOverCount}
285296
/>
286297
)
287-
const file1 = new File(['file1'], 'file1.txt', { type: 'text/plain' })
288-
const file2 = new File(['file2'], 'file2.txt', { type: 'text/plain' })
289-
const file3 = new File(['file3'], 'file3.txt', { type: 'text/plain' })
298+
const file1 = new File([new ArrayBuffer(10000)], 'file1.txt', {
299+
type: 'text/plain',
300+
})
301+
const file2 = new File([new ArrayBuffer(10000)], 'file2.txt', {
302+
type: 'text/plain',
303+
})
304+
const file3 = new File([new ArrayBuffer(10000)], 'file3.txt', {
305+
type: 'text/plain',
306+
})
290307
const files = [file1, file2, file3]
291308
const input: any = container.querySelector('input')
292-
fireEvent.change(input, { target: { files } })
309+
await fireEvent.change(input, { target: { files } })
293310
expect(handleUpload).toHaveBeenCalledTimes(0)
311+
expect(handleOverCount).toHaveBeenCalledTimes(1)
312+
expect(handleOverCount).toHaveBeenCalledWith(3)
294313
})
295314
test('should render button', () => {
296315
const clearUpload = vi.fn()

0 commit comments

Comments
 (0)