element-plus/packages/components/tooltip/__tests__/tooltip.spec.ts

127 lines
3.4 KiB
TypeScript
Raw Normal View History

import { nextTick } from 'vue'
import { mount } from '@vue/test-utils'
import { debugWarn } from '@element-plus/utils'
import { rAF } from '@element-plus/test-utils/tick'
import { ElPopperTrigger } from '@element-plus/components/popper'
import Tooltip from '../src/tooltip.vue'
2020-08-25 22:25:46 +08:00
import type { VNode } from 'vue'
jest.useFakeTimers()
jest.mock('@element-plus/utils/error', () => ({
debugWarn: jest.fn(),
}))
const AXIOM = 'Rem is the best girl'
describe('<ElTooltip />', () => {
const createComponent = (props: any = {}, content: string | VNode = '') =>
mount(Tooltip, {
slots: {
default: () => AXIOM,
content: () => content,
},
props,
attachTo: document.body,
})
let wrapper: ReturnType<typeof createComponent>
const findTrigger = () => wrapper.findComponent(ElPopperTrigger)
2020-08-25 22:25:46 +08:00
afterEach(() => {
wrapper?.unmount()
2020-08-25 22:25:46 +08:00
document.body.innerHTML = ''
;(debugWarn as jest.Mock).mockClear()
2020-08-25 22:25:46 +08:00
})
describe('rendering', () => {
it('should render correctly', async () => {
wrapper = createComponent()
await nextTick()
expect(findTrigger().text()).toContain(AXIOM)
})
it('content should teleport according appendTo', async () => {
const el = document.createElement('div')
el.id = 'test'
document.body.appendChild(el)
wrapper = createComponent({ appendTo: '#test' }, 'test appendTo props')
await nextTick()
const trigger$ = findTrigger()
const triggerEl = trigger$.find('.el-tooltip__trigger')
await triggerEl.trigger('mouseenter')
expect(document.querySelector('#test')?.innerHTML).toContain(
'test appendTo props'
)
})
2020-08-25 22:25:46 +08:00
})
describe('deprecating API', () => {
it('should warn about API that will be deprecated', async () => {
expect(debugWarn).toHaveBeenCalledTimes(0)
wrapper = createComponent({
openDelay: 200,
visibleArrow: true,
})
await nextTick()
expect(debugWarn).toHaveBeenCalledTimes(2)
})
})
describe('functionality', () => {
const content = 'Test content'
2020-08-25 22:25:46 +08:00
it('should be able to update popper content manually', async () => {
wrapper = createComponent()
await nextTick()
const { vm } = wrapper
expect(vm.updatePopper).toBeDefined()
vm.updatePopper()
})
it('should be able to open & close tooltip content', async () => {
wrapper = createComponent({}, content)
await nextTick()
const trigger$ = findTrigger()
const triggerEl = trigger$.find('.el-tooltip__trigger')
await triggerEl.trigger('mouseenter')
jest.runAllTimers()
await rAF()
expect(wrapper.emitted()).toHaveProperty('show')
await triggerEl.trigger('mouseleave') // dispatches a timer with 200ms timeout.
jest.runAllTimers()
await rAF()
expect(wrapper.emitted()).toHaveProperty('hide')
})
it('should be able to toggle visibility of tooltip content', async () => {
wrapper = createComponent(
{
trigger: 'click',
},
content
)
await nextTick()
const trigger$ = findTrigger()
const triggerEl = trigger$.find('.el-tooltip__trigger')
await triggerEl.trigger('click')
jest.runAllTimers()
await rAF()
expect(wrapper.emitted()).toHaveProperty('show')
await triggerEl.trigger('click')
jest.runAllTimers()
await rAF()
expect(wrapper.emitted()).toHaveProperty('hide')
})
})
})