-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(ImageUploader): get complete upload data #6779
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3869c96
refactor: use Promise.allSettled
Layouwen c14473c
fix: console.error test
Layouwen 07a2cdd
refactor: use Promise.all
Layouwen b48998a
test: add test case
Layouwen 9175ac2
chore: adjust code order
Layouwen 3a07d54
chore: adjust code order
Layouwen 3d157eb
fix: only return successful data
Layouwen ff3fec0
test: verify the successfully uploaded file
Layouwen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import React, { createRef, forwardRef, useState } from 'react' | ||
import { | ||
act, | ||
cleanup, | ||
fireEvent, | ||
render, | ||
screen, | ||
sleep, | ||
testA11y, | ||
fireEvent, | ||
waitFor, | ||
userEvent, | ||
sleep, | ||
screen, | ||
cleanup, | ||
act, | ||
waitFor, | ||
waitForElementToBeRemoved, | ||
} from 'testing' | ||
import ImageUploader, { ImageUploadItem, ImageUploaderRef } from '..' | ||
|
@@ -65,19 +65,26 @@ describe('ImageUploader', () => { | |
}) | ||
|
||
const App = forwardRef<ImageUploaderRef, any>((props, ref) => { | ||
const [fileList, setFileList] = useState<ImageUploadItem[]>([ | ||
{ | ||
url: demoSrc, | ||
}, | ||
]) | ||
const { onChange: propsOnChange, defaultFileList, ...restProps } = props | ||
const [fileList, setFileList] = useState<ImageUploadItem[]>( | ||
defaultFileList || [ | ||
{ | ||
url: demoSrc, | ||
}, | ||
] | ||
) | ||
const onChange = (newFileList: ImageUploadItem[]) => { | ||
setFileList(newFileList) | ||
propsOnChange?.(newFileList) | ||
} | ||
|
||
return ( | ||
<ImageUploader | ||
ref={ref} | ||
value={fileList} | ||
onChange={setFileList} | ||
onChange={onChange} | ||
upload={mockUpload} | ||
{...props} | ||
{...restProps} | ||
/> | ||
) | ||
}) | ||
|
@@ -389,4 +396,48 @@ describe('ImageUploader', () => { | |
expect(ref.current).toBeDefined() | ||
expect(ref.current?.nativeElement).toBeDefined() | ||
}) | ||
|
||
test('get all upload url', async () => { | ||
function mockUploadWithFailure(failOnCount: number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 定义放最前面 |
||
let count = 0 | ||
return async (file: File) => { | ||
count++ | ||
if (count === failOnCount + 1) { | ||
throw new Error('Fail to upload') | ||
} | ||
return { | ||
url: URL.createObjectURL(file), | ||
extra: { | ||
fileName: file.name, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
const fn = jest.fn() | ||
const FAIL_INDEX = 1 | ||
const mockUpload = mockUploadWithFailure(FAIL_INDEX) | ||
|
||
render( | ||
<App multiple defaultFileList={[]} upload={mockUpload} onChange={fn} /> | ||
) | ||
|
||
const fileNameList = ['one.png', 'two.png', 'three.png'] | ||
|
||
mockInputFile( | ||
fileNameList.map(name => new File([name], name, { type: 'image/png' })) | ||
) | ||
|
||
await act(async () => { | ||
jest.runAllTimers() | ||
}) | ||
|
||
expect(fn.mock.lastCall[0].length).toBe(2) | ||
|
||
const successFileNames = fileNameList.filter((_, i) => i !== FAIL_INDEX) | ||
const mockInputSuccessFileNames = fn.mock.lastCall[0].map( | ||
(item: ImageUploadItem) => item.extra.fileName | ||
) | ||
expect(successFileNames).toEqual(mockInputSuccessFileNames) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
测试用例~~