/*! * @license * Copyright Squiz Australia Pty Ltd. All Rights Reserved. */ import { AxiosInstance } from 'axios'; import { watchAndWaitForUploadAndScanComplete } from './scanWatcher'; jest.mock('./responseHandler', () => ({ handleResponse: jest.fn(), })); import { handleResponse } from './responseHandler'; describe('scanWatcher', () => { let mockApiClient: AxiosInstance; const mockHandleResponse = handleResponse as jest.MockedFunction; beforeEach(() => { jest.clearAllMocks(); jest.useFakeTimers(); mockApiClient = { get: jest.fn(), post: jest.fn(), } as any; }); afterEach(() => { jest.useRealTimers(); }); describe('watchAndWaitForUploadAndScanComplete', () => { const testEndpoint = '/upload-job/status/'; const testId = 'test-id-123'; it('should resolve when status is Success', async () => { mockHandleResponse.mockResolvedValue({ status: 'Success' }); await expect(watchAndWaitForUploadAndScanComplete(mockApiClient, testEndpoint, testId)).resolves.toBeUndefined(); expect(mockApiClient.get).toHaveBeenCalledWith(testEndpoint + testId); }); it('should reject when status is Flagged', async () => { mockHandleResponse.mockResolvedValue({ status: 'Flagged' }); await expect(watchAndWaitForUploadAndScanComplete(mockApiClient, testEndpoint, testId)).rejects.toThrow( 'upload has been flagged as a virus', ); }); it('should reject when status is Error', async () => { mockHandleResponse.mockResolvedValue({ status: 'Error' }); await expect(watchAndWaitForUploadAndScanComplete(mockApiClient, testEndpoint, testId)).rejects.toThrow( 'there has been an error', ); }); }); });