upload-dragger.test.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { computed, provide } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { describe, expect, test, vi } from 'vitest'
  4. import { uploadContextKey } from '@element-plus/components/upload'
  5. import UploadDragger from '../src/upload-dragger.vue'
  6. const AXIOM = 'Rem is the best girl'
  7. const _mount = (props = {}) =>
  8. mount({
  9. setup() {
  10. provide(uploadContextKey, { accept: computed(() => 'video/*') })
  11. return () => <UploadDragger {...props}>{AXIOM}</UploadDragger>
  12. },
  13. })
  14. describe('<upload-dragger />', () => {
  15. describe('render test', () => {
  16. test('should render correct', () => {
  17. const wrapper = _mount()
  18. expect(wrapper.text()).toBe(AXIOM)
  19. })
  20. })
  21. describe('functionality', () => {
  22. test('onDrag works', async () => {
  23. const wrapper = _mount()
  24. await wrapper.find('.el-upload-dragger').trigger('dragover')
  25. expect(wrapper.classes('is-dragover')).toBe(true)
  26. })
  27. test('ondrop works for any given video type', async () => {
  28. const onDrop = vi.fn()
  29. const wrapper = _mount({ onDrop })
  30. const dragger = wrapper.findComponent(UploadDragger)
  31. await dragger.trigger('drop', {
  32. dataTransfer: {
  33. files: [
  34. {
  35. type: 'video/mp4',
  36. name: 'test.mp4',
  37. },
  38. ],
  39. },
  40. })
  41. expect(onDrop).toHaveBeenCalledTimes(1)
  42. expect(dragger.emitted('file')).toHaveLength(1)
  43. await dragger.trigger('drop', {
  44. dataTransfer: {
  45. files: [
  46. {
  47. type: 'video/mov',
  48. name: 'test.mov',
  49. },
  50. ],
  51. },
  52. })
  53. expect(dragger.emitted('file')).toHaveLength(2)
  54. })
  55. })
  56. })