import { mount } from '@vue/test-utils'; import video from './video.vue'; import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; // 模拟后端接口 const mock = new MockAdapter(axios); const mockCameras = [ { name: '相机1', id: 1, streamName: 'stream123' }, { name: '相机2', id: 2, streamName: 'stream456' } ]; // 模拟API响应 mock.onGet('/api/list').reply(200, mockCameras); describe('video.vue', () => { let wrapper; beforeEach(() => { wrapper = mount(video, { global: { mocks: { $nextTick: jest.fn(), uni: { createVideoContext: jest.fn(() => ({ play: jest.fn(), stop: jest.fn() })), request: jest.fn(), showToast: jest.fn() } } } }); }); // 测试相机列表加载 it('加载相机列表', async () => { await wrapper.vm.$nextTick(); expect(wrapper.vm.cameras).toEqual(mockCameras); }); // 测试流媒体地址生成 it('生成正确的RTMP地址', () => { const currentIp = '127.0.0.1'; window.location.hostname = currentIp; wrapper.vm.currentCamera = mockCameras[0]; wrapper.vm.updateStreamUrl(); expect(wrapper.vm.streamUrl).toBe(`rtmp://${currentIp}/live/camera1`); }); // 测试相机切换 it('切换相机并更新地址', () => { wrapper.vm.handleCameraChange({ detail: { value: 1 } }); expect(wrapper.vm.currentCamera).toEqual(mockCameras[1]); expect(wrapper.vm.streamUrl).toBe(`rtmp://${window.location.hostname}/live/camera2`); }); // 测试播放失败处理 it('处理播放失败', async () => { wrapper.vm.uni.request.mockRejectedValue(new Error('API请求失败')); await wrapper.vm.startPlay(); expect(wrapper.vm.uni.showToast).toHaveBeenCalledWith({ title: '播放失败', icon: 'none' }); }); });