2021-07-03 19:04:03 +08:00
|
|
|
import { ref, nextTick, h } from 'vue'
|
|
|
|
import { mount } from '@vue/test-utils'
|
|
|
|
|
2021-11-29 15:58:44 +08:00
|
|
|
import { useTeleport } from '../use-teleport'
|
2021-07-03 19:04:03 +08:00
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
|
|
|
const appendToBody = ref(true)
|
2021-09-04 19:29:28 +08:00
|
|
|
const { showTeleport, hideTeleport, renderTeleport } = useTeleport(
|
|
|
|
() => h('div', AXIOM),
|
|
|
|
appendToBody
|
|
|
|
)
|
2021-07-03 19:04:03 +08:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
return [
|
2021-09-04 19:29:28 +08:00
|
|
|
h(
|
|
|
|
'button',
|
|
|
|
{
|
|
|
|
class: 'show',
|
|
|
|
onClick: showTeleport,
|
|
|
|
},
|
|
|
|
'show'
|
|
|
|
),
|
|
|
|
h(
|
|
|
|
'button',
|
|
|
|
{
|
|
|
|
class: 'hide',
|
|
|
|
onClick: hideTeleport,
|
|
|
|
},
|
|
|
|
'hide'
|
|
|
|
),
|
2021-07-03 19:04:03 +08:00
|
|
|
h('button', {
|
|
|
|
class: 'toggle',
|
|
|
|
onClick: () => {
|
|
|
|
// toggle append to body.
|
|
|
|
appendToBody.value = !appendToBody.value
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
renderTeleport(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('useModal', () => {
|
|
|
|
let wrapper: ReturnType<typeof mount>
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|