element-plus/packages/collapse/__tests__/collapse.spec.ts
2020-08-13 10:56:07 +08:00

143 lines
4.7 KiB
TypeScript

import { mount } from '@vue/test-utils'
import { nextTick } from 'vue'
import Collapse from '../src/collapse.vue'
import CollapseItem from '../src/collapse-item.vue'
describe('Collapse.vue', () => {
test('create', async () => {
const wrapper = mount({
components: {
'el-collapse': Collapse,
'el-collapse-item': CollapseItem,
},
data() {
return {
activeNames: ['1'],
}
},
template: `
<el-collapse v-model="activeNames">
<el-collapse-item title="title1" name="1">
<div class="content">111</div>
</el-collapse-item>
<el-collapse-item title="title2" name="2">
<div class="content">222</div>
</el-collapse-item>
<el-collapse-item title="title3" name="3">
<div class="content">333</div>
</el-collapse-item>
<el-collapse-item title="title4" name="4">
<div class="content">444</div>
</el-collapse-item>
</el-collapse>
`,
})
const vm = wrapper.vm
const collapseWrapper = wrapper.findComponent(Collapse)
const collapseItemWrappers = collapseWrapper.findAllComponents(CollapseItem)
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({
components: {
'el-collapse': Collapse,
'el-collapse-item': CollapseItem,
},
data() {
return {
activeNames: ['1'],
}
},
template: `
<el-collapse accordion v-model="activeNames">
<el-collapse-item title="title1" name="1">
<div class="content">111</div>
</el-collapse-item>
<el-collapse-item title="title2" name="2">
<div class="content">222</div>
</el-collapse-item>
<el-collapse-item title="title3" name="3">
<div class="content">333</div>
</el-collapse-item>
<el-collapse-item title="title4" name="4">
<div class="content">444</div>
</el-collapse-item>
</el-collapse>
`,
})
const vm = wrapper.vm
const collapseWrapper = wrapper.findComponent(Collapse)
const collapseItemWrappers = collapseWrapper.findAllComponents(CollapseItem)
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 wrapper = mount({
components: {
'el-collapse': Collapse,
'el-collapse-item': CollapseItem,
},
data() {
return {
activeNames: ['1'],
}
},
template: `
<el-collapse v-model="activeNames">
<el-collapse-item title="title1" name="1">
<div class="content">111</div>
</el-collapse-item>
<el-collapse-item title="title2" name="2">
<div class="content">222</div>
</el-collapse-item>
<el-collapse-item title="title3" name="3">
<div class="content">333</div>
</el-collapse-item>
<el-collapse-item title="title4" name="4">
<div class="content">444</div>
</el-collapse-item>
</el-collapse>
`,
})
const vm = wrapper.vm
const collapseWrapper = wrapper.findComponent(Collapse)
const collapseItemWrappers = collapseWrapper.findAllComponents(CollapseItem)
const collapseItemHeaderEls = vm.$el.querySelectorAll('.el-collapse-item__header')
expect(collapseItemWrappers[0].vm.isActive).toBe(true)
expect(vm.activeNames).toEqual(['1'])
collapseItemHeaderEls[2].click()
await nextTick()
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(collapseItemWrappers[0].vm.isActive).toBe(false)
expect(vm.activeNames).toEqual(['3'])
})
})