|
|
@@ -0,0 +1,146 @@
|
|
|
+import { mount } from '@vue/test-utils'
|
|
|
+import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
+import { nextTick } from 'vue'
|
|
|
+import HazardDetection from './HazardDetection.vue'
|
|
|
+import MobileHazardDetection from './mobile/m-HazardDetection.vue'
|
|
|
+
|
|
|
+const apiMocks = vi.hoisted(() => ({
|
|
|
+ getHazardHistory: vi.fn(),
|
|
|
+ getLatestRecognitionRecord: vi.fn(),
|
|
|
+ hazardDetection: vi.fn(),
|
|
|
+ uploadImage: vi.fn(),
|
|
|
+ getRecognitionRecordDetail: vi.fn(),
|
|
|
+ getThirdSceneExampleImage: vi.fn(),
|
|
|
+ submitEvaluation: vi.fn(),
|
|
|
+ deleteRecognitionRecord: vi.fn()
|
|
|
+}))
|
|
|
+
|
|
|
+const messageMocks = vi.hoisted(() => ({
|
|
|
+ success: vi.fn(),
|
|
|
+ warning: vi.fn(),
|
|
|
+ error: vi.fn()
|
|
|
+}))
|
|
|
+
|
|
|
+const routerMocks = vi.hoisted(() => ({
|
|
|
+ go: vi.fn(),
|
|
|
+ back: vi.fn(),
|
|
|
+ push: vi.fn()
|
|
|
+}))
|
|
|
+
|
|
|
+vi.mock('@/request/apis.js', () => ({
|
|
|
+ apis: apiMocks
|
|
|
+}))
|
|
|
+
|
|
|
+vi.mock('element-plus', () => ({
|
|
|
+ ElMessage: messageMocks
|
|
|
+}))
|
|
|
+
|
|
|
+vi.mock('vue-router', () => ({
|
|
|
+ useRouter: () => routerMocks
|
|
|
+}))
|
|
|
+
|
|
|
+vi.mock('@/utils/nativeBridge.js', () => ({
|
|
|
+ initNativeNavForSubPage: vi.fn()
|
|
|
+}))
|
|
|
+
|
|
|
+const successfulHazardResponse = {
|
|
|
+ statusCode: 200,
|
|
|
+ data: {
|
|
|
+ scene_name: 'tunnel',
|
|
|
+ labels: ['person'],
|
|
|
+ display_labels: ['person'],
|
|
|
+ third_scenes: ['person_fall'],
|
|
|
+ detections: [],
|
|
|
+ element_hazards: {},
|
|
|
+ annotated_image: 'https://example.test/result.png'
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const mountDesktopHazardDetection = () =>
|
|
|
+ mount(HazardDetection, {
|
|
|
+ global: {
|
|
|
+ stubs: {
|
|
|
+ Sidebar: true,
|
|
|
+ DeleteConfirmModal: true,
|
|
|
+ 'el-icon': true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+const mountMobileHazardDetection = () =>
|
|
|
+ mount(MobileHazardDetection, {
|
|
|
+ global: {
|
|
|
+ stubs: {
|
|
|
+ MobileHeader: true,
|
|
|
+ MobileHistoryDrawer: true,
|
|
|
+ DeleteConfirmModal: true,
|
|
|
+ MobileToast: true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+const resolvePendingLatestChecks = async (resolvers) => {
|
|
|
+ resolvers.forEach((resolve) => {
|
|
|
+ resolve({
|
|
|
+ statusCode: 200,
|
|
|
+ data: {
|
|
|
+ effect_evaluation: 5
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ await Promise.resolve()
|
|
|
+ await nextTick()
|
|
|
+}
|
|
|
+
|
|
|
+describe('HazardDetection start recognition single click guard', () => {
|
|
|
+ let latestRecordResolvers
|
|
|
+
|
|
|
+ beforeEach(() => {
|
|
|
+ vi.clearAllMocks()
|
|
|
+ latestRecordResolvers = []
|
|
|
+ apiMocks.getHazardHistory.mockResolvedValue({
|
|
|
+ statusCode: 200,
|
|
|
+ total: 0,
|
|
|
+ data: []
|
|
|
+ })
|
|
|
+ apiMocks.getLatestRecognitionRecord.mockImplementation(
|
|
|
+ () =>
|
|
|
+ new Promise((resolve) => {
|
|
|
+ latestRecordResolvers.push(resolve)
|
|
|
+ })
|
|
|
+ )
|
|
|
+ apiMocks.hazardDetection.mockResolvedValue(successfulHazardResponse)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('locks the desktop start button before the latest-record check finishes', async () => {
|
|
|
+ const wrapper = mountDesktopHazardDetection()
|
|
|
+ wrapper.vm.uploadedImageUrl = 'https://example.test/upload.png'
|
|
|
+ await nextTick()
|
|
|
+
|
|
|
+ const startButton = wrapper.find('.start-identify-btn')
|
|
|
+ await startButton.trigger('click')
|
|
|
+ await startButton.trigger('click')
|
|
|
+
|
|
|
+ expect(apiMocks.getLatestRecognitionRecord).toHaveBeenCalledTimes(1)
|
|
|
+ expect(startButton.attributes('disabled')).toBeDefined()
|
|
|
+
|
|
|
+ await resolvePendingLatestChecks(latestRecordResolvers)
|
|
|
+ wrapper.unmount()
|
|
|
+ })
|
|
|
+
|
|
|
+ it('locks the mobile start button before the latest-record check finishes', async () => {
|
|
|
+ const wrapper = mountMobileHazardDetection()
|
|
|
+ wrapper.vm.uploadedImageUrl = 'https://example.test/upload.png'
|
|
|
+ await nextTick()
|
|
|
+
|
|
|
+ const startButton = wrapper.find('.start-identify-btn')
|
|
|
+ await startButton.trigger('click')
|
|
|
+ await startButton.trigger('click')
|
|
|
+
|
|
|
+ expect(apiMocks.getLatestRecognitionRecord).toHaveBeenCalledTimes(1)
|
|
|
+ expect(startButton.attributes('disabled')).toBeDefined()
|
|
|
+
|
|
|
+ await resolvePendingLatestChecks(latestRecordResolvers)
|
|
|
+ wrapper.unmount()
|
|
|
+ })
|
|
|
+})
|