Files
yovision/Sense/ui/tests/unit/sense/liveviewLifecycle.spec.js

42 lines
1.7 KiB
JavaScript

import SenseLiveview from '@/views/sense/liveview/index.vue'
import { createLiveviewSession, getLiveviewSession, listLiveviewRoutes } from '@/api/sense/liveview'
jest.mock('@/api/sense/liveview', () => ({
listLiveviewRoutes: jest.fn(),
createLiveviewSession: jest.fn(),
getLiveviewSession: jest.fn()
}))
describe('Sense live-view page lifecycle', () => {
beforeEach(() => {
jest.useFakeTimers()
listLiveviewRoutes.mockResolvedValue({ data: { list: [], count: 0 }})
createLiveviewSession.mockResolvedValue({ data: { id: 'view_test', playerUrl: '/api/v1/liveview/player/view_test', status: 'waiting', detail: '等待播放器连接' }})
getLiveviewSession.mockResolvedValue({ data: { id: 'view_test', playerUrl: '/api/v1/liveview/player/view_test', status: 'ready', detail: '上游拉流正常' }})
})
afterEach(() => {
jest.clearAllMocks()
jest.useRealTimers()
})
it('opens only the selected route and clears polling when unmounted', async() => {
const context = {
...SenseLiveview.data(),
currentRoute: { id: 'device-1:main', deviceName: '东门摄像机' },
playerOpen: true
}
context.stopTimers = SenseLiveview.methods.stopTimers.bind(context)
context.refreshSession = SenseLiveview.methods.refreshSession.bind(context)
context.startPolling = SenseLiveview.methods.startPolling.bind(context)
await SenseLiveview.methods.openSession.call(context)
expect(createLiveviewSession).toHaveBeenCalledWith('device-1:main')
expect(context.session.playerUrl).toBe('/api/v1/liveview/player/view_test')
expect(jest.getTimerCount()).toBe(2)
SenseLiveview.beforeUnmount.call(context)
expect(jest.getTimerCount()).toBe(0)
})
})