mirror of
https://github.com/element-plus/element-plus.git
synced 2025-01-12 10:45:10 +08:00
fix(hooks): fix use-lock-screen hook (#1651)
- Fix when unmounting `use-lock-screen` could cleanup the side effects
This commit is contained in:
parent
2299dcf399
commit
184af0b56f
@ -1,15 +1,62 @@
|
||||
import { ref, nextTick } from 'vue'
|
||||
import { ref, nextTick, defineComponent, onMounted } from 'vue'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { hasClass } from '@element-plus/utils/dom'
|
||||
|
||||
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
|
||||
})
|
||||
},
|
||||
template: `<div></div>`,
|
||||
})
|
||||
|
||||
describe('useLockScreen', () => {
|
||||
test('should lock screen when trigger is true', async () => {
|
||||
const _ref = ref(false)
|
||||
const cls = 'el-popup-parent--hidden'
|
||||
useLockScreen(_ref)
|
||||
expect(hasClass(document.body, cls)).toBe(false)
|
||||
_ref.value = true
|
||||
const wrapper = mount({
|
||||
template: `
|
||||
<test-comp />
|
||||
`,
|
||||
components: {
|
||||
'test-comp': Comp,
|
||||
},
|
||||
})
|
||||
await nextTick()
|
||||
expect(hasClass(document.body, cls)).toBe(true)
|
||||
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,
|
||||
},
|
||||
})
|
||||
|
||||
await nextTick()
|
||||
|
||||
expect(hasClass(document.body, kls)).toBe(true)
|
||||
|
||||
wrapper.vm.shouldRender = false
|
||||
await nextTick()
|
||||
|
||||
expect(hasClass(document.body, kls)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { watch, isRef } from 'vue'
|
||||
import { watch, isRef, onUnmounted } from 'vue'
|
||||
|
||||
import getScrollBarWidth from '@element-plus/utils/scrollbar-width'
|
||||
import throwError from '@element-plus/utils/error'
|
||||
@ -27,6 +27,17 @@ export default (trigger: Ref<boolean>) => {
|
||||
let withoutHiddenClass = false
|
||||
let bodyPaddingRight = '0'
|
||||
let computedBodyPaddingRight = 0
|
||||
|
||||
onUnmounted(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
const cleanup = () => {
|
||||
removeClass(document.body, 'el-popup-parent--hidden')
|
||||
if (withoutHiddenClass) {
|
||||
document.body.style.paddingRight = bodyPaddingRight
|
||||
}
|
||||
}
|
||||
watch(trigger, val => {
|
||||
if (val) {
|
||||
withoutHiddenClass = !hasClass(document.body, 'el-popup-parent--hidden')
|
||||
@ -51,11 +62,7 @@ export default (trigger: Ref<boolean>) => {
|
||||
}
|
||||
addClass(document.body, 'el-popup-parent--hidden')
|
||||
} else {
|
||||
if (withoutHiddenClass) {
|
||||
document.body.style.paddingRight = bodyPaddingRight
|
||||
removeClass(document.body, 'el-popup-parent--hidden')
|
||||
}
|
||||
withoutHiddenClass = true
|
||||
cleanup()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user