mirror of
https://gitee.com/wot-design-uni/wot-design-uni.git
synced 2025-12-06 17:18:40 +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
342 lines
7.9 KiB
TypeScript
342 lines
7.9 KiB
TypeScript
import { mount } from '@vue/test-utils'
|
||
import WdText from '@/uni_modules/wot-design-uni/components/wd-text/wd-text.vue'
|
||
import { describe, test, expect, vi, beforeEach } from 'vitest'
|
||
import { TextType } from '@/uni_modules/wot-design-uni/components/wd-text/types'
|
||
|
||
describe('WdText', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
// 测试基本渲染
|
||
test('基本渲染', () => {
|
||
const wrapper = mount(WdText)
|
||
|
||
expect(wrapper.classes()).toContain('wd-text')
|
||
expect(wrapper.classes()).toContain('is-default') // 默认类型
|
||
})
|
||
|
||
// 测试不同类型
|
||
test('不同类型渲染', () => {
|
||
const types: TextType[] = ['default', 'primary', 'success', 'warning', 'error']
|
||
|
||
types.forEach((type) => {
|
||
const wrapper = mount(WdText, {
|
||
props: { type }
|
||
})
|
||
|
||
expect(wrapper.classes()).toContain(`is-${type}`)
|
||
})
|
||
})
|
||
|
||
// 测试无效类型
|
||
test('处理无效类型', () => {
|
||
// 模拟 console.error
|
||
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||
|
||
mount(WdText, {
|
||
props: { type: 'invalid' as any }
|
||
})
|
||
|
||
// 验证错误信息
|
||
expect(consoleErrorSpy).toHaveBeenCalled()
|
||
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('type must be one of'))
|
||
|
||
// 恢复 console.error
|
||
consoleErrorSpy.mockRestore()
|
||
})
|
||
|
||
// 测试文本内容
|
||
test('文本内容渲染', () => {
|
||
const text = 'Hello World'
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { text }
|
||
})
|
||
|
||
expect(wrapper.text()).toBe(text)
|
||
})
|
||
|
||
// 测试文本大小
|
||
test('自定义文本大小', () => {
|
||
const size = '20px'
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { size }
|
||
})
|
||
|
||
expect(wrapper.attributes('style')).toContain(`font-size: ${size}`)
|
||
})
|
||
|
||
// 测试文本颜色
|
||
test('自定义文本颜色', () => {
|
||
const color = '#ff0000'
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { color }
|
||
})
|
||
|
||
// 浏览器可能会将 #ff0000 转换为 rgb(255, 0, 0),所以我们只检查是否包含 color:
|
||
expect(wrapper.attributes('style')).toContain('color:')
|
||
})
|
||
|
||
// 测试粗体
|
||
test('粗体文本', () => {
|
||
const wrapper = mount(WdText, {
|
||
props: { bold: true }
|
||
})
|
||
|
||
expect(wrapper.classes()).toContain('is-bold')
|
||
})
|
||
|
||
// 测试文本装饰
|
||
test('文本装饰', () => {
|
||
const decoration = 'underline'
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { decoration }
|
||
})
|
||
|
||
expect(wrapper.attributes('style')).toContain(`text-decoration: ${decoration}`)
|
||
})
|
||
|
||
// 测试行高
|
||
test('行高设置', () => {
|
||
const lineHeight = '1.5'
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { lineHeight }
|
||
})
|
||
|
||
expect(wrapper.attributes('style')).toContain(`line-height: ${lineHeight}`)
|
||
})
|
||
|
||
// 测试行数限制
|
||
test('行数限制', () => {
|
||
const lines = 2
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { lines }
|
||
})
|
||
|
||
expect(wrapper.classes()).toContain(`is-lines-${lines}`)
|
||
})
|
||
|
||
// 测试点击事件
|
||
test('点击事件', async () => {
|
||
const wrapper = mount(WdText)
|
||
|
||
await wrapper.trigger('click')
|
||
|
||
const emitted = wrapper.emitted() as Record<string, any[]>
|
||
expect(emitted['click']).toBeTruthy()
|
||
expect(emitted['click'].length).toBe(1)
|
||
})
|
||
|
||
// 测试前缀
|
||
test('前缀渲染', () => {
|
||
const prefix = '¥'
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { prefix }
|
||
})
|
||
|
||
expect(wrapper.text()).toContain(prefix)
|
||
})
|
||
|
||
// 测试后缀
|
||
test('后缀渲染', () => {
|
||
const suffix = '元'
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { suffix }
|
||
})
|
||
|
||
expect(wrapper.text()).toContain(suffix)
|
||
})
|
||
|
||
// 测试前缀插槽
|
||
test('前缀插槽', () => {
|
||
const wrapper = mount(WdText, {
|
||
slots: {
|
||
prefix: '<span class="custom-prefix">前缀</span>'
|
||
}
|
||
})
|
||
|
||
expect(wrapper.find('.custom-prefix').exists()).toBe(true)
|
||
expect(wrapper.text()).toContain('前缀')
|
||
})
|
||
|
||
// 测试后缀插槽
|
||
test('后缀插槽', () => {
|
||
const wrapper = mount(WdText, {
|
||
slots: {
|
||
suffix: '<span class="custom-suffix">后缀</span>'
|
||
}
|
||
})
|
||
|
||
expect(wrapper.find('.custom-suffix').exists()).toBe(true)
|
||
expect(wrapper.text()).toContain('后缀')
|
||
})
|
||
|
||
// 测试自定义类名
|
||
test('自定义类名', () => {
|
||
const customClass = 'custom-text'
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { customClass }
|
||
})
|
||
|
||
expect(wrapper.classes()).toContain(customClass)
|
||
})
|
||
|
||
// 测试自定义样式
|
||
test('自定义样式', () => {
|
||
const customStyle = 'margin: 8px;'
|
||
|
||
const wrapper = mount(WdText, {
|
||
props: { customStyle }
|
||
})
|
||
|
||
expect(wrapper.attributes('style')).toContain(customStyle)
|
||
})
|
||
|
||
// 测试日期模式
|
||
test('日期格式化', () => {
|
||
const wrapper = mount(WdText, {
|
||
props: {
|
||
text: '1672531200000', // 2023-01-01 00:00:00
|
||
mode: 'date'
|
||
}
|
||
})
|
||
|
||
expect(wrapper.text()).toBe('2023-01-01')
|
||
})
|
||
|
||
// 测试价格模式
|
||
test('价格格式化', () => {
|
||
const wrapper = mount(WdText, {
|
||
props: {
|
||
text: '1234567.89',
|
||
mode: 'price'
|
||
}
|
||
})
|
||
|
||
expect(wrapper.text()).toBe('1,234,567.89')
|
||
})
|
||
|
||
// 测试手机号脱敏
|
||
test('手机号脱敏', () => {
|
||
const wrapper = mount(WdText, {
|
||
props: {
|
||
text: '13812345678',
|
||
mode: 'phone',
|
||
format: true
|
||
}
|
||
})
|
||
|
||
expect(wrapper.text()).toBe('138****5678')
|
||
})
|
||
|
||
// 测试姓名脱敏
|
||
test('姓名脱敏', () => {
|
||
const wrapper = mount(WdText, {
|
||
props: {
|
||
text: '张三丰',
|
||
mode: 'name',
|
||
format: true
|
||
}
|
||
})
|
||
|
||
expect(wrapper.text()).toBe('张**丰')
|
||
})
|
||
|
||
// 测试无效的脱敏模式
|
||
test('无效脱敏模式报错', () => {
|
||
// 模拟 console.error
|
||
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||
|
||
expect(() => {
|
||
mount(WdText, {
|
||
props: {
|
||
text: 'test',
|
||
mode: 'invalid' as any,
|
||
format: true
|
||
}
|
||
})
|
||
}).toThrow('mode must be one of phone or name for encryption')
|
||
|
||
// 恢复 console.error
|
||
consoleErrorSpy.mockRestore()
|
||
})
|
||
|
||
// 测试样式对象转换
|
||
test('样式对象转字符串', () => {
|
||
// 不再尝试导入 objToStyle 函数,而是直接测试结果
|
||
const wrapper = mount(WdText, {
|
||
props: {
|
||
customStyle: 'margin: 10px; padding: 5px;'
|
||
}
|
||
})
|
||
|
||
// 验证样式是否被正确应用
|
||
const style = wrapper.attributes('style')
|
||
expect(style).toContain('margin')
|
||
expect(style).toContain('padding')
|
||
})
|
||
|
||
// 测试处理行数限制
|
||
test('正确处理行数限制', () => {
|
||
// 不再尝试导入 isDef 函数,而是直接测试组件行为
|
||
const wrapper = mount(WdText, {
|
||
props: {
|
||
lines: 2,
|
||
text: '这是一段很长的文本,可能会超过两行。这是一段很长的文本,可能会超过两行。'
|
||
}
|
||
})
|
||
|
||
// 验证组件能够正确处理行数限制
|
||
expect(wrapper.exists()).toBe(true)
|
||
// 检查是否应用了行数限制的类
|
||
expect(wrapper.classes().join(' ')).toContain('is-lines-2')
|
||
})
|
||
|
||
// 测试日期格式化功能
|
||
test('正确格式化日期', () => {
|
||
// 我们已经在顶部模拟了 dayjs,所以这里只需要测试结果
|
||
const wrapper = mount(WdText, {
|
||
props: {
|
||
text: '1672531200000',
|
||
mode: 'date'
|
||
}
|
||
})
|
||
|
||
// 验证格式化后的日期文本
|
||
expect(wrapper.text()).toBe('2023-01-01')
|
||
})
|
||
|
||
// 测试属性变化
|
||
test('属性变化时更新', async () => {
|
||
const wrapper = mount(WdText, {
|
||
props: {
|
||
text: 'Hello',
|
||
type: 'default'
|
||
}
|
||
})
|
||
|
||
// 初始状态
|
||
expect(wrapper.text()).toBe('Hello')
|
||
expect(wrapper.classes()).toContain('is-default')
|
||
|
||
// 更新属性
|
||
await wrapper.setProps({
|
||
text: 'World',
|
||
type: 'primary'
|
||
})
|
||
|
||
// 验证更新后的状态
|
||
expect(wrapper.text()).toBe('World')
|
||
expect(wrapper.classes()).toContain('is-primary')
|
||
})
|
||
})
|