2021-03-16 21:40:45 +08:00
|
|
|
import { ref, nextTick, defineComponent, onMounted } from 'vue'
|
|
|
|
import { mount } from '@vue/test-utils'
|
2020-09-09 21:18:08 +08:00
|
|
|
import { hasClass } from '@element-plus/utils/dom'
|
2021-03-16 21:40:45 +08:00
|
|
|
|
2020-09-09 21:18:08 +08:00
|
|
|
import useLockScreen from '../use-lockscreen'
|
|
|
|
|
2021-03-16 21:40:45 +08:00
|
|
|
const kls = 'el-popup-parent--hidden'
|
|
|
|
|
|
|
|
const Comp = defineComponent({
|
|
|
|
setup() {
|
|
|
|
const flag = ref(false)
|
|
|
|
useLockScreen(flag)
|
|
|
|
onMounted(() => {
|
|
|
|
flag.value = true
|
|
|
|
})
|
|
|
|
},
|
|
|
|
template: `<div></div>`,
|
|
|
|
})
|
|
|
|
|
2020-09-09 21:18:08 +08:00
|
|
|
describe('useLockScreen', () => {
|
|
|
|
test('should lock screen when trigger is true', async () => {
|
2021-03-16 21:40:45 +08:00
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
|
|
|
<test-comp />
|
|
|
|
`,
|
|
|
|
components: {
|
|
|
|
'test-comp': Comp,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(hasClass(document.body, kls)).toBe(true)
|
|
|
|
|
|
|
|
wrapper.unmount()
|
|
|
|
await nextTick()
|
|
|
|
expect(hasClass(document.body, kls)).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should cleanup when unmounted', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
|
|
|
<test-comp v-if="shouldRender" />
|
|
|
|
`,
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
shouldRender: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
'test-comp': Comp,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
wrapper.vm.shouldRender = false
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
expect(hasClass(document.body, kls)).toBe(false)
|
2020-09-09 21:18:08 +08:00
|
|
|
})
|
|
|
|
})
|