trigger.test.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { nextTick, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. import { POPPER_INJECTION_KEY } from '@element-plus/components/popper'
  5. import ElTrigger from '../src/trigger.vue'
  6. import type { VueWrapper } from '@vue/test-utils'
  7. import type { PopperTriggerInstance } from '../src/trigger'
  8. const AXIOM = 'rem is the best girl'
  9. const popperInjection = {
  10. triggerRef: ref(null),
  11. popperInstanceRef: ref(null),
  12. contentRef: ref(null),
  13. }
  14. const mountTrigger = (props = {}) =>
  15. mount(<ElTrigger {...props}>{AXIOM}</ElTrigger>, {
  16. global: {
  17. provide: {
  18. [POPPER_INJECTION_KEY as symbol]: popperInjection,
  19. },
  20. },
  21. })
  22. describe('<ElPopperTrigger />', () => {
  23. let wrapper: VueWrapper<PopperTriggerInstance>
  24. afterEach(() => {
  25. popperInjection.triggerRef.value = null
  26. })
  27. describe('rendering', () => {
  28. it('should be able to render with children', async () => {
  29. wrapper = mountTrigger()
  30. await nextTick()
  31. expect(popperInjection.triggerRef.value).not.toBeNull()
  32. expect(wrapper.text()).toEqual(AXIOM)
  33. })
  34. it('should be able to render for virtual ref', async () => {
  35. wrapper = mountTrigger({
  36. virtualTriggering: true,
  37. })
  38. await nextTick()
  39. expect(wrapper.text()).not.toEqual(AXIOM)
  40. const virtualRef = document.createElement('div')
  41. await wrapper.setProps({
  42. virtualRef,
  43. })
  44. expect(popperInjection.triggerRef.value).toStrictEqual
  45. })
  46. })
  47. describe('can attach handlers', () => {
  48. it('should be able to attach handlers to the trigger', async () => {
  49. const onClick = vi.fn()
  50. const virtualRef = document.createElement('div')
  51. wrapper = mountTrigger({
  52. onClick,
  53. virtualTriggering: true,
  54. virtualRef,
  55. })
  56. await nextTick()
  57. expect(onClick).not.toHaveBeenCalled()
  58. const evt = new MouseEvent('click')
  59. virtualRef.dispatchEvent(evt)
  60. await nextTick()
  61. expect(onClick).toHaveBeenCalled()
  62. })
  63. })
  64. })