2022-01-31 23:07:09 +08:00
|
|
|
import { h, nextTick } from 'vue'
|
2022-04-19 12:46:57 +08:00
|
|
|
import { afterEach, describe, expect, it, test, vi } from 'vitest'
|
2022-03-25 15:35:56 +08:00
|
|
|
import { POPPER_CONTAINER_SELECTOR, useZIndex } from '@element-plus/hooks'
|
2021-08-24 13:36:48 +08:00
|
|
|
import makeMount from '@element-plus/test-utils/make-mount'
|
2022-01-31 23:07:09 +08:00
|
|
|
import { rAF } from '@element-plus/test-utils/tick'
|
|
|
|
import { ElPopperTrigger } from '@element-plus/components/popper'
|
2021-08-24 13:36:48 +08:00
|
|
|
import Popover from '../src/index.vue'
|
2020-10-19 13:44:50 +08:00
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
2022-04-19 12:46:57 +08:00
|
|
|
|
2020-10-19 13:44:50 +08:00
|
|
|
const mount = makeMount(Popover, {
|
|
|
|
slots: {
|
2022-01-04 09:15:15 +08:00
|
|
|
default: () => AXIOM,
|
|
|
|
reference: () => h('button', 'click me'),
|
2020-10-19 13:44:50 +08:00
|
|
|
},
|
2022-02-04 14:59:58 +08:00
|
|
|
props: {},
|
2022-01-04 09:15:15 +08:00
|
|
|
global: {
|
|
|
|
attachTo: document.body,
|
|
|
|
},
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|
|
|
|
describe('Popover.vue', () => {
|
2022-01-04 09:15:15 +08:00
|
|
|
let wrapper: ReturnType<typeof mount>
|
|
|
|
const findContentComp = () =>
|
|
|
|
wrapper.findComponent({
|
|
|
|
name: 'ElPopperContent',
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper?.unmount()
|
|
|
|
document.body.innerHTML = ''
|
|
|
|
})
|
|
|
|
|
2020-10-19 13:44:50 +08:00
|
|
|
test('render test', () => {
|
2022-01-04 09:15:15 +08:00
|
|
|
wrapper = mount()
|
|
|
|
|
|
|
|
expect(findContentComp().text()).toEqual(AXIOM)
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should render with title', () => {
|
|
|
|
const title = 'test title'
|
2022-01-04 09:15:15 +08:00
|
|
|
wrapper = mount({
|
2020-10-19 13:44:50 +08:00
|
|
|
props: {
|
|
|
|
title,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
expect(findContentComp().text()).toContain(title)
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
test("should modify popover's style with width", async () => {
|
2022-01-04 09:15:15 +08:00
|
|
|
wrapper = mount({
|
2020-10-19 13:44:50 +08:00
|
|
|
props: {
|
|
|
|
width: 200,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const popperContent = findContentComp()
|
|
|
|
expect(getComputedStyle(popperContent.element).width).toBe('200px')
|
2020-10-19 13:44:50 +08:00
|
|
|
|
|
|
|
await wrapper.setProps({
|
|
|
|
width: '100vw',
|
|
|
|
})
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
expect(getComputedStyle(popperContent.element).width).toBe('100vw')
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('the content should be overrode by slots', () => {
|
|
|
|
const content = 'test content'
|
2022-01-04 09:15:15 +08:00
|
|
|
wrapper = mount({
|
2020-10-19 13:44:50 +08:00
|
|
|
props: {
|
|
|
|
content,
|
|
|
|
},
|
|
|
|
})
|
2022-01-04 09:15:15 +08:00
|
|
|
expect(findContentComp().text()).toContain(AXIOM)
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should render content when no slots were passed', () => {
|
|
|
|
const content = 'test content'
|
2022-01-04 09:15:15 +08:00
|
|
|
const virtualRef = document.createElement('button')
|
|
|
|
wrapper = makeMount(Popover, {
|
2020-10-19 13:44:50 +08:00
|
|
|
props: {
|
|
|
|
content,
|
2022-02-11 17:56:21 +08:00
|
|
|
teleported: false,
|
2022-01-04 09:15:15 +08:00
|
|
|
virtualRef,
|
|
|
|
virtualTriggering: true,
|
2020-10-19 13:44:50 +08:00
|
|
|
},
|
|
|
|
})()
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
expect(findContentComp().text()).toBe(content)
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|
2020-11-20 10:04:20 +08:00
|
|
|
|
|
|
|
test('popper z-index should be dynamical', () => {
|
2022-01-04 09:15:15 +08:00
|
|
|
wrapper = mount()
|
2020-11-20 10:04:20 +08:00
|
|
|
|
2022-02-09 16:31:31 +08:00
|
|
|
const { currentZIndex } = useZIndex()
|
2020-11-20 10:04:20 +08:00
|
|
|
expect(
|
2022-01-04 09:15:15 +08:00
|
|
|
Number.parseInt(window.getComputedStyle(findContentComp().element).zIndex)
|
2022-02-09 16:31:31 +08:00
|
|
|
).toBeLessThanOrEqual(currentZIndex.value)
|
2021-03-26 16:01:29 +08:00
|
|
|
})
|
2022-01-18 10:42:21 +08:00
|
|
|
|
|
|
|
test('defind hide method', async () => {
|
|
|
|
wrapper = mount()
|
|
|
|
const vm = wrapper.vm as any
|
|
|
|
expect(vm.hide).toBeDefined()
|
|
|
|
})
|
2022-01-31 23:07:09 +08:00
|
|
|
|
|
|
|
test('should be able to emit after-enter and after-leave', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
attrs: {
|
|
|
|
trigger: 'click',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
const trigger$ = wrapper.findComponent(ElPopperTrigger)
|
|
|
|
const triggerEl = trigger$.find('.el-tooltip__trigger')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.useFakeTimers()
|
2022-01-31 23:07:09 +08:00
|
|
|
await triggerEl.trigger('click')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
|
|
|
vi.useRealTimers()
|
2022-01-31 23:07:09 +08:00
|
|
|
await rAF()
|
|
|
|
expect(wrapper.emitted()).toHaveProperty('after-enter')
|
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.useFakeTimers()
|
2022-01-31 23:07:09 +08:00
|
|
|
await triggerEl.trigger('click')
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.runAllTimers()
|
|
|
|
vi.useRealTimers()
|
2022-01-31 23:07:09 +08:00
|
|
|
await rAF()
|
|
|
|
expect(wrapper.emitted()).toHaveProperty('after-leave')
|
|
|
|
})
|
2022-02-04 14:59:58 +08:00
|
|
|
|
|
|
|
describe('teleported API', () => {
|
|
|
|
it('should mount on popper container', async () => {
|
|
|
|
expect(document.body.innerHTML).toBe('')
|
|
|
|
mount()
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect(
|
|
|
|
document.body.querySelector(POPPER_CONTAINER_SELECTOR).innerHTML
|
|
|
|
).not.toBe('')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not mount on the popper container', async () => {
|
|
|
|
expect(document.body.innerHTML).toBe('')
|
|
|
|
mount({
|
|
|
|
props: { teleported: false },
|
|
|
|
})
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect(
|
|
|
|
document.body.querySelector(POPPER_CONTAINER_SELECTOR).innerHTML
|
|
|
|
).toBe('')
|
|
|
|
})
|
|
|
|
})
|
2020-10-19 13:44:50 +08:00
|
|
|
})
|