refactor(components): [popper] use JSX in Unit test (#8462)

This commit is contained in:
zz 2022-09-22 09:45:43 +08:00 committed by GitHub
parent d73016155a
commit ff1d3db089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 33 deletions

View File

@ -1,10 +1,11 @@
import { nextTick, ref } from 'vue' import { nextTick, ref } from 'vue'
import { shallowMount } from '@vue/test-utils' import { mount } from '@vue/test-utils'
import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { POPPER_CONTENT_INJECTION_KEY } from '@element-plus/tokens' import { POPPER_CONTENT_INJECTION_KEY } from '@element-plus/tokens'
import ElArrow from '../src/arrow.vue' import ElArrow from '../src/arrow.vue'
import type { VueWrapper } from '@vue/test-utils' import type { VueWrapper } from '@vue/test-utils'
import type { PopperArrowInstance } from '../src/arrow'
const popperContentInjection = { const popperContentInjection = {
arrowRef: ref(null), arrowRef: ref(null),
@ -12,7 +13,7 @@ const popperContentInjection = {
} }
const mountArrow = () => const mountArrow = () =>
shallowMount(ElArrow, { mount(<ElArrow />, {
global: { global: {
provide: { provide: {
[POPPER_CONTENT_INJECTION_KEY as symbol]: popperContentInjection, [POPPER_CONTENT_INJECTION_KEY as symbol]: popperContentInjection,
@ -21,7 +22,7 @@ const mountArrow = () =>
}) })
describe('<ElPopperArrow />', () => { describe('<ElPopperArrow />', () => {
let wrapper: VueWrapper<InstanceType<typeof ElArrow>> let wrapper: VueWrapper<PopperArrowInstance>
beforeEach(() => { beforeEach(() => {
wrapper = mountArrow() wrapper = mountArrow()

View File

@ -4,6 +4,9 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { POPPER_INJECTION_KEY } from '@element-plus/tokens' import { POPPER_INJECTION_KEY } from '@element-plus/tokens'
import ElContent from '../src/content.vue' import ElContent from '../src/content.vue'
import type { VueWrapper } from '@vue/test-utils'
import type { PopperContentInstance } from '../src/content'
const AXIOM = 'rem is the best girl' const AXIOM = 'rem is the best girl'
const popperInjection = { const popperInjection = {
triggerRef: ref(), triggerRef: ref(),
@ -12,11 +15,7 @@ const popperInjection = {
} }
const mountContent = (props = {}) => const mountContent = (props = {}) =>
mount(ElContent, { mount(<ElContent {...props}>{AXIOM}</ElContent>, {
props,
slots: {
default: () => AXIOM,
},
global: { global: {
provide: { provide: {
[POPPER_INJECTION_KEY as symbol]: popperInjection, [POPPER_INJECTION_KEY as symbol]: popperInjection,
@ -27,7 +26,8 @@ const mountContent = (props = {}) =>
describe('<ElPopperContent />', () => { describe('<ElPopperContent />', () => {
describe('with triggerRef provided', () => { describe('with triggerRef provided', () => {
const triggerKls = 'el-popper__trigger' const triggerKls = 'el-popper__trigger'
let wrapper: ReturnType<typeof mountContent> let wrapper: VueWrapper<PopperContentInstance>
beforeEach(() => { beforeEach(() => {
const trigger = document.createElement('div') const trigger = document.createElement('div')
trigger.className = triggerKls trigger.className = triggerKls

View File

@ -1,4 +1,4 @@
import { h, inject, nextTick } from 'vue' import { defineComponent, inject, nextTick } from 'vue'
import { mount } from '@vue/test-utils' import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
import { POPPER_INJECTION_KEY } from '@element-plus/tokens' import { POPPER_INJECTION_KEY } from '@element-plus/tokens'
@ -6,29 +6,20 @@ import ElPopper from '../src/popper.vue'
const AXIOM = 'rem is the best girl' const AXIOM = 'rem is the best girl'
const TestChild = { const TestChild = defineComponent({
template: `<div ref="contentRef">${AXIOM}</div>`,
setup() { setup() {
const { contentRef } = inject(POPPER_INJECTION_KEY, undefined)! const { contentRef } = inject(POPPER_INJECTION_KEY, undefined)!
return () => <div ref={contentRef}>{AXIOM}</div>
return {
contentRef,
}
}, },
} })
describe('<ElPopper />', () => { describe('<ElPopper />', () => {
const mountPopper = () => {
return mount(ElPopper, {
slots: {
default: () => h(TestChild),
},
})
}
it('should be able to provide instance to its children', async () => { it('should be able to provide instance to its children', async () => {
const wrapper = mountPopper() const wrapper = mount(
<ElPopper>
<TestChild />
</ElPopper>
)
await nextTick() await nextTick()

View File

@ -4,18 +4,19 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
import { POPPER_INJECTION_KEY } from '@element-plus/tokens' import { POPPER_INJECTION_KEY } from '@element-plus/tokens'
import ElTrigger from '../src/trigger.vue' import ElTrigger from '../src/trigger.vue'
import type { VueWrapper } from '@vue/test-utils'
import type { PopperTriggerInstance } from '../src/trigger'
const AXIOM = 'rem is the best girl' const AXIOM = 'rem is the best girl'
const popperInjection = { const popperInjection = {
triggerRef: ref(null), triggerRef: ref(null),
popperInstanceRef: ref(null), popperInstanceRef: ref(null),
contentRef: ref(null), contentRef: ref(null),
} }
const mountTrigger = (props = {}) => const mountTrigger = (props = {}) =>
mount(ElTrigger, { mount(<ElTrigger {...props}>{AXIOM}</ElTrigger>, {
props,
slots: {
default: () => AXIOM,
},
global: { global: {
provide: { provide: {
[POPPER_INJECTION_KEY as symbol]: popperInjection, [POPPER_INJECTION_KEY as symbol]: popperInjection,
@ -24,7 +25,7 @@ const mountTrigger = (props = {}) =>
}) })
describe('<ElPopperTrigger />', () => { describe('<ElPopperTrigger />', () => {
let wrapper: ReturnType<typeof mountTrigger> let wrapper: VueWrapper<PopperTriggerInstance>
afterEach(() => { afterEach(() => {
popperInjection.triggerRef.value = null popperInjection.triggerRef.value = null