2022-03-25 15:35:56 +08:00
|
|
|
import { defineComponent, h, nextTick, ref } from 'vue'
|
2021-07-03 19:04:03 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2024-04-14 11:35:04 +08:00
|
|
|
import { NOOP } from '@vue/shared'
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
2021-11-29 15:58:44 +08:00
|
|
|
import { useTeleport } from '../use-teleport'
|
2022-02-21 14:28:22 +08:00
|
|
|
import type { VueWrapper } from '@vue/test-utils'
|
2021-07-03 19:04:03 +08:00
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
const Comp = defineComponent({
|
2021-07-03 19:04:03 +08:00
|
|
|
setup() {
|
|
|
|
const appendToBody = ref(true)
|
2021-09-04 19:29:28 +08:00
|
|
|
const { showTeleport, hideTeleport, renderTeleport } = useTeleport(
|
|
|
|
() => h('div', AXIOM),
|
|
|
|
appendToBody
|
|
|
|
)
|
2024-04-14 11:35:04 +08:00
|
|
|
showTeleport()
|
2021-07-03 19:04:03 +08:00
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
return () => (
|
|
|
|
<>
|
|
|
|
<button class="show" onClick={showTeleport}>
|
|
|
|
show
|
|
|
|
</button>
|
|
|
|
<button class="hide" onClick={hideTeleport}>
|
|
|
|
hide
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
class="toggle"
|
|
|
|
onClick={() => (appendToBody.value = !appendToBody.value)}
|
|
|
|
>
|
|
|
|
toggle
|
|
|
|
</button>
|
|
|
|
{renderTeleport()}
|
|
|
|
</>
|
|
|
|
)
|
2021-07-03 19:04:03 +08:00
|
|
|
},
|
2022-02-21 14:28:22 +08:00
|
|
|
})
|
2021-07-03 19:04:03 +08:00
|
|
|
|
2023-12-13 09:07:57 +08:00
|
|
|
describe('useTeleport', () => {
|
2022-02-21 14:28:22 +08:00
|
|
|
let wrapper: VueWrapper<InstanceType<typeof Comp>>
|
2021-07-03 19:04:03 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = mount(Comp)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.unmount()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should render correctly', async () => {
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.text()).not.toContain(AXIOM)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should render teleport to the DOM', async () => {
|
|
|
|
await nextTick()
|
|
|
|
expect(wrapper.text()).not.toContain(AXIOM)
|
|
|
|
|
|
|
|
await wrapper.find('.show').trigger('click')
|
|
|
|
|
|
|
|
expect(document.body.innerHTML).toContain(AXIOM)
|
|
|
|
|
|
|
|
await wrapper.find('.hide').trigger('click')
|
|
|
|
|
|
|
|
expect(document.body.innerHTML).not.toContain(AXIOM)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should render to the current wrapper instead of document.body', async () => {
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
expect(wrapper.text()).not.toContain(AXIOM)
|
|
|
|
|
|
|
|
await wrapper.find('.toggle').trigger('click')
|
|
|
|
|
|
|
|
expect(wrapper.text()).toContain(AXIOM)
|
|
|
|
})
|
|
|
|
})
|
2024-04-14 11:35:04 +08:00
|
|
|
|
|
|
|
describe('useTeleport when isClient is false', () => {
|
|
|
|
const mockIsClient = false
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
vi.resetModules()
|
|
|
|
vi.doMock('@element-plus/utils', () => ({
|
|
|
|
isClient: mockIsClient,
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
afterEach(() => {
|
|
|
|
vi.doUnmock('@element-plus/utils')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get default value when isClient is false', async () => {
|
|
|
|
const { useTeleport: mockUseTeleport } = await import('../use-teleport')
|
|
|
|
const appendToBody = ref(true)
|
|
|
|
const { isTeleportVisible, showTeleport, hideTeleport, renderTeleport } =
|
|
|
|
mockUseTeleport(() => h('div', AXIOM), appendToBody)
|
|
|
|
|
|
|
|
expect(isTeleportVisible.value).toBeFalsy()
|
|
|
|
expect(showTeleport).toEqual(NOOP)
|
|
|
|
expect(hideTeleport).toEqual(NOOP)
|
|
|
|
expect(renderTeleport).toEqual(NOOP)
|
|
|
|
})
|
|
|
|
})
|