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(Dropdown): add test for Dropdown #1516

Merged
merged 3 commits into from
Sep 22, 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
179 changes: 179 additions & 0 deletions src/dropdown/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { mount } from '@vue/test-utils';
import { jest, expect, it } from '@jest/globals';
import { Dropdown, DropdownMenu, DropdownItem } from '../index';

// every component needs four parts: props/events/slots/functions.
describe('Dropdown', () => {
const props = {
options: [
{
content: '操作一',
value: 1,
},
{
content: '操作二',
value: 2,
},
{
content: '操作三',
value: 3,
},
],
};

it('equals disable to true', () => {
const wrapper = mount(Dropdown, {
propsData: {
disabled: true,
},
});

expect(wrapper.props().disabled).toEqual(true);
});

it('should emit click event', () => {
const onClick = jest.fn();
const dropdownWrapper = mount({
render() {
return <Dropdown options={props.options} onClick={onClick}></Dropdown>;
},
}).findComponent(Dropdown);
const wrapper = mount({
provide() {
return {
dropdown: dropdownWrapper.vm,
};
},
render() {
return <DropdownMenu></DropdownMenu>;
},
});
wrapper.find('.t-dropdown__item').trigger('click');
expect(onClick).toBeCalled();
});

it('should exits divider', () => {
const options = [
{
content: '操作一',
value: 1,
},
{
content: '操作二',
value: 2,
divider: true,
},
{
content: '操作三',
value: 3,
},
];
const dropdownWrapper = mount({
render() {
return <Dropdown options={options}></Dropdown>;
},
}).findComponent(Dropdown);
const wrapper = mount({
provide() {
return {
dropdown: dropdownWrapper.vm,
};
},
render() {
return <DropdownMenu></DropdownMenu>;
},
});
expect(wrapper.find('.t-divider').element.parentElement.textContent).toBe(options[1].content);
});

it('should equals custom height', () => {
const options = Array.from({ length: 20 }).map((v, k) => ({
content: `操作${k + 1}`,
value: k + 1,
}));
const maxHeight = 400;
const minColumnWidth = 88;
const dropdownWrapper = mount({
render() {
return <Dropdown options={options} maxHeight={maxHeight} minColumnWidth={minColumnWidth}></Dropdown>;
},
}).findComponent(Dropdown);
const wrapper = mount({
provide() {
return {
dropdown: dropdownWrapper.vm,
};
},
render() {
return <DropdownMenu></DropdownMenu>;
},
}).findComponent(DropdownMenu);
expect(wrapper.find('.t-dropdown__menu-column').exists()).toBe(true);
expect(wrapper.find('.t-dropdown__menu-column').element.style.maxHeight).toBe(`${maxHeight}px`);
expect(wrapper.find('.t-dropdown__menu-column').element.style.minWidth).toBe(`${minColumnWidth}px`);
});

it('should handle each option of click event', () => {
const options = [
{
content: '操作一',
value: 1,
onClick: jest.fn(),
},
{
content: '操作二',
value: 2,
onClick: jest.fn(),
},
{
content: '操作三',
value: 3,
onClick: jest.fn(),
},
];
const dropdownWrapper = mount({
render() {
return <Dropdown options={options}></Dropdown>;
},
}).findComponent(Dropdown);
const wrapper = mount({
provide() {
return {
dropdown: dropdownWrapper.vm,
};
},
render() {
return <DropdownMenu></DropdownMenu>;
},
}).findComponent(DropdownMenu);
wrapper.findAll('.t-dropdown__item-text').trigger('click');
options.forEach((value) => {
expect(value.onClick).toBeCalled();
});
});

it('use dropdown by slots', async () => {
const wrapper = mount({
render() {
return (
<Dropdown trigger="click">
<div>menu</div>
<template slot="dropdown">
<DropdownMenu>
<DropdownItem class="op1" value={1}>
操作一
</DropdownItem>
<DropdownItem value={2}>操作二</DropdownItem>
<DropdownItem value={3}>操作三</DropdownItem>
<DropdownItem value={4}>操作四</DropdownItem>
<DropdownItem value={5}>操作五</DropdownItem>
</DropdownMenu>
</template>
</Dropdown>
);
},
});
await wrapper.trigger('click');
expect(document.querySelector('.op1')).not.toBe(null);
});
});