2019-01-12 03:33:27 +00:00
|
|
|
import Button from '../index';
|
|
|
|
import Icon from '../../icon';
|
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import Vue from 'vue';
|
|
|
|
import { asyncExpect } from '@/tests/utils';
|
2020-03-07 11:45:13 +00:00
|
|
|
import { sleep } from '../../../tests/utils';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2018-05-13 15:11:51 +00:00
|
|
|
|
|
|
|
describe('Button', () => {
|
2020-03-07 11:45:13 +00:00
|
|
|
mountTest(Button);
|
|
|
|
mountTest(Button.Group);
|
2018-05-14 01:49:26 +00:00
|
|
|
it('renders correctly', () => {
|
2018-09-05 13:28:54 +00:00
|
|
|
const wrapper = mount({
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
return <Button>Follow</Button>;
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
2018-05-14 01:49:26 +00:00
|
|
|
|
2018-05-13 15:11:51 +00:00
|
|
|
it('create primary button', () => {
|
|
|
|
const wrapper = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Button type="primary">按钮</Button>;
|
2018-05-13 15:11:51 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
expect(wrapper.contains('.ant-btn-primary')).toBe(true);
|
|
|
|
});
|
2018-05-13 15:11:51 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
it('renders Chinese characters correctly', done => {
|
|
|
|
const wrapper = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Button>按钮</Button>;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(wrapper.text()).toBe('按 钮');
|
2018-05-13 15:11:51 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper1 = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Button icon="search">按钮</Button>;
|
|
|
|
},
|
|
|
|
});
|
2018-05-13 15:11:51 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper1.html()).toMatchSnapshot();
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper2 = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return (
|
|
|
|
<Button>
|
|
|
|
<Icon type="search" />
|
|
|
|
按钮
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(wrapper2.html()).toMatchSnapshot();
|
2018-09-05 13:28:54 +00:00
|
|
|
// should not insert space when there is icon
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper3 = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Button icon="search">按钮</Button>;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(wrapper3.html()).toMatchSnapshot();
|
2018-09-05 13:28:54 +00:00
|
|
|
// should not insert space when there is icon while loading
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper4 = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return (
|
|
|
|
<Button icon="search" loading>
|
|
|
|
按钮
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(wrapper4.html()).toMatchSnapshot();
|
2018-09-05 13:28:54 +00:00
|
|
|
// should insert space while loading
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper5 = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Button loading>按钮</Button>;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(wrapper5.html()).toMatchSnapshot();
|
|
|
|
const wrapper6 = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return (
|
|
|
|
<Button>
|
|
|
|
<span>按钮</span>
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2018-05-14 01:49:26 +00:00
|
|
|
Vue.nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper6.find('.ant-btn').contains('.ant-btn-two-chinese-chars')).toBe(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
it('should change loading state instantly by default', async () => {
|
2018-05-14 01:49:26 +00:00
|
|
|
const DefaultButton = {
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2018-05-14 01:49:26 +00:00
|
|
|
return {
|
|
|
|
loading: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
enterLoading() {
|
|
|
|
this.loading = true;
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
|
|
|
},
|
2018-05-13 15:11:51 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Button loading={this.loading} onClick={this.enterLoading}>
|
|
|
|
Button
|
|
|
|
</Button>
|
|
|
|
);
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(DefaultButton, { sync: false });
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.trigger('click');
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.findAll('.ant-btn-loading').length).toBe(1);
|
|
|
|
});
|
|
|
|
});
|
2018-05-13 15:11:51 +00:00
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
it('should change loading state with delay', async () => {
|
2018-05-14 01:49:26 +00:00
|
|
|
const DefaultButton = {
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2018-05-14 01:49:26 +00:00
|
|
|
return {
|
|
|
|
loading: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
enterLoading() {
|
|
|
|
this.loading = { delay: 1000 };
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
|
|
|
},
|
2018-05-13 15:11:51 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Button loading={this.loading} onClick={this.enterLoading}>
|
|
|
|
Button
|
|
|
|
</Button>
|
|
|
|
);
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(DefaultButton, { sync: false });
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.trigger('click');
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.contains('.ant-btn-loading')).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
it('should not clickable when button is loading', () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
const wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Button loading onClick={onClick}>
|
|
|
|
button
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
wrapper.trigger('click');
|
|
|
|
expect(onClick).not.toHaveBeenCalledWith();
|
|
|
|
});
|
2018-05-14 01:49:26 +00:00
|
|
|
it('should support link button', () => {
|
|
|
|
const wrapper = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return (
|
|
|
|
<Button target="_blank" href="http://ant.design">
|
|
|
|
link button
|
|
|
|
</Button>
|
|
|
|
);
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
2018-05-13 15:11:51 +00:00
|
|
|
|
2018-05-14 01:49:26 +00:00
|
|
|
it('fixbug renders {0} , 0 and {false}', () => {
|
|
|
|
const wrapper = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Button>{0}</Button>;
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
2018-05-13 15:11:51 +00:00
|
|
|
|
2018-05-14 01:49:26 +00:00
|
|
|
const wrapper1 = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Button>0</Button>;
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
expect(wrapper1.html()).toMatchSnapshot();
|
2018-05-13 15:11:51 +00:00
|
|
|
|
2018-05-14 01:49:26 +00:00
|
|
|
const wrapper2 = mount({
|
2019-02-01 09:23:00 +00:00
|
|
|
render() {
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Button>{false}</Button>;
|
2018-05-14 01:49:26 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
expect(wrapper2.html()).toMatchSnapshot();
|
|
|
|
});
|
2019-01-02 12:18:13 +00:00
|
|
|
|
|
|
|
it('should not render as link button when href is undefined', async () => {
|
|
|
|
const wrapper = mount({
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2019-01-02 12:18:13 +00:00
|
|
|
return (
|
2019-01-12 03:33:27 +00:00
|
|
|
<Button type="primary" href={undefined}>
|
|
|
|
button
|
|
|
|
</Button>
|
|
|
|
);
|
2019-01-02 12:18:13 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
it('should support to change loading', async () => {
|
|
|
|
const wrapper = mount(Button, {
|
|
|
|
slots: {
|
|
|
|
default: ['button'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
wrapper.setProps({ loading: true });
|
|
|
|
expect(wrapper.findAll('.ant-btn-loading').length).toBe(1);
|
|
|
|
wrapper.setProps({ loading: false });
|
|
|
|
expect(wrapper.findAll('.ant-btn-loading').length).toBe(0);
|
|
|
|
wrapper.setProps({ loading: { delay: 50 } });
|
|
|
|
expect(wrapper.findAll('.ant-btn-loading').length).toBe(0);
|
|
|
|
await sleep(50);
|
|
|
|
expect(wrapper.findAll('.ant-btn-loading').length).toBe(1);
|
|
|
|
wrapper.setProps({ loading: false });
|
|
|
|
await sleep(50);
|
|
|
|
expect(wrapper.findAll('.ant-btn-loading').length).toBe(0);
|
|
|
|
expect(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|