import Axios from 'axios'; import { DyFM_Error, DyFM_HttpCallType, DyFM_HttpResponseType } from '@futdevpro/fsm-dynamo'; import { DyNTS_ApiCall_Params } from '../../_models/control-models/api-call-params.control-model'; import { DyNTS_ApiCallInput_Params, DyNTS_ApiService } from './api.service'; describe('| DyNTS_ApiService', () => { let callParams: DyNTS_ApiCall_Params; let inputParams: DyNTS_ApiCallInput_Params; beforeEach(() => { callParams = new DyNTS_ApiCall_Params({ baseUrl: 'http://example.com', endpoint: '/api/test', type: DyFM_HttpCallType.get, httpOptions: { headers: {}, responseType: DyFM_HttpResponseType.json, }, name: 'Test API Call', getFullResponse: false, }); inputParams = { pathParams: { id: '123' }, body: { key: 'value' }, }; }); // Helper function to create a complete mock Axios instance const createMockAxiosInstance = (methods: any = {}) => { return { defaults: { headers: { common: {}, delete: {}, get: {}, head: {}, post: {}, put: {}, patch: {}, }, httpsAgent: undefined, httpAgent: undefined, timeout: undefined, maxRedirects: undefined, withCredentials: undefined, responseType: undefined, }, get: jasmine.createSpy('get'), post: jasmine.createSpy('post'), put: jasmine.createSpy('put'), patch: jasmine.createSpy('patch'), delete: jasmine.createSpy('delete'), ...methods, }; }; it('| should make a GET request successfully', async () => { callParams.type = DyFM_HttpCallType.get; const response = { data: { success: true } }; const mockAxios = createMockAxiosInstance({ get: jasmine.createSpy('get').and.returnValue(Promise.resolve(response)), }); spyOn(Axios, 'create').and.returnValue(mockAxios); const result = await DyNTS_ApiService.startApiCall(callParams, inputParams); expect(result).toEqual(response.data); }); it('| should handle a POST request successfully', async () => { callParams.type = DyFM_HttpCallType.post; const response = { data: { success: true } }; const mockAxios = createMockAxiosInstance({ post: jasmine.createSpy('post').and.returnValue(Promise.resolve(response)), }); spyOn(Axios, 'create').and.returnValue(mockAxios); const result = await DyNTS_ApiService.startApiCall(callParams, inputParams); expect(result).toEqual(response.data); }); it('| should throw an error for unsupported HTTP method', async () => { callParams.type = 'unsupported' as DyFM_HttpCallType; const mockAxios = createMockAxiosInstance(); spyOn(Axios, 'create').and.returnValue(mockAxios); await expectAsync(DyNTS_ApiService.startApiCall(callParams, inputParams)).toBeRejectedWith( jasmine.any(DyFM_Error) ); }); it('| should handle error response with DYNAMO flag', async () => { const errorResponse = { response: { data: { flag: 'DYNAMO', message: 'Error' } }, }; const mockAxios = createMockAxiosInstance({ get: jasmine.createSpy('get').and.returnValue(Promise.reject(errorResponse)), }); spyOn(Axios, 'create').and.returnValue(mockAxios); await expectAsync(DyNTS_ApiService.startApiCall(callParams, inputParams)).toBeRejectedWith( jasmine.any(DyFM_Error) ); }); it('| should handle DNS error', async () => { const error = { code: 'ENOTFOUND' }; const mockAxios = createMockAxiosInstance({ get: jasmine.createSpy('get').and.returnValue(Promise.reject(error)), }); spyOn(Axios, 'create').and.returnValue(mockAxios); await expectAsync(DyNTS_ApiService.startApiCall(callParams, inputParams)).toBeRejectedWith( jasmine.any(DyFM_Error) ); }); it('| should handle connection refused error', async () => { const error = { code: 'ECONNREFUSED' }; const mockAxios = createMockAxiosInstance({ get: jasmine.createSpy('get').and.returnValue(Promise.reject(error)), }); spyOn(Axios, 'create').and.returnValue(mockAxios); await expectAsync(DyNTS_ApiService.startApiCall(callParams, inputParams)).toBeRejectedWith( jasmine.any(DyFM_Error) ); }); });