diff --git a/src/collapse/__test__/index.test.jsx b/src/collapse/__test__/index.test.jsx new file mode 100644 index 000000000..bb792e58e --- /dev/null +++ b/src/collapse/__test__/index.test.jsx @@ -0,0 +1,225 @@ +import { ref, reactive } from 'vue'; +import { mount } from '@vue/test-utils'; +import { Collapse, CollapsePanel } from '../index'; + +describe('Collapse', () => { + describe('collapse props', () => { + test('defaultExpandAll', async () => { + const defaultExpandAll = ref(false); + const wrapper = mount({ + setup() { + return () => ( + + + + + ); + }, + }); + + const panel1 = wrapper.findComponent({ ref: '1' }); + const panel2 = wrapper.findComponent({ ref: '2' }); + + expect(panel1.classes()).not.toContain('t-collapse-panel--active'); + expect(panel2.classes()).not.toContain('t-collapse-panel--active'); + + defaultExpandAll.value = true; + await wrapper.vm.$nextTick(); + + expect(panel1.classes()).toContain('t-collapse-panel--active'); + expect(panel2.classes()).toContain('t-collapse-panel--active'); + }); + + test('default-value', async () => { + const wrapper = mount({ + setup() { + return () => ( + + + + + ); + }, + }); + + const panel1 = wrapper.findComponent({ ref: '1' }); + const panel2 = wrapper.findComponent({ ref: '2' }); + + expect(panel1.classes()).toContain('t-collapse-panel--active'); + expect(panel2.classes()).not.toContain('t-collapse-panel--active'); + }); + + test('value', async () => { + const val = ref([]); + const wrapper = mount({ + setup() { + return () => ( + + + + + ); + }, + }); + + const panel1 = wrapper.findComponent({ ref: '1' }); + const panel2 = wrapper.findComponent({ ref: '2' }); + await panel1.find('.t-collapse-panel__header').trigger('click'); + + expect(val.value).toHaveLength(1); + expect(val.value).toContain('1'); + + await panel2.find('.t-collapse-panel__header').trigger('click'); + + expect(val.value).toHaveLength(2); + expect(val.value).toContain('2'); + }); + + test('expandMutex', async () => { + const val = ref([]); + const wrapper = mount({ + setup() { + return () => ( + + + + + ); + }, + }); + + const panel1 = wrapper.findComponent({ ref: '1' }); + const panel2 = wrapper.findComponent({ ref: '2' }); + await panel1.find('.t-collapse-panel__header').trigger('click'); + + expect(val.value).toHaveLength(1); + expect(val.value).toContain('1'); + + await panel2.find('.t-collapse-panel__header').trigger('click'); + + expect(val.value).toHaveLength(1); + expect(val.value).toContain('2'); + }); + + test('onChange', async () => { + const handleChange = vi.fn(); + const wrapper = mount({ + setup() { + return () => ( + + + + ); + }, + }); + + const panel = wrapper.findComponent({ ref: '1' }); + await panel.find('.t-collapse-panel__header').trigger('click') + + expect(handleChange).toHaveBeenCalled(); + }); + + test('disabled', async () => { + const disabled = ref(false); + const handleChange = vi.fn(); + const wrapper = mount({ + setup() { + return () => ( + + + + ); + }, + }); + + const panel = wrapper.findComponent({ ref: '1' }); + await panel.find('.t-collapse-panel__header').trigger('click'); + + expect(handleChange).toHaveBeenCalled(); + + disabled.value = true; + await wrapper.vm.$nextTick(); + await panel.find('.t-collapse-panel__header').trigger('click'); + + expect(handleChange).toHaveBeenCalledTimes(1); + }); + }); + + describe('collapse-panel props', () => { + test('header and content', async () => { + const wrapper = mount({ + setup() { + return () => ( + + + + ); + }, + }); + + const panel = wrapper.findComponent({ ref: '1' }); + + expect(panel.find('.t-collapse-panel__title').text()).toBe('标题'); + expect(panel.find('.t-collapse-panel__body').text()).toBe('内容'); + }); + + test('disabled', async () => { + const handleChange = vi.fn(); + const wrapper = mount({ + setup() { + return () => ( + + + + ); + }, + }); + + const panel = wrapper.findComponent({ ref: '1' }); + + await panel.find('.t-collapse-panel__header').trigger('click') + + expect(handleChange).not.toHaveBeenCalled(); + }); + + test('headerRightContent', async () => { + const wrapper = mount({ + setup() { + return () => ( + + + + ); + }, + }); + + const panel = wrapper.findComponent({ ref: '1' }); + + expect(panel.find('.t-collapse-panel__header-right').text()).toBe('操作'); + }); + + test('slot', async () => { + const wrapper = mount({ + setup() { + return () => ( + + + {{ + header: () =>

标题

, + headerRightContent: () => 操作, + default: () =>
内容
, + }} +
+
+ ); + }, + }); + + const panel = wrapper.findComponent({ ref: '1' }); + + expect(panel.find('.t-collapse-panel__title > h4').html()).toBe('

标题

'); + expect(panel.find('.t-collapse-panel__header-extra > span').html()).toBe('操作'); + expect(panel.find('.t-collapse-panel__body > div').html()).toBe('
内容
'); + }); + }); +});