2022-03-25 15:35:56 +08:00
|
|
|
import { defineComponent, nextTick, onMounted, ref } from 'vue'
|
2021-03-16 21:40:45 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-03-25 15:35:56 +08:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { hasClass } from '@element-plus/utils'
|
2021-03-16 21:40:45 +08:00
|
|
|
|
2021-11-29 15:58:44 +08:00
|
|
|
import { useLockscreen } from '../use-lockscreen'
|
2020-09-09 21:18:08 +08:00
|
|
|
|
2021-03-16 21:40:45 +08:00
|
|
|
const kls = 'el-popup-parent--hidden'
|
|
|
|
|
|
|
|
const Comp = defineComponent({
|
|
|
|
setup() {
|
|
|
|
const flag = ref(false)
|
2021-11-29 15:58:44 +08:00
|
|
|
useLockscreen(flag)
|
2021-03-16 21:40:45 +08:00
|
|
|
onMounted(() => {
|
|
|
|
flag.value = true
|
|
|
|
})
|
2022-02-21 14:28:22 +08:00
|
|
|
|
|
|
|
return () => undefined
|
2021-03-16 21:40:45 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-11-29 15:58:44 +08:00
|
|
|
describe('useLockscreen', () => {
|
2022-02-21 14:28:22 +08:00
|
|
|
it('should lock screen when trigger is true', async () => {
|
2021-03-16 21:40:45 +08:00
|
|
|
const wrapper = mount({
|
2022-02-21 14:28:22 +08:00
|
|
|
setup: () => () => <Comp />,
|
2021-03-16 21:40:45 +08:00
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(hasClass(document.body, kls)).toBe(true)
|
|
|
|
|
|
|
|
wrapper.unmount()
|
|
|
|
await nextTick()
|
|
|
|
expect(hasClass(document.body, kls)).toBe(false)
|
|
|
|
})
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
it('should cleanup when unmounted', async () => {
|
|
|
|
const shouldRender = ref(true)
|
|
|
|
mount({
|
|
|
|
setup: () => () => shouldRender.value ? <Comp /> : undefined,
|
2021-03-16 21:40:45 +08:00
|
|
|
})
|
|
|
|
|
2020-09-09 21:18:08 +08:00
|
|
|
await nextTick()
|
2021-03-16 21:40:45 +08:00
|
|
|
|
|
|
|
expect(hasClass(document.body, kls)).toBe(true)
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
shouldRender.value = false
|
2021-03-16 21:40:45 +08:00
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
expect(hasClass(document.body, kls)).toBe(false)
|
2020-09-09 21:18:08 +08:00
|
|
|
})
|
|
|
|
})
|