2020-08-07 16:51:34 +08:00
|
|
|
import { nextTick } from 'vue'
|
2021-09-17 15:27:31 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2020-08-07 16:51:34 +08:00
|
|
|
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)
|
2021-09-04 19:29:28 +08:00
|
|
|
const collapseItemHeaderEls = vm.$el.querySelectorAll(
|
|
|
|
'.el-collapse-item__header'
|
|
|
|
)
|
2020-08-07 16:51:34 +08:00
|
|
|
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)
|
2021-09-04 19:29:28 +08:00
|
|
|
const collapseItemHeaderEls = vm.$el.querySelectorAll(
|
|
|
|
'.el-collapse-item__header'
|
|
|
|
)
|
2020-08-07 16:51:34 +08:00
|
|
|
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 () => {
|
2020-12-10 16:17:48 +08:00
|
|
|
const onChange = jest.fn()
|
2020-08-07 16:51:34 +08:00
|
|
|
const wrapper = mount({
|
|
|
|
components: {
|
|
|
|
'el-collapse': Collapse,
|
|
|
|
'el-collapse-item': CollapseItem,
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
activeNames: ['1'],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
template: `
|
2020-12-10 16:17:48 +08:00
|
|
|
<el-collapse v-model="activeNames" @change="onChange">
|
2020-08-07 16:51:34 +08:00
|
|
|
<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>
|
|
|
|
`,
|
2020-12-10 16:17:48 +08:00
|
|
|
methods: {
|
|
|
|
onChange,
|
|
|
|
},
|
2020-08-07 16:51:34 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const vm = wrapper.vm
|
|
|
|
const collapseWrapper = wrapper.findComponent(Collapse)
|
|
|
|
const collapseItemWrappers = collapseWrapper.findAllComponents(CollapseItem)
|
2021-09-04 19:29:28 +08:00
|
|
|
const collapseItemHeaderEls = vm.$el.querySelectorAll(
|
|
|
|
'.el-collapse-item__header'
|
|
|
|
)
|
2020-08-07 16:51:34 +08:00
|
|
|
expect(collapseItemWrappers[0].vm.isActive).toBe(true)
|
|
|
|
expect(vm.activeNames).toEqual(['1'])
|
2020-12-10 16:17:48 +08:00
|
|
|
expect(onChange).not.toHaveBeenCalled()
|
2020-08-07 16:51:34 +08:00
|
|
|
collapseItemHeaderEls[2].click()
|
|
|
|
await nextTick()
|
2020-12-10 16:17:48 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(1)
|
2020-08-07 16:51:34 +08:00
|
|
|
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()
|
2020-12-10 16:17:48 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(2)
|
2020-08-07 16:51:34 +08:00
|
|
|
expect(collapseItemWrappers[0].vm.isActive).toBe(false)
|
|
|
|
expect(vm.activeNames).toEqual(['3'])
|
|
|
|
})
|
2022-01-19 17:31:51 +08:00
|
|
|
|
|
|
|
test('deep watch modelValue', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
components: {
|
|
|
|
'el-collapse': Collapse,
|
|
|
|
'el-collapse-item': CollapseItem,
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
activeNames: ['1'],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.activeNames.push('2')
|
|
|
|
},
|
|
|
|
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>
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
const collapseWrapper = wrapper.findComponent(Collapse)
|
|
|
|
const collapseItemWrappers = collapseWrapper.findAllComponents(CollapseItem)
|
|
|
|
expect(collapseItemWrappers[0].vm.isActive).toBe(true)
|
|
|
|
expect(collapseItemWrappers[1].vm.isActive).toBe(true)
|
|
|
|
})
|
2020-08-07 16:51:34 +08:00
|
|
|
})
|