import { mount } from '@vue/test-utils' import WdStatusTip from '@/uni_modules/wot-design-uni/components/wd-status-tip/wd-status-tip.vue' import WdImg from '@/uni_modules/wot-design-uni/components/wd-img/wd-img.vue' import { describe, test, expect, vi, beforeEach } from 'vitest' describe('WdStatusTip', () => { beforeEach(() => { vi.clearAllMocks() }) // 测试基本渲染 test('基本渲染', () => { const wrapper = mount(WdStatusTip, { global: { components: { WdImg } } }) expect(wrapper.classes()).toContain('wd-status-tip') expect(wrapper.findComponent(WdImg).exists()).toBe(true) expect(wrapper.findComponent(WdImg).props('src')).toContain('network.png') }) // 测试自定义提示文案 test('自定义提示文案', () => { const tip = '暂无数据' const wrapper = mount(WdStatusTip, { props: { tip }, global: { components: { WdImg } } }) expect(wrapper.find('.wd-status-tip__text').exists()).toBe(true) expect(wrapper.find('.wd-status-tip__text').text()).toBe(tip) }) // 测试自定义样式 test('自定义类名和样式', () => { const customClass = 'custom-tip' const customStyle = 'color: red;' const wrapper = mount(WdStatusTip, { props: { customClass, customStyle }, global: { components: { WdImg } } }) expect(wrapper.classes()).toContain(customClass) expect(wrapper.attributes('style')).toBe(customStyle) }) // 测试自定义图片 test('自定义图片', () => { const image = 'custom-image.png' const wrapper = mount(WdStatusTip, { props: { image }, global: { components: { WdImg } } }) expect(wrapper.findComponent(WdImg).props('src')).toBe(image) }) // 测试预设图片类型 test('预设图片类型', () => { const presetTypes = ['search', 'network', 'content', 'collect', 'comment', 'halo', 'message'] presetTypes.forEach((type) => { const wrapper = mount(WdStatusTip, { props: { image: type }, global: { components: { WdImg } } }) expect(wrapper.findComponent(WdImg).props('src')).toContain(`${type}.png`) }) }) // 测试自定义图片大小 - 数字 test('数字类型的图片大小', () => { const imageSize = 100 const wrapper = mount(WdStatusTip, { props: { imageSize }, global: { components: { WdImg } } }) expect(wrapper.findComponent(WdImg).props('customStyle')).toContain('height:100px') expect(wrapper.findComponent(WdImg).props('customStyle')).toContain('width:100px') }) // 测试自定义图片大小 - 字符串 test('字符串类型的图片大小', () => { const imageSize = '100rpx' const wrapper = mount(WdStatusTip, { props: { imageSize }, global: { components: { WdImg } } }) expect(wrapper.findComponent(WdImg).props('customStyle')).toContain('height:100rpx') expect(wrapper.findComponent(WdImg).props('customStyle')).toContain('width:100rpx') }) // 测试自定义图片大小 - 对象 test('对象类型的图片大小', () => { const imageSize = { width: 100, height: 200 } const wrapper = mount(WdStatusTip, { props: { imageSize }, global: { components: { WdImg } } }) expect(wrapper.findComponent(WdImg).props('customStyle')).toContain('height:200px') expect(wrapper.findComponent(WdImg).props('customStyle')).toContain('width:100px') }) // 测试自定义图片模式 test('自定义图片模式', () => { const imageMode = 'widthFix' const wrapper = mount(WdStatusTip, { props: { imageMode }, global: { components: { WdImg } } }) expect(wrapper.findComponent(WdImg).props('mode')).toBe(imageMode) }) // 测试自定义 URL 前缀 test('自定义 URL 前缀', () => { const urlPrefix = 'https://example.com/images/' const wrapper = mount(WdStatusTip, { props: { urlPrefix, image: 'network' }, global: { components: { WdImg } } }) expect(wrapper.findComponent(WdImg).props('src')).toBe(`${urlPrefix}network.png`) }) // 测试自定义图片插槽 test('自定义图片插槽', () => { const wrapper = mount(WdStatusTip, { slots: { image: '
自定义图片
' }, global: { components: { WdImg } } }) expect(wrapper.findComponent(WdImg).exists()).toBe(false) expect(wrapper.find('.custom-image').exists()).toBe(true) expect(wrapper.find('.custom-image').text()).toBe('自定义图片') }) // 测试无提示文案 test('无提示文案时不渲染', () => { const wrapper = mount(WdStatusTip, { global: { components: { WdImg } } }) expect(wrapper.find('.wd-status-tip__text').exists()).toBe(false) }) // 测试 objToStyle 函数调用 test('调用 objToStyle 函数', () => { // 由于我们已经在顶部模拟了 objToStyle 函数,所以这里不需要再次导入 // 直接验证组件渲染时是否正确设置了图片样式 const wrapper = mount(WdStatusTip, { props: { imageSize: 100 }, global: { components: { WdImg } } }) // 验证图片样式是否正确设置 const imgComponent = wrapper.findComponent(WdImg) expect(imgComponent.props('customStyle')).toContain('width:100px') expect(imgComponent.props('customStyle')).toContain('height:100px') }) // 测试 addUnit 函数调用 test('调用 addUnit 函数', () => { // 由于我们已经在顶部模拟了 addUnit 函数,所以这里不需要再次导入 // 直接验证组件渲染时是否正确设置了图片样式 const wrapper = mount(WdStatusTip, { props: { imageSize: 100 }, global: { components: { WdImg } } }) // 验证图片样式是否正确设置 const imgComponent = wrapper.findComponent(WdImg) expect(imgComponent.props('customStyle')).toContain('100px') }) // 测试 isObj 函数调用 test('调用 isObj 函数', () => { // 由于我们已经在顶部模拟了 isObj 函数,所以这里不需要再次导入 // 直接验证组件渲染时是否正确设置了图片样式 const wrapper = mount(WdStatusTip, { props: { imageSize: { width: 100, height: 200 } }, global: { components: { WdImg } } }) // 验证图片样式是否正确设置 const imgComponent = wrapper.findComponent(WdImg) expect(imgComponent.props('customStyle')).toContain('width:100px') expect(imgComponent.props('customStyle')).toContain('height:200px') }) // 测试 isDef 函数调用 test('调用 isDef 函数', () => { // 由于我们已经在顶部模拟了 isDef 函数,所以这里不需要再次导入 // 直接验证组件渲染时是否正确设置了图片样式 const wrapper = mount(WdStatusTip, { props: { imageSize: { width: 100, height: 200 } }, global: { components: { WdImg } } }) // 验证图片样式是否正确设置 const imgComponent = wrapper.findComponent(WdImg) expect(imgComponent.props('customStyle')).toContain('width:100px') expect(imgComponent.props('customStyle')).toContain('height:200px') }) })