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

149 lines
3.6 KiB
TypeScript
Raw Normal View History

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'
const COMMON_CONFIG = {
global: {
provide: {
elForm: {},
elFormItem: {},
},
},
2020-07-24 17:26:08 +08:00
}
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' },
...COMMON_CONFIG,
})
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' },
...COMMON_CONFIG,
})
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' },
...COMMON_CONFIG,
})
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 },
...COMMON_CONFIG,
})
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' },
...COMMON_CONFIG,
})
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 },
...COMMON_CONFIG,
})
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 },
...COMMON_CONFIG,
})
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 },
...COMMON_CONFIG,
})
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
},
...COMMON_CONFIG,
})
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
},
...COMMON_CONFIG,
})
2020-07-27 21:32:08 +08:00
await wrapper.trigger('click')
expect(wrapper.emitted()).toBeDefined()
2020-07-24 17:26:08 +08:00
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>',
},
...COMMON_CONFIG,
})
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 },
...COMMON_CONFIG,
})
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 },
...COMMON_CONFIG,
})
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', () => {
const TestComponent = {
template: `<el-button-group>
<el-button type="primary"></el-button>
<el-button type="primary"></el-button>
</el-button-group>`,
components: {
'el-button-group': ButtonGroup,
'el-button': Button,
},
}
it('create', () => {
const wrapper = mount(TestComponent)
expect(wrapper.classes()).toContain('el-button-group')
expect(wrapper.findAll('button').length).toBe(2)
})
})