tooltip.test.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. import { rAF } from '@element-plus/test-utils/tick'
  5. import { ElPopperTrigger } from '@element-plus/components/popper'
  6. import Tooltip from '../src/tooltip.vue'
  7. import type { VNode } from 'vue'
  8. vi.mock('@element-plus/utils/error', () => ({
  9. debugWarn: vi.fn(),
  10. }))
  11. const AXIOM = 'Rem is the best girl'
  12. describe('<ElTooltip />', () => {
  13. const createComponent = (props = {}, content: string | VNode = '') =>
  14. mount(
  15. <Tooltip
  16. {...props}
  17. v-slots={{
  18. default: () => AXIOM,
  19. content: () => content,
  20. }}
  21. />,
  22. {
  23. attachTo: document.body,
  24. }
  25. )
  26. let wrapper: ReturnType<typeof createComponent>
  27. const findTrigger = () => wrapper.findComponent(ElPopperTrigger)
  28. afterEach(() => {
  29. wrapper?.unmount()
  30. document.body.innerHTML = ''
  31. })
  32. describe('rendering', () => {
  33. it('should render correctly', async () => {
  34. wrapper = createComponent()
  35. await nextTick()
  36. expect(findTrigger().text()).toContain(AXIOM)
  37. })
  38. it('content should teleport according appendTo', async () => {
  39. const el = document.createElement('div')
  40. el.id = 'test'
  41. document.body.appendChild(el)
  42. wrapper = createComponent({ appendTo: '#test' }, 'test appendTo props')
  43. await nextTick()
  44. const trigger$ = findTrigger()
  45. const triggerEl = trigger$.find('.el-tooltip__trigger')
  46. await triggerEl.trigger('mouseenter')
  47. expect(document.querySelector('#test')?.innerHTML).toContain(
  48. 'test appendTo props'
  49. )
  50. })
  51. })
  52. describe('functionality', () => {
  53. const content = 'Test content'
  54. it('should be able to update popper content manually', async () => {
  55. wrapper = createComponent()
  56. await nextTick()
  57. const { vm } = wrapper
  58. expect(vm.updatePopper).toBeDefined()
  59. vm.updatePopper()
  60. })
  61. it('should be able to open & close tooltip content', async () => {
  62. wrapper = createComponent({}, content)
  63. await nextTick()
  64. const trigger$ = findTrigger()
  65. const triggerEl = trigger$.find('.el-tooltip__trigger')
  66. vi.useFakeTimers()
  67. await triggerEl.trigger('mouseenter')
  68. vi.runAllTimers()
  69. vi.useRealTimers()
  70. await rAF()
  71. expect(wrapper.emitted()).toHaveProperty('show')
  72. vi.useFakeTimers()
  73. await triggerEl.trigger('mouseleave') // dispatches a timer with 200ms timeout.
  74. vi.runAllTimers()
  75. vi.useRealTimers()
  76. await rAF()
  77. expect(wrapper.emitted()).toHaveProperty('hide')
  78. })
  79. it('should be able to toggle visibility of tooltip content', async () => {
  80. wrapper = createComponent(
  81. {
  82. trigger: 'click',
  83. },
  84. content
  85. )
  86. await nextTick()
  87. const trigger$ = findTrigger()
  88. const triggerEl = trigger$.find('.el-tooltip__trigger')
  89. vi.useFakeTimers()
  90. await triggerEl.trigger('click')
  91. vi.runAllTimers()
  92. vi.useRealTimers()
  93. await rAF()
  94. expect(wrapper.emitted()).toHaveProperty('show')
  95. vi.useFakeTimers()
  96. await triggerEl.trigger('click')
  97. vi.runAllTimers()
  98. vi.useRealTimers()
  99. await rAF()
  100. expect(wrapper.emitted()).toHaveProperty('hide')
  101. })
  102. })
  103. })