directive.test.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { nextTick, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, it } from 'vitest'
  4. import { rAF } from '@element-plus/test-utils/tick'
  5. import Popover from '../src/popover.vue'
  6. import PopoverDirective, { VPopover } from '../src/directive'
  7. import type { PopoverInstance } from '../src/popover'
  8. const AXIOM = 'Rem is the best boy'
  9. const _mount = () => {
  10. const popoverRef = ref<PopoverInstance>()
  11. return mount(
  12. () => (
  13. <>
  14. <Popover
  15. ref={popoverRef}
  16. title="title"
  17. content={AXIOM}
  18. virtual-triggering
  19. trigger="click"
  20. />
  21. <div v-popover={popoverRef.value} id="reference-node">
  22. trigger
  23. </div>
  24. </>
  25. ),
  26. {
  27. global: {
  28. directives: {
  29. [VPopover]: PopoverDirective,
  30. },
  31. },
  32. }
  33. )
  34. }
  35. describe('v-popover', () => {
  36. afterEach(() => {
  37. document.body.innerHTML = ''
  38. })
  39. it('should render correctly', async () => {
  40. const wrapper = _mount()
  41. await nextTick()
  42. expect(document.body.querySelector('.el-popover')?.innerHTML).toContain(
  43. AXIOM
  44. )
  45. wrapper.unmount()
  46. })
  47. it('should show popover when reference is mounted', async () => {
  48. const wrapper = _mount()
  49. await nextTick()
  50. const refNode = '#reference-node'
  51. expect(wrapper.find(refNode).exists()).toBe(true)
  52. expect(
  53. document.body.querySelector('.el-popover')?.getAttribute('style')
  54. ).toContain('display: none')
  55. await wrapper.find(refNode).trigger('click', {
  56. button: 0,
  57. })
  58. await nextTick()
  59. await rAF()
  60. expect(
  61. document.body.querySelector('.el-popover')?.getAttribute('style')
  62. ).not.toContain('display: none')
  63. wrapper.unmount()
  64. })
  65. })