2021-03-26 16:01:29 +08:00
|
|
|
import { h, nextTick, Fragment, withDirectives, ref } from 'vue'
|
2020-10-19 13:44:50 +08:00
|
|
|
import makeMount from '@element-plus/test-utils/make-mount'
|
2020-12-03 22:43:18 +08:00
|
|
|
import { rAF } from '@element-plus/test-utils/tick'
|
2021-08-24 13:36:48 +08:00
|
|
|
import Popover from '../src/index.vue'
|
|
|
|
import PopoverDirective, { VPopover } from '../src/directive'
|
2020-10-19 13:44:50 +08:00
|
|
|
|
|
|
|
import type { ComponentPublicInstance } from 'vue'
|
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
template: `
|
|
|
|
<el-popover title="title" :content="content" ref="popover"/>
|
|
|
|
<div v-popover:popover v-if="visible" id="reference-node">
|
|
|
|
${AXIOM}
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
components: {
|
|
|
|
[Popover.name]: Popover,
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
visible: false,
|
|
|
|
content: AXIOM,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const mount = makeMount(Comp, {
|
2021-09-04 19:29:28 +08:00
|
|
|
props: {},
|
2020-10-19 13:44:50 +08:00
|
|
|
global: {
|
|
|
|
directives: {
|
|
|
|
[VPopover]: PopoverDirective,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('v-popover', () => {
|
|
|
|
test('should render correctly', () => {
|
|
|
|
const wrapper = mount()
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(document.body.querySelector('.el-popover').innerHTML).toContain(
|
|
|
|
AXIOM
|
|
|
|
)
|
2020-12-03 22:43:18 +08:00
|
|
|
wrapper.unmount()
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should show popover when reference is mounted', async () => {
|
|
|
|
const wrapper = mount()
|
|
|
|
const refNode = '#reference-node'
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(wrapper.find(refNode).exists()).toBe(false)
|
|
|
|
;(wrapper.vm as ComponentPublicInstance<{ visible: boolean }>).visible =
|
|
|
|
true
|
2020-10-19 13:44:50 +08:00
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
expect(wrapper.find(refNode).exists()).toBe(true)
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
document.body.querySelector('.el-popover').getAttribute('style')
|
|
|
|
).toContain('display: none')
|
2020-10-19 13:44:50 +08:00
|
|
|
await wrapper.find(refNode).trigger('click')
|
2020-12-03 22:43:18 +08:00
|
|
|
await rAF()
|
|
|
|
await nextTick()
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
document.body.querySelector('.el-popover').getAttribute('style')
|
|
|
|
).not.toContain('display: none')
|
2020-12-03 22:43:18 +08:00
|
|
|
wrapper.unmount()
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|
2021-03-26 16:01:29 +08:00
|
|
|
|
|
|
|
test('should render correctly with tabindex', async () => {
|
|
|
|
const tabindex = ref(1)
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
|
|
|
return () => {
|
|
|
|
return h(Fragment, null, [
|
2021-09-04 19:29:28 +08:00
|
|
|
h(Popover, {
|
|
|
|
title: 'title',
|
|
|
|
content: AXIOM,
|
|
|
|
ref: 'popover',
|
|
|
|
tabindex: tabindex.value,
|
|
|
|
}),
|
|
|
|
withDirectives(h('div', { ref: 'trigger' }, AXIOM), [
|
|
|
|
[PopoverDirective, 'popover'],
|
|
|
|
]),
|
2021-03-26 16:01:29 +08:00
|
|
|
])
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = makeMount(Comp, {})()
|
|
|
|
const triggerDom = wrapper.vm.$refs.trigger
|
|
|
|
expect((triggerDom as HTMLElement).getAttribute('tabindex')).toEqual('1')
|
|
|
|
|
|
|
|
tabindex.value = 2
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect((triggerDom as HTMLElement).getAttribute('tabindex')).toEqual('2')
|
|
|
|
|
|
|
|
wrapper.unmount()
|
|
|
|
})
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|