use-lockscreen.test.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { computed, defineComponent, nextTick, onMounted, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { describe, expect, it } from 'vitest'
  4. import { hasClass } from '@element-plus/utils'
  5. import { useLockscreen } from '../use-lockscreen'
  6. import { useNamespace } from '../use-namespace'
  7. const kls = 'el-popup-parent--hidden'
  8. const Comp = defineComponent({
  9. setup() {
  10. const flag = ref(false)
  11. useLockscreen(flag)
  12. onMounted(() => {
  13. flag.value = true
  14. })
  15. return () => undefined
  16. },
  17. })
  18. describe('useLockscreen', () => {
  19. it('should lock screen when trigger is true', async () => {
  20. const wrapper = mount({
  21. setup: () => () => <Comp />,
  22. })
  23. await nextTick()
  24. expect(hasClass(document.body, kls)).toBe(true)
  25. wrapper.unmount()
  26. await nextTick()
  27. setTimeout(() => {
  28. expect(hasClass(document.body, kls)).toBe(false)
  29. }, 250)
  30. })
  31. it('should cleanup when unmounted', async () => {
  32. const shouldRender = ref(true)
  33. mount({
  34. setup: () => () => shouldRender.value ? <Comp /> : undefined,
  35. })
  36. await nextTick()
  37. expect(hasClass(document.body, kls)).toBe(true)
  38. shouldRender.value = false
  39. await nextTick()
  40. setTimeout(() => {
  41. expect(hasClass(document.body, kls)).toBe(false)
  42. }, 250)
  43. })
  44. it('should render a different namespace than the given one', async () => {
  45. const namespace = 'test'
  46. const wrapper = mount({
  47. setup() {
  48. const ns = useNamespace(
  49. 'lock',
  50. computed(() => namespace)
  51. )
  52. const trigger = ref(false)
  53. useLockscreen(trigger, { ns })
  54. onMounted(() => {
  55. trigger.value = true
  56. })
  57. return () => () => undefined
  58. },
  59. })
  60. mount(wrapper)
  61. await nextTick()
  62. expect(hasClass(document.body, `${namespace}-lock-parent--hidden`)).toBe(
  63. true
  64. )
  65. wrapper.unmount()
  66. })
  67. })