/*! * @license * Copyright Squiz Australia Pty Ltd. All Rights Reserved. */ import { AxiosError, AxiosResponse } from 'axios'; import { handleResponse, isAxiosResponse } from './responseHandler'; describe('responseHandler', () => { describe('handleResponse', () => { it('should return data for successful 2xx responses', async () => { const mockResponse: AxiosResponse = { data: { message: 'Success' }, status: 200, statusText: 'OK', headers: {}, config: {} as any, }; const responsePromise = Promise.resolve(mockResponse); const result = await handleResponse(responsePromise); expect(result).toEqual({ message: 'Success' }); }); it('should throw error for non-2xx responses', async () => { const mockError = new AxiosError('Request failed'); mockError.response = { data: { message: 'Not found' }, status: 404, statusText: 'Not Found', headers: {}, config: {} as any, }; const responsePromise = Promise.reject(mockError); await expect(handleResponse(responsePromise)).rejects.toThrow(); }); it('should throw error for network errors', async () => { const networkError = new AxiosError('Network Error'); const responsePromise = Promise.reject(networkError); await expect(handleResponse(responsePromise)).rejects.toThrow(); }); }); describe('isAxiosResponse', () => { it('should return true for valid AxiosResponse objects', () => { const mockResponse: AxiosResponse = { data: {}, status: 200, statusText: 'OK', headers: {}, config: {} as any, }; expect(isAxiosResponse(mockResponse)).toBe(true); }); it('should return false for non-AxiosResponse objects', () => { expect(isAxiosResponse({})).toBe(false); expect(isAxiosResponse(null)).toBe(false); expect(isAxiosResponse(undefined)).toBe(false); expect(isAxiosResponse('string')).toBe(false); }); }); });