directive.test.ts 1.7 KB

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