Skip to content
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 8 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/components/image-uploader/image-uploader.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import React, { forwardRef, useRef, useState, useImperativeHandle } from 'react'
import { useIsomorphicLayoutEffect, useSize, useUnmount } from 'ahooks'
import { AddOutline, CloseOutline } from 'antd-mobile-icons'
import type {
ReactNode,
InputHTMLAttributes,
CSSProperties,
InputHTMLAttributes,
ReactElement,
ReactNode,
} from 'react'
import { AddOutline, CloseOutline } from 'antd-mobile-icons'
import { mergeProps } from '../../utils/with-default-props'
import ImageViewer, { ImageViewerShowHandler } from '../image-viewer'
import PreviewItem from './preview-item'
import { usePropsValue } from '../../utils/use-props-value'
import { useIsomorphicLayoutEffect, useUnmount, useSize } from 'ahooks'
import Space from '../space'
import { NativeProps, withNativeProps } from '../../utils/native-props'
import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react'
import { measureCSSLength } from '../../utils/measure-css-length'
import { NativeProps, withNativeProps } from '../../utils/native-props'
import { usePropsValue } from '../../utils/use-props-value'
import { mergeProps } from '../../utils/with-default-props'
import { useConfig } from '../config-provider'
import type { ImageProps } from '../image'
import Grid, { GridProps } from '../grid'
import type { ImageProps } from '../image'
import ImageViewer, { ImageViewerShowHandler } from '../image-viewer'
import Space from '../space'
import PreviewItem from './preview-item'

export type TaskStatus = 'pending' | 'fail' | 'success'

Expand Down Expand Up @@ -225,10 +225,10 @@ export const ImageUploader = forwardRef<ImageUploaderRef, ImageUploaderProps>(
return task
})
})
throw e
console.error(e)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试用例~~

})
).catch(error => console.error(error))
)
setValue(prev => prev.concat(newVal))
}

Expand Down
53 changes: 45 additions & 8 deletions src/components/image-uploader/tests/image-uploader.test.tsx
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 '..'
Expand Down Expand Up @@ -65,19 +65,24 @@ describe('ImageUploader', () => {
})

const App = forwardRef<ImageUploaderRef, any>((props, ref) => {
const { onChange: propsOnChange, ...restProps } = props
const [fileList, setFileList] = useState<ImageUploadItem[]>([
{
url: demoSrc,
},
])
const onChange = (newFileList: ImageUploadItem[]) => {
setFileList(newFileList)
propsOnChange?.(newFileList)
}

return (
<ImageUploader
ref={ref}
value={fileList}
onChange={setFileList}
onChange={onChange}
upload={mockUpload}
{...props}
{...restProps}
/>
)
})
Expand Down Expand Up @@ -389,4 +394,36 @@ describe('ImageUploader', () => {
expect(ref.current).toBeDefined()
expect(ref.current?.nativeElement).toBeDefined()
})

test('get all upload url', async () => {
const fn = jest.fn()
const mockUpload = mockUploadWithFailure(2)

render(<App multiple upload={mockUpload} onChange={fn} />)

mockInputFile([
new File(['one'], 'one.png', { type: 'image/png' }),
new File(['two'], 'two.png', { type: 'image/png' }),
new File(['three'], 'three.png', { type: 'image/png' }),
])

await act(async () => {
jest.runAllTimers()
})

expect(fn.mock.lastCall[0].length).toBe(4)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

失败的 2 个要去掉,否则实际上传和 UI 会对不上


function mockUploadWithFailure(failOnCount: number) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

定义放最前面

let count = 0
return async () => {
count++
if (count === failOnCount) {
throw new Error('Fail to upload')
}
return {
url: 'count: ' + count,
}
}
}
})
})
Loading