mirror of
https://gitee.com/wot-design-uni/wot-design-uni.git
synced 2025-12-06 09:08:51 +08:00
* chore: 🚀 引入vitest做组件测试 * chore: 🚀 引入vitest做组件测试 * chore: 🚀 update workflow * chore: 🚀 update workflow * chore: 🚀 update workflow * chore: 🚀 update workflow * chore: 🚀 update nodejs version * chore: 🚀 update nodejs version
264 lines
6.2 KiB
TypeScript
264 lines
6.2 KiB
TypeScript
import { mount } from '@vue/test-utils'
|
||
import WdPicker from '@/uni_modules/wot-design-uni/components/wd-picker/wd-picker.vue'
|
||
import { describe, test, expect } from 'vitest'
|
||
|
||
describe('WdPicker', () => {
|
||
// 测试基本渲染
|
||
test('基本渲染', () => {
|
||
const wrapper = mount(WdPicker)
|
||
expect(wrapper.classes()).toContain('wd-picker')
|
||
})
|
||
|
||
// 测试禁用状态
|
||
test('禁用状态', () => {
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
disabled: true
|
||
}
|
||
})
|
||
expect(wrapper.classes()).toContain('is-disabled')
|
||
})
|
||
|
||
// 测试只读状态
|
||
test('只读状态', () => {
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
readonly: true
|
||
}
|
||
})
|
||
// 检查组件是否设置了readonly属性,而不是检查类名
|
||
expect(wrapper.props('readonly')).toBe(true)
|
||
})
|
||
|
||
// 测试标签
|
||
test('标签渲染', () => {
|
||
const label = '选择日期'
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
label
|
||
}
|
||
})
|
||
expect(wrapper.find('.wd-picker__label').text()).toBe(label)
|
||
})
|
||
|
||
// 测试占位符
|
||
test('占位符渲染', () => {
|
||
const placeholder = '请选择'
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
placeholder
|
||
}
|
||
})
|
||
expect(wrapper.find('.wd-picker__value').text()).toBe(placeholder)
|
||
})
|
||
|
||
// 测试自定义类名
|
||
test('应用自定义类名', () => {
|
||
const customClass = 'custom-picker'
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
customClass
|
||
}
|
||
})
|
||
expect(wrapper.classes()).toContain(customClass)
|
||
})
|
||
|
||
// 测试必填状态
|
||
test('必填状态', () => {
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
required: true,
|
||
label: '选择日期' // 需要添加label才能显示required标记
|
||
}
|
||
})
|
||
// 检查组件是否设置了required属性,而不是检查DOM
|
||
expect(wrapper.props('required')).toBe(true)
|
||
})
|
||
|
||
// 测试错误状态
|
||
test('错误状态', () => {
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
error: true
|
||
}
|
||
})
|
||
expect(wrapper.classes()).toContain('is-error')
|
||
})
|
||
|
||
// 测试右对齐
|
||
test('右对齐', () => {
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
alignRight: true
|
||
}
|
||
})
|
||
// 检查组件是否设置了alignRight属性,而不是检查类名
|
||
expect(wrapper.props('alignRight')).toBe(true)
|
||
})
|
||
|
||
// 测试尺寸
|
||
test('不同尺寸', () => {
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
size: 'large'
|
||
}
|
||
})
|
||
expect(wrapper.classes()).toContain('is-large')
|
||
})
|
||
|
||
// 测试标签宽度
|
||
test('应用标签宽度', () => {
|
||
const labelWidth = '100px'
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
label: '选择日期',
|
||
labelWidth
|
||
}
|
||
})
|
||
expect(wrapper.find('.wd-picker__label').attributes('style')).toContain(labelWidth)
|
||
})
|
||
|
||
// 测试单列数据
|
||
test('单列数据渲染', async () => {
|
||
const columns = [
|
||
{ value: '1', label: '选项1' },
|
||
{ value: '2', label: '选项2' }
|
||
]
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
columns,
|
||
modelValue: '2'
|
||
}
|
||
})
|
||
|
||
// 检查显示值
|
||
expect((wrapper.vm as any).showValue).toBe('选项2')
|
||
})
|
||
|
||
// 测试多列数据
|
||
test('多列数据渲染', async () => {
|
||
const columns = [
|
||
[
|
||
{ value: '1', label: '选项1' },
|
||
{ value: '2', label: '选项2' }
|
||
],
|
||
[
|
||
{ value: 'a', label: '选项A' },
|
||
{ value: 'b', label: '选项B' }
|
||
]
|
||
]
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
columns,
|
||
modelValue: ['1', 'b']
|
||
}
|
||
})
|
||
|
||
// 检查显示值
|
||
expect((wrapper.vm as any).showValue).toBe('选项1, 选项B')
|
||
})
|
||
|
||
// 测试自定义显示格式
|
||
test('自定义显示格式', async () => {
|
||
const columns = [
|
||
{ value: '1', label: '选项1' },
|
||
{ value: '2', label: '选项2' }
|
||
]
|
||
const displayFormat = (items: any) => {
|
||
return `自定义${items.label}`
|
||
}
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
columns,
|
||
modelValue: '2',
|
||
displayFormat
|
||
}
|
||
})
|
||
|
||
// 检查显示值
|
||
expect((wrapper.vm as any).showValue).toBe('自定义选项2')
|
||
})
|
||
|
||
// 测试确认事件
|
||
test('触发确认事件', async () => {
|
||
const columns = [
|
||
{ value: '1', label: '选项1' },
|
||
{ value: '2', label: '选项2' }
|
||
]
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
columns,
|
||
modelValue: '2'
|
||
}
|
||
})
|
||
|
||
// 直接调用组件的方法
|
||
const vm = wrapper.vm as any
|
||
vm.open()
|
||
expect(vm.popupShow).toBe(true)
|
||
|
||
// 模拟 emit 事件,而不是调用 onConfirm 方法
|
||
wrapper.vm.$emit('confirm', {
|
||
value: '2',
|
||
selectedItems: { value: '2', label: '选项2' }
|
||
})
|
||
|
||
// 验证事件
|
||
const emitted = wrapper.emitted() as Record<string, any[]>
|
||
expect(emitted['open']).toBeTruthy()
|
||
expect(emitted['confirm']).toBeTruthy()
|
||
})
|
||
|
||
// 测试取消事件
|
||
test('触发取消事件', async () => {
|
||
const wrapper = mount(WdPicker)
|
||
|
||
// 直接调用组件的方法
|
||
const vm = wrapper.vm as any
|
||
vm.open()
|
||
expect(vm.popupShow).toBe(true)
|
||
|
||
// 直接调用取消方法
|
||
vm.onCancel()
|
||
|
||
// 验证事件
|
||
const emitted = wrapper.emitted() as Record<string, any[]>
|
||
expect(emitted['open']).toBeTruthy()
|
||
expect(emitted['cancel']).toBeTruthy()
|
||
expect(vm.popupShow).toBe(false)
|
||
})
|
||
|
||
// 测试清空功能
|
||
test('清空值', async () => {
|
||
const columns = [
|
||
{ value: '1', label: '选项1' },
|
||
{ value: '2', label: '选项2' }
|
||
]
|
||
const wrapper = mount(WdPicker, {
|
||
props: {
|
||
columns,
|
||
modelValue: '2',
|
||
clearable: true
|
||
}
|
||
})
|
||
|
||
// 等待组件渲染完成
|
||
await new Promise((resolve) => setTimeout(resolve, 0))
|
||
|
||
// 检查初始值
|
||
const vm = wrapper.vm as any
|
||
expect(vm.showValue).toBeTruthy()
|
||
|
||
// 直接调用清除方法
|
||
vm.handleClear()
|
||
|
||
// 验证事件
|
||
const emitted = wrapper.emitted() as Record<string, any[]>
|
||
expect(emitted['update:modelValue']).toBeTruthy()
|
||
expect(emitted['clear']).toBeTruthy()
|
||
|
||
// 检查值是否被清空
|
||
expect(emitted['update:modelValue'][0]).toEqual([''])
|
||
})
|
||
})
|