machine.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { describe, it, expect, vi, beforeEach } from 'vitest'
  2. import { listMachines, getMachine, addMachine, updateMachine, deleteMachine } from '@/api/machine'
  3. import * as request from '@/utils/request'
  4. import { mockMachines, wrapResponse, wrapErrorResponse } from '../../fixtures'
  5. vi.mock('@/utils/request', () => ({
  6. get: vi.fn(),
  7. post: vi.fn()
  8. }))
  9. describe('Machine API', () => {
  10. beforeEach(() => {
  11. vi.clearAllMocks()
  12. })
  13. describe('listMachines', () => {
  14. it('should call GET /admin/machines/list', async () => {
  15. const mockResponse = wrapResponse(mockMachines)
  16. vi.mocked(request.get).mockResolvedValue(mockResponse)
  17. const result = await listMachines()
  18. expect(request.get).toHaveBeenCalledWith('/admin/machines/list')
  19. expect(result.code).toBe(200)
  20. expect(result.data).toHaveLength(mockMachines.length)
  21. expect(result.data[0].machineId).toBe(mockMachines[0].machineId)
  22. })
  23. })
  24. describe('getMachine', () => {
  25. it('should call GET /admin/machines/detail with id', async () => {
  26. const machine = mockMachines[0]
  27. const mockResponse = wrapResponse(machine)
  28. vi.mocked(request.get).mockResolvedValue(mockResponse)
  29. const result = await getMachine(machine.id)
  30. expect(request.get).toHaveBeenCalledWith('/admin/machines/detail', { id: machine.id })
  31. expect(result.code).toBe(200)
  32. expect(result.data.id).toBe(machine.id)
  33. })
  34. })
  35. describe('addMachine', () => {
  36. it('should call POST /admin/machines/add with data', async () => {
  37. const newMachine = {
  38. ...mockMachines[0],
  39. id: 4,
  40. machineId: 'machine-004',
  41. name: '新机器',
  42. cameraCount: 0
  43. }
  44. const mockResponse = wrapResponse(newMachine, 200, '新增成功')
  45. vi.mocked(request.post).mockResolvedValue(mockResponse)
  46. const addData = {
  47. machineId: newMachine.machineId,
  48. name: newMachine.name,
  49. location: newMachine.location,
  50. description: newMachine.description
  51. }
  52. const result = await addMachine(addData)
  53. expect(request.post).toHaveBeenCalledWith('/admin/machines/add', addData)
  54. expect(result.code).toBe(200)
  55. expect(result.data.machineId).toBe(newMachine.machineId)
  56. })
  57. })
  58. describe('updateMachine', () => {
  59. it('should call POST /admin/machines/update with data', async () => {
  60. const machine = mockMachines[0]
  61. const updatedMachine = { ...machine, name: '更新后名称', location: '三楼' }
  62. const mockResponse = wrapResponse(updatedMachine, 200, '修改成功')
  63. vi.mocked(request.post).mockResolvedValue(mockResponse)
  64. const updateData = {
  65. id: machine.id,
  66. name: '更新后名称',
  67. location: '三楼',
  68. description: machine.description,
  69. enabled: machine.enabled
  70. }
  71. const result = await updateMachine(updateData)
  72. expect(request.post).toHaveBeenCalledWith('/admin/machines/update', updateData)
  73. expect(result.code).toBe(200)
  74. expect(result.data.name).toBe('更新后名称')
  75. })
  76. })
  77. describe('deleteMachine', () => {
  78. it('should call POST /admin/machines/delete with id', async () => {
  79. const mockResponse = wrapResponse(null, 200, '删除成功')
  80. vi.mocked(request.post).mockResolvedValue(mockResponse)
  81. const result = await deleteMachine(mockMachines[2].id) // Use machine with 0 cameras
  82. expect(request.post).toHaveBeenCalledWith('/admin/machines/delete', undefined, {
  83. params: { id: mockMachines[2].id }
  84. })
  85. expect(result.code).toBe(200)
  86. })
  87. it('should handle delete failure when machine has cameras', async () => {
  88. const mockResponse = wrapErrorResponse('该机器下存在摄像头,无法删除', 400)
  89. vi.mocked(request.post).mockResolvedValue(mockResponse)
  90. const result = await deleteMachine(mockMachines[0].id) // Machine with cameras
  91. expect(result.code).toBe(400)
  92. expect(result.message).toBe('该机器下存在摄像头,无法删除')
  93. })
  94. })
  95. })