upload.test.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { nextTick, ref } from 'vue'
  2. import { flushPromises, mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, test, vi } from 'vitest'
  4. import { EVENT_CODE } from '@element-plus/constants'
  5. import Upload from '../src/upload.vue'
  6. import UploadContent from '../src/upload-content.vue'
  7. const AXIOM = 'Rem is the best girl'
  8. const mockGetFile = (element: HTMLInputElement, files: File[]) =>
  9. vi.spyOn(element, 'files', 'get').mockImplementation((): any => files)
  10. describe('<upload />', () => {
  11. afterEach(() => {
  12. vi.restoreAllMocks()
  13. })
  14. describe('render test', () => {
  15. test('basic rendering', async () => {
  16. const drag = ref(false)
  17. const wrapper = mount(() => <Upload drag={drag.value}>{AXIOM}</Upload>)
  18. expect(wrapper.text()).toEqual(AXIOM)
  19. drag.value = true
  20. await nextTick()
  21. expect(wrapper.find('.el-upload-dragger').exists()).toBe(true)
  22. })
  23. })
  24. describe('functionality', () => {
  25. test('works with keydown & click', async () => {
  26. const wrapper = mount(() => <UploadContent />)
  27. const onClick = vi.fn()
  28. wrapper.find('input').element.addEventListener('click', onClick)
  29. await wrapper.trigger('click')
  30. expect(onClick).toHaveBeenCalled()
  31. await wrapper.trigger('keydown', {
  32. key: EVENT_CODE.enter,
  33. })
  34. expect(onClick).toHaveBeenCalledTimes(2)
  35. await wrapper.trigger('keydown', {
  36. key: EVENT_CODE.space,
  37. })
  38. expect(onClick).toHaveBeenCalledTimes(3)
  39. })
  40. test('works when upload file exceeds the limit', async () => {
  41. const onExceed = vi.fn()
  42. const wrapper = mount(() => (
  43. <UploadContent onExceed={onExceed} limit={1} />
  44. ))
  45. const fileList = [
  46. new File(['content'], 'test-file.txt'),
  47. new File(['content'], 'test-file.txt'),
  48. ]
  49. mockGetFile(wrapper.find('input').element, fileList)
  50. await wrapper.find('input').trigger('change')
  51. expect(onExceed).toHaveBeenCalled()
  52. })
  53. test('onStart works', async () => {
  54. const onStart = vi.fn()
  55. const wrapper = mount(() => (
  56. <UploadContent onStart={onStart} autoUpload={false} />
  57. ))
  58. const fileList = [new File(['content'], 'test-file.txt')]
  59. mockGetFile(wrapper.find('input').element, fileList)
  60. await wrapper.find('input').trigger('change')
  61. expect(onStart).toHaveBeenCalled()
  62. })
  63. test('beforeUpload works for rejecting upload', async () => {
  64. const beforeUpload = vi.fn(() => Promise.reject())
  65. const onRemove = vi.fn()
  66. const wrapper = mount(() => (
  67. <UploadContent beforeUpload={beforeUpload} onRemove={onRemove} />
  68. ))
  69. const fileList = [new File(['content'], 'test-file.txt')]
  70. mockGetFile(wrapper.find('input').element, fileList)
  71. await wrapper.find('input').trigger('change')
  72. expect(beforeUpload).toHaveBeenCalled()
  73. await nextTick()
  74. expect(onRemove).toHaveBeenCalled()
  75. })
  76. test('beforeUpload works for resolving upload', async () => {
  77. const beforeUpload = vi.fn(() => Promise.resolve())
  78. const httpRequest = ref(vi.fn(() => Promise.resolve()))
  79. const onSuccess = vi.fn()
  80. const onError = vi.fn()
  81. const wrapper = mount(() => (
  82. <UploadContent
  83. beforeUpload={beforeUpload}
  84. httpRequest={httpRequest.value}
  85. onSuccess={onSuccess}
  86. onError={onError}
  87. />
  88. ))
  89. const fileList = [new File(['content'], 'test-file.txt')]
  90. mockGetFile(wrapper.find('input').element, fileList)
  91. await wrapper.find('input').trigger('change')
  92. expect(beforeUpload).toHaveBeenCalled()
  93. await flushPromises()
  94. expect(onSuccess).toHaveBeenCalled()
  95. expect(onError).not.toHaveBeenCalled()
  96. vi.clearAllMocks()
  97. httpRequest.value = vi.fn(() => Promise.reject())
  98. await nextTick()
  99. await wrapper.find('input').trigger('change')
  100. expect(beforeUpload).toHaveBeenCalled()
  101. await flushPromises()
  102. expect(onSuccess).not.toHaveBeenCalled()
  103. expect(onError).toHaveBeenCalled()
  104. })
  105. test('onProgress should work', async () => {
  106. const onProgress = vi.fn()
  107. const httpRequest = vi.fn(({ onProgress }) => {
  108. onProgress()
  109. return Promise.resolve()
  110. })
  111. const wrapper = mount(
  112. <UploadContent httpRequest={httpRequest} onProgress={onProgress} />
  113. )
  114. const fileList = [new File(['content'], 'test-file.txt')]
  115. mockGetFile(wrapper.find('input').element, fileList)
  116. await wrapper.find('input').trigger('change')
  117. await nextTick()
  118. expect(onProgress).toHaveBeenCalled()
  119. })
  120. })
  121. })