Files
element-plus/packages/hooks/__tests__/use-lockscreen.test.tsx
Liao-js a116f0ef71 fix(hooks): [use-lockscreen] remove hiddenCls (#19429)
* fix(hooks): [use-lockscreen] remove hiddenCls

* fix(hooks): [useLockscreen] unit test

* test(hooks): [useLockscreen] should not cleanup when not all unmounted

* test(hooks): [useLockscreen] update test

* test(hooks): [useLockscreen] update test
2025-01-05 23:07:27 +08:00

100 lines
2.3 KiB
TypeScript

import { computed, defineComponent, nextTick, onMounted, ref } from 'vue'
import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import sleep from '@element-plus/test-utils/sleep'
import { hasClass } from '@element-plus/utils'
import { useLockscreen } from '../use-lockscreen'
import { useNamespace } from '../use-namespace'
const kls = 'el-popup-parent--hidden'
const Comp = defineComponent({
setup() {
const flag = ref(false)
useLockscreen(flag)
onMounted(() => {
flag.value = true
})
return () => undefined
},
})
describe('useLockscreen', () => {
it('should lock screen when trigger is true', async () => {
const wrapper = mount({
setup: () => () => <Comp />,
})
await nextTick()
expect(hasClass(document.body, kls)).toBe(true)
wrapper.unmount()
await nextTick()
await sleep(250)
expect(hasClass(document.body, kls)).toBe(false)
})
it('should cleanup when unmounted', async () => {
const shouldRender = ref(true)
mount({
setup: () => () => shouldRender.value ? <Comp /> : undefined,
})
await nextTick()
expect(hasClass(document.body, kls)).toBe(true)
shouldRender.value = false
await nextTick()
await sleep(250)
expect(hasClass(document.body, kls)).toBe(false)
})
it('should not cleanup when not all unmounted', async () => {
const wrapper1 = mount({
setup: () => () => <Comp />,
})
const wrapper2 = mount({
setup: () => () => <Comp />,
})
await nextTick()
expect(hasClass(document.body, kls)).toBe(true)
wrapper2.unmount()
await sleep(250)
expect(hasClass(document.body, kls)).toBe(true)
wrapper1.unmount()
await sleep(250)
expect(hasClass(document.body, kls)).toBe(false)
})
it('should render a different namespace than the given one', async () => {
const namespace = 'test'
const wrapper = mount({
setup() {
const ns = useNamespace(
'lock',
computed(() => namespace)
)
const trigger = ref(false)
useLockscreen(trigger, { ns })
onMounted(() => {
trigger.value = true
})
return () => undefined
},
})
await nextTick()
expect(hasClass(document.body, `${namespace}-lock-parent--hidden`)).toBe(
true
)
wrapper.unmount()
})
})