import { nextTick } 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' function destroyLoadingInstance(loadingInstance) { if (!loadingInstance) return loadingInstance.close() loadingInstance.$el && loadingInstance.$el.parentNode && loadingInstance.$el.parentNode.removeChild(loadingInstance.$el) } describe('Loading', () => { let loadingInstance, loadingInstance2 afterEach(() => { destroyLoadingInstance(loadingInstance) destroyLoadingInstance(loadingInstance2) }) test('create directive', async () => { const wrapper = mount({ directives: { loading: vLoading, }, template: `
`, data() { return { loading: true, } }, }) await nextTick() const vm = wrapper.vm const maskWrapper = wrapper.find('.el-loading-mask') expect(maskWrapper.exists()).toBeTruthy() vi.useFakeTimers() vm.loading = 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 wrapper1 = mount({ directives: { loading: vLoading, }, template: ``, data() { return { show: true, loading: true, } }, }) const wrapper2 = mount({ directives: { loading: vLoading, }, template: ``, data() { return { show: true, loading: true, } }, }) await nextTick() const vm1 = wrapper1.vm const vm2 = wrapper2.vm vm1.loading = false vm2.loading = false await nextTick() vm1.show = false vm2.show = false await nextTick() expect(document.querySelector('.el-loading-mask')).toBeFalsy() }) test('body directive', async () => { const wrapper = mount({ directives: { loading: vLoading, }, template: ``, data() { return { loading: true, } }, }) 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 wrapper = mount({ directives: { loading: vLoading, }, template: ``, data() { return { loading: true, } }, }) await nextTick() const mask = document.querySelector('.el-loading-mask') expect(mask.parentNode === document.body).toBeTruthy() expect(mask.classList.contains('is-fullscreen')).toBeTruthy() wrapper.vm.loading = false document.body.removeChild(mask) }) test('lock directive', async () => { const wrapper = mount({ directives: { loading: vLoading, }, template: ``, data() { return { loading: true, } }, }) const vm = wrapper.vm await nextTick() expect( document.body.classList.contains('el-loading-parent--hidden') ).toBeTruthy() vm.loading = false document.body.removeChild(document.querySelector('.el-loading-mask')) }) test('text directive', async () => { const wrapper = mount({ directives: { loading: vLoading, }, template: ``, data() { return { loading: true, } }, }) await nextTick() expect(wrapper.find('.el-loading-text').text()).toEqual('loading...') }) test('customClass directive', async () => { const wrapper = mount({ directives: { loading: vLoading, }, template: ``, data() { return { loading: true, } }, }) await nextTick() expect(wrapper.find('.loading-custom-class').exists()).toBeTruthy() }) test('customSvg directive', async () => { const wrapper = mount({ directives: { loading: vLoading, }, template: ``, data() { return { loading: true, svg: `