/*! * @license * Copyright Squiz Australia Pty Ltd. All Rights Reserved. */ import { AxiosError, AxiosInstance, AxiosResponse } from 'axios'; import { checkIfVersionExists } from './versionChecker'; describe('versionChecker', () => { let mockAxiosClient: AxiosInstance; beforeEach(() => { mockAxiosClient = { get: jest.fn(), } as any; }); describe('checkIfVersionExists', () => { it('should return true when version exists (200 response)', async () => { (mockAxiosClient.get as jest.Mock).mockResolvedValue({ status: 200 } as AxiosResponse); const result = await checkIfVersionExists(mockAxiosClient, '/job/test/1.0.0'); expect(result).toBe(true); expect(mockAxiosClient.get).toHaveBeenCalledWith('/job/test/1.0.0', { validateStatus: null }); }); it('should return false when version does not exist (404 response)', async () => { (mockAxiosClient.get as jest.Mock).mockResolvedValue({ status: 404 } as AxiosResponse); const result = await checkIfVersionExists(mockAxiosClient, '/job/test/1.0.0'); expect(result).toBe(false); }); it('should throw error for unexpected response status', async () => { (mockAxiosClient.get as jest.Mock).mockResolvedValue({ status: 500, statusText: 'Internal Server Error', data: { message: 'Server error' }, } as AxiosResponse); await expect(checkIfVersionExists(mockAxiosClient, '/job/test/1.0.0')).rejects.toThrow( 'Unexpected response code 500. Server error', ); }); it('should handle axios errors', async () => { const axiosError = new AxiosError('Network error'); Object.defineProperty(axiosError, 'isAxiosError', { value: true }); (mockAxiosClient.get as jest.Mock).mockRejectedValue(axiosError); await expect(checkIfVersionExists(mockAxiosClient, '/job/test/1.0.0')).rejects.toThrow( 'Network error (code: unknown)', ); }); it('should handle response without error message', async () => { (mockAxiosClient.get as jest.Mock).mockResolvedValue({ status: 503, statusText: 'Service Unavailable', data: {}, } as AxiosResponse); await expect(checkIfVersionExists(mockAxiosClient, '/job/test/1.0.0')).rejects.toThrow( 'Service Unavailable (code: 503)', ); }); }); });