2022-06-18 13:48:27 +08:00
|
|
|
import { nextTick, ref } from 'vue'
|
|
|
|
import { flushPromises, mount } from '@vue/test-utils'
|
|
|
|
import { afterEach, describe, expect, test, vi } from 'vitest'
|
2020-09-27 12:10:36 +08:00
|
|
|
|
2022-06-18 13:48:27 +08:00
|
|
|
import { EVENT_CODE } from '@element-plus/constants'
|
|
|
|
import Upload from '../src/upload.vue'
|
2022-03-05 23:09:31 +08:00
|
|
|
import UploadContent from '../src/upload-content.vue'
|
2020-09-27 12:10:36 +08:00
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
2022-06-18 13:48:27 +08:00
|
|
|
const mockGetFile = (element: HTMLInputElement, files: File[]) =>
|
|
|
|
vi.spyOn(element, 'files', 'get').mockImplementation((): any => files)
|
2020-09-27 12:10:36 +08:00
|
|
|
|
|
|
|
describe('<upload />', () => {
|
2022-06-18 13:48:27 +08:00
|
|
|
afterEach(() => {
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
})
|
|
|
|
|
2020-09-27 12:10:36 +08:00
|
|
|
describe('render test', () => {
|
|
|
|
test('basic rendering', async () => {
|
2022-06-18 13:48:27 +08:00
|
|
|
const drag = ref(false)
|
|
|
|
const wrapper = mount(() => <Upload drag={drag.value}>{AXIOM}</Upload>)
|
2020-09-27 12:10:36 +08:00
|
|
|
expect(wrapper.text()).toEqual(AXIOM)
|
|
|
|
|
2022-06-18 13:48:27 +08:00
|
|
|
drag.value = true
|
|
|
|
await nextTick()
|
2020-09-27 12:10:36 +08:00
|
|
|
expect(wrapper.find('.el-upload-dragger').exists()).toBe(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('functionality', () => {
|
|
|
|
test('works with keydown & click', async () => {
|
2022-06-18 13:48:27 +08:00
|
|
|
const wrapper = mount(() => <UploadContent />)
|
2020-09-27 12:10:36 +08:00
|
|
|
|
2022-06-18 13:48:27 +08:00
|
|
|
const onClick = vi.fn()
|
|
|
|
wrapper.find('input').element.addEventListener('click', onClick)
|
2020-09-27 12:10:36 +08:00
|
|
|
|
|
|
|
await wrapper.trigger('click')
|
2022-06-18 13:48:27 +08:00
|
|
|
expect(onClick).toHaveBeenCalled()
|
|
|
|
|
2020-09-27 12:10:36 +08:00
|
|
|
await wrapper.trigger('keydown', {
|
|
|
|
key: EVENT_CODE.enter,
|
|
|
|
})
|
2022-06-18 13:48:27 +08:00
|
|
|
expect(onClick).toHaveBeenCalledTimes(2)
|
2020-09-27 12:10:36 +08:00
|
|
|
|
|
|
|
await wrapper.trigger('keydown', {
|
|
|
|
key: EVENT_CODE.space,
|
|
|
|
})
|
2022-06-18 13:48:27 +08:00
|
|
|
expect(onClick).toHaveBeenCalledTimes(3)
|
2020-09-27 12:10:36 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('works when upload file exceeds the limit', async () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
const onExceed = vi.fn()
|
2022-06-18 13:48:27 +08:00
|
|
|
const wrapper = mount(() => (
|
|
|
|
<UploadContent onExceed={onExceed} limit={1} />
|
|
|
|
))
|
2020-09-27 12:10:36 +08:00
|
|
|
const fileList = [
|
|
|
|
new File(['content'], 'test-file.txt'),
|
|
|
|
new File(['content'], 'test-file.txt'),
|
|
|
|
]
|
|
|
|
mockGetFile(wrapper.find('input').element, fileList)
|
|
|
|
|
|
|
|
await wrapper.find('input').trigger('change')
|
|
|
|
expect(onExceed).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('onStart works', async () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
const onStart = vi.fn()
|
2022-06-18 13:48:27 +08:00
|
|
|
const wrapper = mount(() => (
|
|
|
|
<UploadContent onStart={onStart} autoUpload={false} />
|
|
|
|
))
|
2020-09-27 12:10:36 +08:00
|
|
|
|
2022-06-18 13:48:27 +08:00
|
|
|
const fileList = [new File(['content'], 'test-file.txt')]
|
2020-09-27 12:10:36 +08:00
|
|
|
mockGetFile(wrapper.find('input').element, fileList)
|
|
|
|
await wrapper.find('input').trigger('change')
|
|
|
|
|
|
|
|
expect(onStart).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('beforeUpload works for rejecting upload', async () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
const beforeUpload = vi.fn(() => Promise.reject())
|
|
|
|
const onRemove = vi.fn()
|
2022-06-18 13:48:27 +08:00
|
|
|
const wrapper = mount(() => (
|
|
|
|
<UploadContent beforeUpload={beforeUpload} onRemove={onRemove} />
|
|
|
|
))
|
2020-09-27 12:10:36 +08:00
|
|
|
const fileList = [new File(['content'], 'test-file.txt')]
|
|
|
|
mockGetFile(wrapper.find('input').element, fileList)
|
2022-06-18 13:48:27 +08:00
|
|
|
|
2020-09-27 12:10:36 +08:00
|
|
|
await wrapper.find('input').trigger('change')
|
|
|
|
|
|
|
|
expect(beforeUpload).toHaveBeenCalled()
|
|
|
|
await nextTick()
|
|
|
|
expect(onRemove).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('beforeUpload works for resolving upload', async () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
const beforeUpload = vi.fn(() => Promise.resolve())
|
2022-06-18 13:48:27 +08:00
|
|
|
const httpRequest = ref(vi.fn(() => Promise.resolve()))
|
2022-04-19 12:46:57 +08:00
|
|
|
const onSuccess = vi.fn()
|
2022-06-18 13:48:27 +08:00
|
|
|
const onError = vi.fn()
|
2022-04-19 12:46:57 +08:00
|
|
|
|
2022-06-18 13:48:27 +08:00
|
|
|
const wrapper = mount(() => (
|
|
|
|
<UploadContent
|
|
|
|
beforeUpload={beforeUpload}
|
|
|
|
httpRequest={httpRequest.value}
|
|
|
|
onSuccess={onSuccess}
|
|
|
|
onError={onError}
|
|
|
|
/>
|
|
|
|
))
|
2020-09-27 12:10:36 +08:00
|
|
|
|
2022-06-18 13:48:27 +08:00
|
|
|
const fileList = [new File(['content'], 'test-file.txt')]
|
2020-09-27 12:10:36 +08:00
|
|
|
mockGetFile(wrapper.find('input').element, fileList)
|
2022-06-18 13:48:27 +08:00
|
|
|
|
2020-09-27 12:10:36 +08:00
|
|
|
await wrapper.find('input').trigger('change')
|
|
|
|
|
|
|
|
expect(beforeUpload).toHaveBeenCalled()
|
2022-04-19 12:46:57 +08:00
|
|
|
await flushPromises()
|
2020-09-27 12:10:36 +08:00
|
|
|
expect(onSuccess).toHaveBeenCalled()
|
2022-06-18 13:48:27 +08:00
|
|
|
expect(onError).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
vi.clearAllMocks()
|
|
|
|
|
|
|
|
httpRequest.value = vi.fn(() => Promise.reject())
|
|
|
|
await nextTick()
|
2020-09-27 12:10:36 +08:00
|
|
|
|
|
|
|
await wrapper.find('input').trigger('change')
|
2022-06-18 13:48:27 +08:00
|
|
|
|
|
|
|
expect(beforeUpload).toHaveBeenCalled()
|
2022-04-19 12:46:57 +08:00
|
|
|
await flushPromises()
|
2022-06-18 13:48:27 +08:00
|
|
|
expect(onSuccess).not.toHaveBeenCalled()
|
2020-09-27 12:10:36 +08:00
|
|
|
expect(onError).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('onProgress should work', async () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
const onProgress = vi.fn()
|
|
|
|
const httpRequest = vi.fn(({ onProgress }) => {
|
2020-09-27 12:10:36 +08:00
|
|
|
onProgress()
|
|
|
|
return Promise.resolve()
|
|
|
|
})
|
2022-06-18 13:48:27 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<UploadContent httpRequest={httpRequest} onProgress={onProgress} />
|
|
|
|
)
|
2020-09-27 12:10:36 +08:00
|
|
|
|
2022-06-18 13:48:27 +08:00
|
|
|
const fileList = [new File(['content'], 'test-file.txt')]
|
2020-09-27 12:10:36 +08:00
|
|
|
mockGetFile(wrapper.find('input').element, fileList)
|
2022-06-18 13:48:27 +08:00
|
|
|
|
2020-09-27 12:10:36 +08:00
|
|
|
await wrapper.find('input').trigger('change')
|
|
|
|
await nextTick()
|
|
|
|
expect(onProgress).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|