use-teleport.test.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { defineComponent, h, nextTick, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  4. import { useTeleport } from '../use-teleport'
  5. import type { VueWrapper } from '@vue/test-utils'
  6. const AXIOM = 'Rem is the best girl'
  7. const Comp = defineComponent({
  8. setup() {
  9. const appendToBody = ref(true)
  10. const { showTeleport, hideTeleport, renderTeleport } = useTeleport(
  11. () => h('div', AXIOM),
  12. appendToBody
  13. )
  14. return () => (
  15. <>
  16. <button class="show" onClick={showTeleport}>
  17. show
  18. </button>
  19. <button class="hide" onClick={hideTeleport}>
  20. hide
  21. </button>
  22. <button
  23. class="toggle"
  24. onClick={() => (appendToBody.value = !appendToBody.value)}
  25. >
  26. toggle
  27. </button>
  28. {renderTeleport()}
  29. </>
  30. )
  31. },
  32. })
  33. describe('useModal', () => {
  34. let wrapper: VueWrapper<InstanceType<typeof Comp>>
  35. beforeEach(() => {
  36. wrapper = mount(Comp)
  37. })
  38. afterEach(() => {
  39. wrapper.unmount()
  40. })
  41. it('should render correctly', async () => {
  42. await nextTick()
  43. expect(wrapper.text()).not.toContain(AXIOM)
  44. })
  45. it('should render teleport to the DOM', async () => {
  46. await nextTick()
  47. expect(wrapper.text()).not.toContain(AXIOM)
  48. await wrapper.find('.show').trigger('click')
  49. expect(document.body.innerHTML).toContain(AXIOM)
  50. await wrapper.find('.hide').trigger('click')
  51. expect(document.body.innerHTML).not.toContain(AXIOM)
  52. })
  53. it('should render to the current wrapper instead of document.body', async () => {
  54. await nextTick()
  55. expect(wrapper.text()).not.toContain(AXIOM)
  56. await wrapper.find('.toggle').trigger('click')
  57. expect(wrapper.text()).toContain(AXIOM)
  58. })
  59. })