arrow.test.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { nextTick, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  4. import { POPPER_CONTENT_INJECTION_KEY } from '@element-plus/components/popper'
  5. import ElArrow from '../src/arrow.vue'
  6. import type { VueWrapper } from '@vue/test-utils'
  7. import type { PopperArrowInstance } from '../src/arrow'
  8. const popperContentInjection = {
  9. arrowRef: ref(null),
  10. arrowOffset: ref(0),
  11. }
  12. const mountArrow = () =>
  13. mount(<ElArrow />, {
  14. global: {
  15. provide: {
  16. [POPPER_CONTENT_INJECTION_KEY as symbol]: popperContentInjection,
  17. },
  18. },
  19. })
  20. describe('<ElPopperArrow />', () => {
  21. let wrapper: VueWrapper<PopperArrowInstance>
  22. beforeEach(() => {
  23. wrapper = mountArrow()
  24. return nextTick()
  25. })
  26. afterEach(() => {
  27. wrapper?.unmount()
  28. popperContentInjection.arrowRef.value = null
  29. popperContentInjection.arrowOffset.value = 0
  30. })
  31. it('should set the arrowRef after mounted', async () => {
  32. expect(popperContentInjection.arrowRef.value).toBe(wrapper.vm.arrowRef)
  33. expect(popperContentInjection.arrowOffset.value).toBe(0)
  34. })
  35. it('should update the offset after props changed', async () => {
  36. expect(popperContentInjection.arrowOffset.value).toBe(0)
  37. await wrapper.setProps({
  38. arrowOffset: 10,
  39. })
  40. expect(popperContentInjection.arrowOffset.value).toBe(10)
  41. })
  42. it('should unset arrowRef before unmount', async () => {
  43. expect(popperContentInjection.arrowRef.value).toBe(wrapper.vm.arrowRef)
  44. wrapper.unmount()
  45. expect(popperContentInjection.arrowRef.value).toBeNull()
  46. })
  47. })