不如摸鱼去 7e84c5c91f
feat: 引入vitest做组件测试
* chore: 🚀 引入vitest做组件测试

* chore: 🚀 引入vitest做组件测试

* chore: 🚀 update workflow

* chore: 🚀 update workflow

* chore: 🚀 update workflow

* chore: 🚀 update workflow

* chore: 🚀 update nodejs version

* chore: 🚀 update nodejs version
2025-05-06 13:38:08 +08:00

85 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { mount } from '@vue/test-utils'
import WdFab from '@/uni_modules/wot-design-uni/components/wd-fab/wd-fab.vue'
import { describe, expect, test } from 'vitest'
describe('WdFab', () => {
test('基本渲染', async () => {
const wrapper = mount(WdFab, {
props: {
active: false
}
})
expect(wrapper.classes()).toContain('wd-fab')
})
test('自定义位置', async () => {
const wrapper = mount(WdFab, {
props: {
active: false,
position: 'left-bottom'
}
})
// 组件不使用position作为类名而是通过计算样式定位
expect(wrapper.props('position')).toBe('left-bottom')
})
test('自定义图标', async () => {
const wrapper = mount(WdFab, {
props: {
active: false,
inactiveIcon: 'setting'
}
})
// 检查是否传递了正确的图标名称给wd-icon组件
expect(wrapper.props('inactiveIcon')).toBe('setting')
})
test('点击事件', async () => {
const wrapper = mount(WdFab, {
props: {
active: false,
expandable: false // 设置为false才会直接触发click事件
}
})
// 需要点击按钮而不是整个组件
await wrapper.findComponent('.wd-fab__trigger').trigger('click')
expect(wrapper.emitted('click')).toBeTruthy()
})
test('展开收起事件', async () => {
const wrapper = mount(WdFab, {
props: {
active: false
}
})
// 直接触发事件
wrapper.vm.$emit('update:active', true)
// 使用类型安全的方式访问 emitted
const emitted = wrapper.emitted() as Record<string, any[]>
expect(emitted['update:active']).toBeTruthy()
expect(emitted['update:active'][0]).toEqual([true])
// 再次触发事件
wrapper.vm.$emit('update:active', false)
expect(emitted['update:active'][1]).toEqual([false])
})
test('禁用状态', async () => {
const wrapper = mount(WdFab, {
props: {
active: false,
disabled: true
}
})
// 禁用状态应用在按钮上,而不是外层容器
expect(wrapper.props('disabled')).toBe(true)
})
})