element-plus/packages/components/button/__tests__/button.spec.ts

156 lines
4.1 KiB
TypeScript
Raw Normal View History

import { ref, h, nextTick } from 'vue'
2020-07-27 18:58:35 +08:00
import { mount } from '@vue/test-utils'
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, {
2020-07-27 18:58:35 +08:00
props: { icon: 'el-icon-search' },
})
2020-07-27 21:32:08 +08:00
expect(wrapper.find('.el-icon-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')
expect(wrapper.find('.el-icon-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, {
2020-07-27 18:58:35 +08:00
props: { size: 'medium' },
})
2020-07-27 21:32:08 +08:00
expect(wrapper.classes()).toContain('el-button--medium')
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: {
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: {
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', () => {
const wrapper = mount({
template: `<el-button-group>
<el-button type="primary">Prev</el-button>
<el-button type="primary">Next</el-button>
</el-button-group>`,
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)
})
it('button group reactive size', async () => {
const size = ref('small')
const wrapper = mount({
setup() {
return () =>
h(ButtonGroup, { size: size.value }, () => [
h(Button, { type: 'primary' }, () => 'Prev'),
h(Button, { type: 'primary' }, () => 'Next'),
h(Button, { type: 'primary', size: 'mini' }, () => 'Mini'),
])
},
})
expect(wrapper.classes()).toContain('el-button-group')
expect(
wrapper.findAll('.el-button-group button.el-button--small').length
).toBe(2)
expect(
wrapper.findAll('.el-button-group button.el-button--mini').length
).toBe(1)
size.value = 'medium'
await nextTick()
expect(
wrapper.findAll('.el-button-group button.el-button--medium').length
).toBe(2)
expect(
wrapper.findAll('.el-button-group button.el-button--mini').length
).toBe(1)
})
2020-08-04 11:20:52 +08:00
})