element-plus/packages/hooks/__tests__/use-lockscreen.vitest.tsx
三咲智子 aaf90d99d0
test: switch to vitest (#5991)
* test: use vitest

* test: add script and ci

* chore: improve tsconfig

* refactor: use-form-item

* fix: remove unused

* chore: improve scripts

* test: improve mock

* refactor: change coverage
2022-02-21 14:28:22 +08:00

51 lines
1.2 KiB
TypeScript

import { ref, nextTick, defineComponent, onMounted } from 'vue'
import { mount } from '@vue/test-utils'
import { describe, it, expect } from 'vitest'
import { hasClass } from '@element-plus/utils'
import { useLockscreen } from '../use-lockscreen'
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()
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()
expect(hasClass(document.body, kls)).toBe(false)
})
})