use-popper-container.test.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { defineComponent, nextTick } from 'vue'
  2. import { config, mount, shallowMount } from '@vue/test-utils'
  3. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  4. import * as vueuse from '@vueuse/core'
  5. import {
  6. usePopperContainer,
  7. usePopperContainerId,
  8. } from '../use-popper-container'
  9. import { ID_INJECTION_KEY } from '../use-id'
  10. const AXIOM = 'rem is the best girl'
  11. vi.mock('@vueuse/core', () => {
  12. return {
  13. isClient: true,
  14. }
  15. })
  16. const mountComponent = () =>
  17. shallowMount(
  18. defineComponent({
  19. setup(_, { expose }) {
  20. const exposes = usePopperContainer()
  21. expose(exposes)
  22. return () => <div>{AXIOM}</div>
  23. },
  24. })
  25. )
  26. describe('usePopperContainer', () => {
  27. afterEach(() => {
  28. document.body.innerHTML = ''
  29. })
  30. it('should append container to the DOM root', async () => {
  31. const { vm } = mountComponent()
  32. await nextTick()
  33. const { selector } = vm
  34. expect(document.body.querySelector(selector.value)).toBeDefined()
  35. })
  36. it('should not append container to the DOM root', async () => {
  37. ;(vueuse as any).isClient = false
  38. const { vm } = mountComponent()
  39. await nextTick()
  40. const { selector } = vm
  41. expect(document.body.querySelector(selector.value)).toBeNull()
  42. })
  43. })
  44. describe('no injection value', () => {
  45. afterEach(() => {
  46. document.body.innerHTML = ''
  47. })
  48. it('usePopperContainerId', () => {
  49. const wrapper = mount({
  50. setup() {
  51. const data = usePopperContainerId()
  52. return data
  53. },
  54. })
  55. expect(wrapper.vm.id).toMatch(/^el-popper-container-\d{0,4}$/)
  56. expect(wrapper.vm.selector).toMatch(/^#el-popper-container-\d{0,4}$/)
  57. expect(wrapper.vm.selector).toBe(`#${wrapper.vm.id}`)
  58. })
  59. })
  60. describe('with injection value', () => {
  61. beforeEach(() => {
  62. config.global.provide = {
  63. [ID_INJECTION_KEY as symbol]: {
  64. prefix: 1024,
  65. current: 0,
  66. },
  67. }
  68. })
  69. afterEach(() => {
  70. document.body.innerHTML = ''
  71. config.global.provide = {}
  72. })
  73. it('usePopperContainerId', () => {
  74. const wrapper = mount({
  75. setup() {
  76. const data = usePopperContainerId()
  77. return data
  78. },
  79. })
  80. expect(wrapper.vm.id).toBe('el-popper-container-1024')
  81. expect(wrapper.vm.selector).toBe('#el-popper-container-1024')
  82. })
  83. })