概述
- button 組件在組件庫中相對來說算是一個比較簡單且獨(dú)立的組件
- 創(chuàng)建
packages/components/Button
目錄存放按鈕組件
詳情
- packages/components/Button/Button.vue
<template>
<button style="background-color: blue; color: red">button</button>
</template>
<script setup lang="ts">
defineOptions({
name: 'LxxButton',
})
</script>
- packages/components/Button/index.ts
import Button from './Button.vue'
import { withInstall } from '@lxx-ui/utils'
// 將組件轉(zhuǎn)換成插件
export const LxxButton = withInstall(Button)
- packages/components/Button/Button.test.tsx
import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import Button from './Button.vue'
describe('Button.vue', () => {
// Props: type
it('should has the correct type class when type prop is set', () => {
const types = ['primary', 'success', 'warning', 'danger', 'info']
types.forEach((type) => {
const wrapper = mount(Button, {
props: { type: type as any },
})
expect(wrapper.classes()).toContain(`er-button--${type}`)
})
})
// Props: size
it('should has the correct size class when size prop is set', () => {
const sizes = ['large', 'default', 'small']
sizes.forEach((size) => {
const wrapper = mount(Button, {
props: { size: size as any },
})
expect(wrapper.classes()).toContain(`er-button--${size}`)
})
})
// Props: plain, round, circle
it.each([
['plain', 'is-plain'],
['round', 'is-round'],
['circle', 'is-circle'],
['disabled', 'is-disabled'],
['loading', 'is-loading'],
])(
'should has the correct class when prop %s is set to true',
(prop, className) => {
const wrapper = mount(Button, {
props: { [prop]: true },
global: {
stubs: ['ErIcon'],
},
})
expect(wrapper.classes()).toContain(className)
}
)
it('should has the correct native type attribute when native-type prop is set', () => {
const wrapper = mount(Button, {
props: { nativeType: 'submit' },
})
expect(wrapper.element.tagName).toBe('BUTTON')
expect((wrapper.element as any).type).toBe('submit')
})
// Props: tag
it('should renders the custom tag when tag prop is set', () => {
const wrapper = mount(Button, {
props: { tag: 'a' },
})
expect(wrapper.element.tagName.toLowerCase()).toBe('a')
})
// Events: click
it('should emits a click event when the button is clicked', async () => {
const wrapper = mount(Button, {})
await wrapper.trigger('click')
expect(wrapper.emitted().click).toHaveLength(1)
})
})