import { nextTick } from 'vue' import { mount } from '@vue/test-utils' import { describe, expect, test, vi } from 'vitest' import Collapse from '../src/collapse.vue' import CollapseItem from '../src/collapse-item.vue' import type { VueWrapper } from '@vue/test-utils' import type { CollapseItemInstance } from '../src/collapse-item' describe('Collapse.vue', () => { test('create', async () => { const wrapper = mount({ data() { return { activeNames: ['1'], } }, render() { return (
111
222
333
444
) }, }) const vm = wrapper.vm const collapseWrapper = wrapper.findComponent(Collapse) const collapseItemWrappers = collapseWrapper.findAllComponents( CollapseItem ) as VueWrapper[] const collapseItemHeaderEls = vm.$el.querySelectorAll( '.el-collapse-item__header' ) expect(collapseItemWrappers[0].vm.isActive).toBe(true) collapseItemHeaderEls[2].click() await nextTick() expect(collapseItemWrappers[0].vm.isActive).toBe(true) expect(collapseItemWrappers[2].vm.isActive).toBe(true) collapseItemHeaderEls[0].click() await nextTick() expect(collapseItemWrappers[0].vm.isActive).toBe(false) }) test('accordion', async () => { const wrapper = mount({ data() { return { activeNames: ['1'], } }, render() { return (
111
222
333
444
) }, }) const vm = wrapper.vm const collapseWrapper = wrapper.findComponent(Collapse) const collapseItemWrappers = collapseWrapper.findAllComponents( CollapseItem ) as VueWrapper[] const collapseItemHeaderEls = vm.$el.querySelectorAll( '.el-collapse-item__header' ) expect(collapseItemWrappers[0].vm.isActive).toBe(true) collapseItemHeaderEls[2].click() await nextTick() expect(collapseItemWrappers[0].vm.isActive).toBe(false) expect(collapseItemWrappers[2].vm.isActive).toBe(true) collapseItemHeaderEls[0].click() await nextTick() expect(collapseItemWrappers[0].vm.isActive).toBe(true) expect(collapseItemWrappers[2].vm.isActive).toBe(false) }) test('event:change', async () => { const onChange = vi.fn() const wrapper = mount({ data() { return { activeNames: ['1'], } }, render() { return (
111
222
333
444
) }, }) const vm = wrapper.vm const collapseWrapper = wrapper.findComponent(Collapse) const collapseItemWrappers = collapseWrapper.findAllComponents( CollapseItem ) as VueWrapper[] const collapseItemHeaderEls = vm.$el.querySelectorAll( '.el-collapse-item__header' ) expect(collapseItemWrappers[0].vm.isActive).toBe(true) expect(vm.activeNames).toEqual(['1']) expect(onChange).not.toHaveBeenCalled() collapseItemHeaderEls[2].click() await nextTick() expect(onChange).toHaveBeenCalledTimes(1) expect(collapseItemWrappers[0].vm.isActive).toBe(true) expect(collapseItemWrappers[2].vm.isActive).toBe(true) expect(vm.activeNames).toEqual(['1', '3']) collapseItemHeaderEls[0].click() await nextTick() expect(onChange).toHaveBeenCalledTimes(2) expect(collapseItemWrappers[0].vm.isActive).toBe(false) expect(vm.activeNames).toEqual(['3']) }) test('deep watch modelValue', async () => { const wrapper = mount({ data() { return { activeNames: ['1'], } }, mounted() { this.activeNames.push('2') }, render() { return (
111
222
333
444
) }, }) await nextTick() const collapseWrapper = wrapper.findComponent(Collapse) const collapseItemWrappers = collapseWrapper.findAllComponents( CollapseItem ) as VueWrapper[] expect(collapseItemWrappers[0].vm.isActive).toBe(true) expect(collapseItemWrappers[1].vm.isActive).toBe(true) }) })