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

137 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-07-27 18:58:35 +08:00
import { mount } from '@vue/test-utils'
2020-07-24 17:26:08 +08:00
import Button from '../src/index.vue'
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', () => {
const instance = mount(Button, {
props: { type: 'primary' },
...COMMON_CONFIG,
})
const buttonElm = instance.element
expect(buttonElm.classList.contains('el-button--primary')).toBeTruthy()
})
it('icon', () => {
const instance = mount(Button, {
props: { icon: 'el-icon-search' },
...COMMON_CONFIG,
})
const buttonElm = instance.element
expect(buttonElm.querySelector('.el-icon-search')).not.toBeUndefined()
})
it('nativeType', () => {
const instance = mount(Button, {
props: { nativeType: 'submit' },
...COMMON_CONFIG,
})
const buttonElm = instance.element
expect(buttonElm.getAttribute('type')).toBe('submit')
})
it('loading', () => {
const instance = mount(Button, {
props: { loading: true },
...COMMON_CONFIG,
})
const buttonElm = instance.element
expect(buttonElm.querySelector('.el-icon-search')).not.toBeUndefined()
expect(buttonElm.classList.contains('is-loading')).toBeTruthy()
expect(buttonElm.querySelector('.el-icon-loading')).not.toBeUndefined()
})
it('disabled', () => {
const instance = mount(Button, {
props: { disabled: true },
...COMMON_CONFIG,
})
const buttonElm = instance.element
expect(buttonElm.classList.contains('is-disabled')).toBeTruthy()
})
it('size', () => {
const instance = mount(Button, {
props: { size: 'medium' },
...COMMON_CONFIG,
})
const buttonElm = instance.element
expect(buttonElm.classList.contains('el-button--medium')).toBeTruthy()
})
it('plain', () => {
const instance = mount(Button, {
props: { plain: true },
...COMMON_CONFIG,
})
const buttonElm = instance.element
expect(buttonElm.classList.contains('is-plain')).toBeTruthy()
})
it('round', () => {
const instance = mount(Button, {
props: { round: true },
...COMMON_CONFIG,
})
const buttonElm = instance.element
expect(buttonElm.classList.contains('is-round')).toBeTruthy()
})
it('circle', () => {
const instance = mount(Button, {
props: { circle: true },
...COMMON_CONFIG,
})
const buttonElm = instance.element
expect(buttonElm.classList.contains('is-circle')).toBeTruthy()
})
2020-07-24 17:26:08 +08:00
test('render text', () => {
const instance = mount(Button, {
slots: {
default: AXIOM,
2020-07-24 17:26:08 +08:00
},
...COMMON_CONFIG,
})
expect(instance.text()).toEqual(AXIOM)
})
test('handle click', async () => {
const instance = mount(Button, {
slots: {
default: AXIOM,
2020-07-24 17:26:08 +08:00
},
...COMMON_CONFIG,
})
await instance.trigger('click')
expect(instance.emitted()).toBeDefined()
2020-07-24 16:44:56 +08:00
})
2020-07-27 18:58:35 +08:00
test('handle click inside', async () => {
const instance = mount(Button, {
slots: {
default: '<span class="inner-slot"></span>',
},
...COMMON_CONFIG,
})
await (<HTMLElement>instance.element.querySelector('.inner-slot')).click()
expect(instance.emitted()).toBeDefined()
})
test('loading implies disabled', async () => {
const instance = mount(Button, {
slots: {
default: AXIOM,
},
props: { loading: true },
...COMMON_CONFIG,
})
await instance.trigger('click')
expect(instance.emitted('click')).toBeUndefined()
})
2020-07-24 16:44:56 +08:00
})