初始化
Some checks failed
Some checks failed
This commit is contained in:
338
frontend/tests/unit/stores/categoryStore.spec.js
Normal file
338
frontend/tests/unit/stores/categoryStore.spec.js
Normal file
@@ -0,0 +1,338 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { useCategoryStore } from '@/stores/categoryStore'
|
||||
import categoryService from '@/services/categoryService'
|
||||
|
||||
// 模拟categoryService
|
||||
vi.mock('@/services/categoryService', () => ({
|
||||
default: {
|
||||
getCategories: vi.fn(),
|
||||
getCategoryById: vi.fn()
|
||||
}
|
||||
}))
|
||||
|
||||
// 模拟console方法
|
||||
const consoleSpy = {
|
||||
error: vi.spyOn(console, 'error').mockImplementation(() => {}),
|
||||
log: vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
}
|
||||
|
||||
describe('Category Store', () => {
|
||||
let categoryStore
|
||||
|
||||
beforeEach(() => {
|
||||
// 创建Pinia实例
|
||||
const pinia = createPinia()
|
||||
setActivePinia(pinia)
|
||||
|
||||
// 获取store实例
|
||||
categoryStore = useCategoryStore()
|
||||
|
||||
// 重置所有模拟
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
// 重置store状态
|
||||
categoryStore.$reset()
|
||||
})
|
||||
|
||||
describe('初始状态', () => {
|
||||
it('应该有正确的初始状态', () => {
|
||||
expect(categoryStore.categories).toEqual([])
|
||||
expect(categoryStore.currentCategory).toBeNull()
|
||||
expect(categoryStore.loading).toBe(false)
|
||||
expect(categoryStore.error).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('getters', () => {
|
||||
beforeEach(() => {
|
||||
// 设置测试数据
|
||||
categoryStore.categories = [
|
||||
{ id: 1, name: '手机CPU', productCount: 50 },
|
||||
{ id: 2, name: '手机GPU', productCount: 40 },
|
||||
{ id: 3, name: '电脑CPU', productCount: 60 },
|
||||
{ id: 4, name: '电脑GPU', productCount: 45 }
|
||||
]
|
||||
})
|
||||
|
||||
it('应该正确获取所有类别', () => {
|
||||
const categories = categoryStore.getAllCategories
|
||||
expect(categories).toEqual([
|
||||
{ id: 1, name: '手机CPU', productCount: 50 },
|
||||
{ id: 2, name: '手机GPU', productCount: 40 },
|
||||
{ id: 3, name: '电脑CPU', productCount: 60 },
|
||||
{ id: 4, name: '电脑GPU', productCount: 45 }
|
||||
])
|
||||
})
|
||||
|
||||
it('应该正确获取类别总数', () => {
|
||||
const count = categoryStore.getCategoryCount
|
||||
expect(count).toBe(4)
|
||||
})
|
||||
|
||||
it('应该正确获取有产品的类别', () => {
|
||||
const categoriesWithProducts = categoryStore.getCategoriesWithProducts
|
||||
expect(categoriesWithProducts).toEqual([
|
||||
{ id: 1, name: '手机CPU', productCount: 50 },
|
||||
{ id: 2, name: '手机GPU', productCount: 40 },
|
||||
{ id: 3, name: '电脑CPU', productCount: 60 },
|
||||
{ id: 4, name: '电脑GPU', productCount: 45 }
|
||||
])
|
||||
})
|
||||
|
||||
it('应该正确获取类别名称列表', () => {
|
||||
const categoryNames = categoryStore.getCategoryNames
|
||||
expect(categoryNames).toEqual(['手机CPU', '手机GPU', '电脑CPU', '电脑GPU'])
|
||||
})
|
||||
|
||||
it('应该根据ID正确获取类别', () => {
|
||||
const category = categoryStore.getCategoryById(1)
|
||||
expect(category).toEqual({ id: 1, name: '手机CPU', productCount: 50 })
|
||||
})
|
||||
|
||||
it('应该根据名称正确获取类别', () => {
|
||||
const category = categoryStore.getCategoryByName('手机CPU')
|
||||
expect(category).toEqual({ id: 1, name: '手机CPU', productCount: 50 })
|
||||
})
|
||||
|
||||
it('应该正确获取当前类别名称', () => {
|
||||
categoryStore.currentCategory = { id: 1, name: '手机CPU' }
|
||||
const categoryName = categoryStore.getCurrentCategoryName
|
||||
expect(categoryName).toBe('手机CPU')
|
||||
})
|
||||
|
||||
it('应该正确检查是否有加载错误', () => {
|
||||
expect(categoryStore.hasError).toBe(false)
|
||||
|
||||
categoryStore.error = '加载失败'
|
||||
expect(categoryStore.hasError).toBe(true)
|
||||
})
|
||||
|
||||
it('应该正确获取错误信息', () => {
|
||||
expect(categoryStore.errorMessage).toBe('')
|
||||
|
||||
categoryStore.error = '加载失败'
|
||||
expect(categoryStore.errorMessage).toBe('加载失败')
|
||||
})
|
||||
})
|
||||
|
||||
describe('actions', () => {
|
||||
describe('fetchCategories', () => {
|
||||
it('应该成功获取所有类别', async () => {
|
||||
// 模拟API响应
|
||||
const mockCategories = [
|
||||
{ id: 1, name: '手机CPU', productCount: 50 },
|
||||
{ id: 2, name: '手机GPU', productCount: 40 }
|
||||
]
|
||||
|
||||
categoryService.getCategories.mockResolvedValue({
|
||||
data: {
|
||||
success: true,
|
||||
data: mockCategories
|
||||
}
|
||||
})
|
||||
|
||||
// 调用action
|
||||
await categoryStore.fetchCategories()
|
||||
|
||||
// 检查结果
|
||||
expect(categoryService.getCategories).toHaveBeenCalled()
|
||||
expect(categoryStore.categories).toEqual(mockCategories)
|
||||
expect(categoryStore.loading).toBe(false)
|
||||
expect(categoryStore.error).toBeNull()
|
||||
})
|
||||
|
||||
it('应该处理获取类别失败的情况', async () => {
|
||||
// 模拟API错误
|
||||
const errorMessage = '获取类别失败'
|
||||
categoryService.getCategories.mockRejectedValue(new Error(errorMessage))
|
||||
|
||||
// 调用action
|
||||
await categoryStore.fetchCategories()
|
||||
|
||||
// 检查结果
|
||||
expect(categoryService.getCategories).toHaveBeenCalled()
|
||||
expect(categoryStore.categories).toEqual([])
|
||||
expect(categoryStore.loading).toBe(false)
|
||||
expect(categoryStore.error).toBe(errorMessage)
|
||||
expect(consoleSpy.error).toHaveBeenCalledWith(
|
||||
'获取类别失败:',
|
||||
expect.any(Error)
|
||||
)
|
||||
})
|
||||
|
||||
it('应该处理API返回失败状态的情况', async () => {
|
||||
// 模拟API返回失败状态
|
||||
categoryService.getCategories.mockResolvedValue({
|
||||
data: {
|
||||
success: false,
|
||||
message: '服务器错误'
|
||||
}
|
||||
})
|
||||
|
||||
// 调用action
|
||||
await categoryStore.fetchCategories()
|
||||
|
||||
// 检查结果
|
||||
expect(categoryService.getCategories).toHaveBeenCalled()
|
||||
expect(categoryStore.categories).toEqual([])
|
||||
expect(categoryStore.loading).toBe(false)
|
||||
expect(categoryStore.error).toBe('服务器错误')
|
||||
})
|
||||
})
|
||||
|
||||
describe('fetchCategoryById', () => {
|
||||
it('应该成功获取指定类别', async () => {
|
||||
// 模拟API响应
|
||||
const mockCategory = { id: 1, name: '手机CPU', productCount: 50 }
|
||||
|
||||
categoryService.getCategoryById.mockResolvedValue({
|
||||
data: {
|
||||
success: true,
|
||||
data: mockCategory
|
||||
}
|
||||
})
|
||||
|
||||
// 调用action
|
||||
await categoryStore.fetchCategoryById(1)
|
||||
|
||||
// 检查结果
|
||||
expect(categoryService.getCategoryById).toHaveBeenCalledWith(1)
|
||||
expect(categoryStore.currentCategory).toEqual(mockCategory)
|
||||
expect(categoryStore.loading).toBe(false)
|
||||
expect(categoryStore.error).toBeNull()
|
||||
})
|
||||
|
||||
it('应该处理获取指定类别失败的情况', async () => {
|
||||
// 模拟API错误
|
||||
const errorMessage = '获取类别详情失败'
|
||||
categoryService.getCategoryById.mockRejectedValue(new Error(errorMessage))
|
||||
|
||||
// 调用action
|
||||
await categoryStore.fetchCategoryById(1)
|
||||
|
||||
// 检查结果
|
||||
expect(categoryService.getCategoryById).toHaveBeenCalledWith(1)
|
||||
expect(categoryStore.currentCategory).toBeNull()
|
||||
expect(categoryStore.loading).toBe(false)
|
||||
expect(categoryStore.error).toBe(errorMessage)
|
||||
expect(consoleSpy.error).toHaveBeenCalledWith(
|
||||
'获取类别详情失败:',
|
||||
expect.any(Error)
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('setCurrentCategory', () => {
|
||||
it('应该正确设置当前类别', () => {
|
||||
const mockCategory = { id: 1, name: '手机CPU', productCount: 50 }
|
||||
|
||||
// 调用action
|
||||
categoryStore.setCurrentCategory(mockCategory)
|
||||
|
||||
// 检查结果
|
||||
expect(categoryStore.currentCategory).toEqual(mockCategory)
|
||||
})
|
||||
|
||||
it('应该正确清除当前类别', () => {
|
||||
// 设置初始状态
|
||||
categoryStore.currentCategory = { id: 1, name: '手机CPU', productCount: 50 }
|
||||
|
||||
// 调用action
|
||||
categoryStore.setCurrentCategory(null)
|
||||
|
||||
// 检查结果
|
||||
expect(categoryStore.currentCategory).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('clearError', () => {
|
||||
it('应该正确清除错误信息', () => {
|
||||
// 设置初始状态
|
||||
categoryStore.error = '测试错误'
|
||||
|
||||
// 调用action
|
||||
categoryStore.clearError()
|
||||
|
||||
// 检查结果
|
||||
expect(categoryStore.error).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('reset', () => {
|
||||
it('应该正确重置store状态', () => {
|
||||
// 设置初始状态
|
||||
categoryStore.categories = [{ id: 1, name: '手机CPU', productCount: 50 }]
|
||||
categoryStore.currentCategory = { id: 1, name: '手机CPU', productCount: 50 }
|
||||
categoryStore.loading = true
|
||||
categoryStore.error = '测试错误'
|
||||
|
||||
// 调用action
|
||||
categoryStore.reset()
|
||||
|
||||
// 检查结果
|
||||
expect(categoryStore.categories).toEqual([])
|
||||
expect(categoryStore.currentCategory).toBeNull()
|
||||
expect(categoryStore.loading).toBe(false)
|
||||
expect(categoryStore.error).toBeNull()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('复杂场景测试', () => {
|
||||
it('应该正确处理并发请求', async () => {
|
||||
// 模拟API响应
|
||||
const mockCategories = [
|
||||
{ id: 1, name: '手机CPU', productCount: 50 },
|
||||
{ id: 2, name: '手机GPU', productCount: 40 }
|
||||
]
|
||||
|
||||
categoryService.getCategories.mockResolvedValue({
|
||||
data: {
|
||||
success: true,
|
||||
data: mockCategories
|
||||
}
|
||||
})
|
||||
|
||||
// 并发调用action
|
||||
const [result1, result2] = await Promise.all([
|
||||
categoryStore.fetchCategories(),
|
||||
categoryStore.fetchCategories()
|
||||
])
|
||||
|
||||
// 检查结果
|
||||
expect(categoryService.getCategories).toHaveBeenCalledTimes(2)
|
||||
expect(categoryStore.categories).toEqual(mockCategories)
|
||||
expect(categoryStore.loading).toBe(false)
|
||||
expect(categoryStore.error).toBeNull()
|
||||
})
|
||||
|
||||
it('应该正确处理数据更新', async () => {
|
||||
// 设置初始数据
|
||||
categoryStore.categories = [
|
||||
{ id: 1, name: '手机CPU', productCount: 50 }
|
||||
]
|
||||
|
||||
// 模拟API响应(更新后的数据)
|
||||
const updatedCategories = [
|
||||
{ id: 1, name: '手机CPU', productCount: 55 },
|
||||
{ id: 2, name: '手机GPU', productCount: 40 }
|
||||
]
|
||||
|
||||
categoryService.getCategories.mockResolvedValue({
|
||||
data: {
|
||||
success: true,
|
||||
data: updatedCategories
|
||||
}
|
||||
})
|
||||
|
||||
// 调用action
|
||||
await categoryStore.fetchCategories()
|
||||
|
||||
// 检查结果
|
||||
expect(categoryStore.categories).toEqual(updatedCategories)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user