Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(collapse): add unit test #331

Merged
merged 4 commits into from
Sep 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 225 additions & 0 deletions src/collapse/__test__/index.test.jsx
Original file line number Diff line number Diff line change
@@ -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 () => (
<Collapse defaultExpandAll={defaultExpandAll}>
<CollapsePanel ref="1" value="1" header="标题1" content="内容1" />
<CollapsePanel ref="2" value="2" header="标题2" content="内容2" />
</Collapse>
);
},
});

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 () => (
<Collapse default-value={['1']}>
<CollapsePanel ref="1" value="1" header="标题1" content="内容1" />
<CollapsePanel ref="2" value="2" header="标题2" content="内容2" />
</Collapse>
);
},
});

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 () => (
<Collapse v-model={val.value}>
<CollapsePanel ref="1" value="1" header="标题1" content="内容1" />
<CollapsePanel ref="2" value="2" header="标题2" content="内容2" />
</Collapse>
);
},
});

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 () => (
<Collapse expandMutex v-model={val.value}>
<CollapsePanel ref="1" value="1" header="标题1" content="内容1" />
<CollapsePanel ref="2" value="2" header="标题2" content="内容2" />
</Collapse>
);
},
});

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 () => (
<Collapse onChange={handleChange}>
<CollapsePanel ref="1" value="1" header="标题" content="内容" />
</Collapse>
);
},
});

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 () => (
<Collapse disabled={disabled.value} onChange={handleChange}>
<CollapsePanel ref="1" value="1" header="标题" content="内容" />
</Collapse>
);
},
});

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 () => (
<Collapse>
<CollapsePanel ref="1" value="1" header="标题" content="内容" />
</Collapse>
);
},
});

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 () => (
<Collapse onChange={handleChange}>
<CollapsePanel ref="1" value="1" disabled header="标题" content="内容" />
</Collapse>
);
},
});

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 () => (
<Collapse>
<CollapsePanel ref="1" value="1" headerRightContent="操作" header="标题" content="内容" />
</Collapse>
);
},
});

const panel = wrapper.findComponent({ ref: '1' });

expect(panel.find('.t-collapse-panel__header-right').text()).toBe('操作');
});

test('slot', async () => {
const wrapper = mount({
setup() {
return () => (
<Collapse>
<CollapsePanel ref="1" value="1">
{{
header: () => <h4>标题</h4>,
headerRightContent: () => <span>操作</span>,
default: () => <div>内容</div>,
}}
</CollapsePanel>
</Collapse>
);
},
});

const panel = wrapper.findComponent({ ref: '1' });

expect(panel.find('.t-collapse-panel__title > h4').html()).toBe('<h4>标题</h4>');
expect(panel.find('.t-collapse-panel__header-extra > span').html()).toBe('<span>操作</span>');
expect(panel.find('.t-collapse-panel__body > div').html()).toBe('<div>内容</div>');
});
});
});