import { nextTick, ref } from 'vue' import { mount } from '@vue/test-utils' import { afterEach, describe, expect, test, vi } from 'vitest' import { Loading } from '../src/service' import { vLoading } from '../src/directive' import ElInput from '../../input' import type { VNode } from 'vue' import type { LoadingInstance } from '../src/loading' function destroyLoadingInstance(loadingInstance: LoadingInstance) { if (!loadingInstance) return loadingInstance.close() loadingInstance.$el && loadingInstance.$el.parentNode && loadingInstance.$el.parentNode.removeChild(loadingInstance.$el) } const _mount = (render?: () => VNode | null) => { return mount(render, { global: { directives: { loading: vLoading }, }, }) } describe('Loading', () => { let loadingInstance: LoadingInstance, loadingInstance2: LoadingInstance afterEach(() => { destroyLoadingInstance(loadingInstance) destroyLoadingInstance(loadingInstance2) }) test('create directive', async () => { const loading = ref(true) const wrapper = _mount(() =>
) await nextTick() const maskWrapper = wrapper.find('.el-loading-mask') expect(maskWrapper.exists()).toBeTruthy() vi.useFakeTimers() loading.value = false // Trigger update event for dispatching close event. await nextTick() vi.runAllTimers() vi.useRealTimers() await nextTick() expect(wrapper.find('.el-loading-mask').exists()).toBeFalsy() }) test('unmounted directive', async () => { const loading1 = ref(true) const show1 = ref(true) const loading2 = ref(true) const show2 = ref(true) _mount(() => (show1.value ? : null)) _mount(() => (show2.value ? : null)) await nextTick() loading1.value = false loading2.value = false await nextTick() show1.value = false show2.value = false await nextTick() expect(document.querySelector('.el-loading-mask')).toBeFalsy() }) test('body directive', async () => { const loading = ref(true) const wrapper = _mount(() => ) await nextTick() const mask = document.querySelector('.el-loading-mask')! expect(mask.parentNode === document.body).toBeTruthy() wrapper.vm.loading = false document.body.removeChild(mask) }) test('fullscreen directive', async () => { const loading = ref(true) _mount(() => ) await nextTick() const mask = document.querySelector('.el-loading-mask')! expect(mask.parentNode === document.body).toBeTruthy() expect(mask.classList.contains('is-fullscreen')).toBeTruthy() loading.value = false document.body.removeChild(mask) }) test('lock directive', async () => { const loading = ref(true) _mount(() => ) await nextTick() expect( document.body.classList.contains('el-loading-parent--hidden') ).toBeTruthy() loading.value = false document.body.removeChild(document.querySelector('.el-loading-mask')!) }) test('text directive', async () => { const loading = ref(true) const wrapper = _mount(() => ( )) await nextTick() expect(wrapper.find('.el-loading-text').text()).toEqual('loading...') }) test('customClass directive', async () => { const loading = ref(true) const wrapper = _mount(() => ( )) await nextTick() expect(wrapper.find('.loading-custom-class').exists()).toBeTruthy() }) test('customSvg directive', async () => { const loading = ref(true) const svg = '