2021-08-29 12:33:35 +08:00
|
|
|
import { ref, h, nextTick } from 'vue'
|
2020-07-27 18:58:35 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2021-12-04 11:20:06 +08:00
|
|
|
import { Loading, Search } from '@element-plus/icons-vue'
|
2020-08-04 11:20:52 +08:00
|
|
|
import Button from '../src/button.vue'
|
|
|
|
import ButtonGroup from '../src/button-group.vue'
|
2020-07-24 17:26:08 +08:00
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
2020-07-24 16:44:56 +08:00
|
|
|
describe('Button.vue', () => {
|
2020-07-27 18:58:35 +08:00
|
|
|
it('create', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { type: 'primary' },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('el-button--primary')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('icon', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2021-10-27 23:17:13 +08:00
|
|
|
props: { icon: Search },
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
2021-10-27 23:17:13 +08:00
|
|
|
expect(wrapper.findComponent(Search).exists()).toBeTruthy()
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('nativeType', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { nativeType: 'submit' },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.attributes('type')).toBe('submit')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('loading', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { loading: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-loading')
|
2021-10-27 23:17:13 +08:00
|
|
|
expect(wrapper.findComponent(Loading).exists()).toBeTruthy()
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('size', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2021-12-28 19:38:23 +08:00
|
|
|
props: { size: 'large' },
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
2021-12-28 19:38:23 +08:00
|
|
|
expect(wrapper.classes()).toContain('el-button--large')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('plain', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { plain: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-plain')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('round', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { round: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-round')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('circle', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { circle: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-circle')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
|
2020-07-24 17:26:08 +08:00
|
|
|
test('render text', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-24 17:26:08 +08:00
|
|
|
slots: {
|
2020-07-27 16:17:41 +08:00
|
|
|
default: AXIOM,
|
2020-07-24 17:26:08 +08:00
|
|
|
},
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.text()).toEqual(AXIOM)
|
2020-07-24 17:26:08 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('handle click', async () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-24 17:26:08 +08:00
|
|
|
slots: {
|
2020-07-27 16:17:41 +08:00
|
|
|
default: AXIOM,
|
2020-07-24 17:26:08 +08:00
|
|
|
},
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect(wrapper.emitted()).toBeDefined()
|
2020-07-24 16:44:56 +08:00
|
|
|
})
|
2020-07-27 18:58:35 +08:00
|
|
|
|
|
|
|
test('handle click inside', async () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
slots: {
|
|
|
|
default: '<span class="inner-slot"></span>',
|
|
|
|
},
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
await (<HTMLElement>wrapper.element.querySelector('.inner-slot')).click()
|
|
|
|
expect(wrapper.emitted()).toBeDefined()
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('loading implies disabled', async () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
slots: {
|
|
|
|
default: AXIOM,
|
|
|
|
},
|
|
|
|
props: { loading: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect(wrapper.emitted('click')).toBeUndefined()
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
|
2020-07-27 19:18:09 +08:00
|
|
|
it('disabled', async () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 19:18:09 +08:00
|
|
|
props: { disabled: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-disabled')
|
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect(wrapper.emitted('click')).toBeUndefined()
|
2020-07-27 19:18:09 +08:00
|
|
|
})
|
2020-07-24 16:44:56 +08:00
|
|
|
})
|
2020-08-04 11:20:52 +08:00
|
|
|
describe('Button Group', () => {
|
|
|
|
it('create', () => {
|
2021-08-29 12:33:35 +08:00
|
|
|
const wrapper = mount({
|
2021-10-24 22:47:39 +08:00
|
|
|
template: `
|
|
|
|
<el-button-group>
|
2021-08-29 12:33:35 +08:00
|
|
|
<el-button type="primary">Prev</el-button>
|
|
|
|
<el-button type="primary">Next</el-button>
|
2021-10-24 22:47:39 +08:00
|
|
|
</el-button-group>`,
|
2021-08-29 12:33:35 +08:00
|
|
|
components: {
|
|
|
|
'el-button-group': ButtonGroup,
|
|
|
|
'el-button': Button,
|
|
|
|
},
|
|
|
|
})
|
2020-08-04 11:20:52 +08:00
|
|
|
expect(wrapper.classes()).toContain('el-button-group')
|
|
|
|
expect(wrapper.findAll('button').length).toBe(2)
|
|
|
|
})
|
2021-08-29 12:33:35 +08:00
|
|
|
|
|
|
|
it('button group reactive size', async () => {
|
|
|
|
const size = ref('small')
|
|
|
|
const wrapper = mount({
|
2021-09-04 19:29:28 +08:00
|
|
|
setup() {
|
|
|
|
return () =>
|
|
|
|
h(ButtonGroup, { size: size.value }, () => [
|
|
|
|
h(Button, { type: 'primary' }, () => 'Prev'),
|
|
|
|
h(Button, { type: 'primary' }, () => 'Next'),
|
|
|
|
])
|
2021-08-29 12:33:35 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(wrapper.classes()).toContain('el-button-group')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
wrapper.findAll('.el-button-group button.el-button--small').length
|
|
|
|
).toBe(2)
|
2021-08-29 12:33:35 +08:00
|
|
|
|
2021-12-28 19:38:23 +08:00
|
|
|
size.value = 'large'
|
2021-08-29 12:33:35 +08:00
|
|
|
await nextTick()
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
2021-12-28 19:38:23 +08:00
|
|
|
wrapper.findAll('.el-button-group button.el-button--large').length
|
2021-09-04 19:29:28 +08:00
|
|
|
).toBe(2)
|
2021-08-29 12:33:35 +08:00
|
|
|
})
|
2021-09-28 20:51:08 +08:00
|
|
|
|
|
|
|
it('button group type', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
setup() {
|
|
|
|
return () =>
|
|
|
|
h(ButtonGroup, { type: 'warning' }, () => [
|
|
|
|
h(Button, { type: 'primary' }, () => 'Prev'),
|
|
|
|
h(Button, {}, () => 'Next'),
|
|
|
|
])
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(wrapper.classes()).toContain('el-button-group')
|
|
|
|
expect(
|
|
|
|
wrapper.findAll('.el-button-group button.el-button--primary').length
|
|
|
|
).toBe(1)
|
|
|
|
expect(
|
|
|
|
wrapper.findAll('.el-button-group button.el-button--warning').length
|
|
|
|
).toBe(1)
|
|
|
|
})
|
2021-10-24 22:47:39 +08:00
|
|
|
|
|
|
|
it('add space in two Chinese characters', async () => {
|
|
|
|
const wrapper = mount(Button, {
|
|
|
|
slots: {
|
|
|
|
default: '中文',
|
|
|
|
},
|
2021-11-10 15:21:20 +08:00
|
|
|
props: {
|
|
|
|
autoInsertSpace: true,
|
|
|
|
},
|
2021-10-24 22:47:39 +08:00
|
|
|
})
|
|
|
|
expect(wrapper.find('.el-button span').text()).toBe('中文')
|
|
|
|
expect(wrapper.find('.el-button span').classes()).toContain(
|
|
|
|
'el-button__text--expand'
|
|
|
|
)
|
|
|
|
})
|
2020-08-04 11:20:52 +08:00
|
|
|
})
|