import { DyFM_HttpCallType, DyFM_HttpResponseType, DyFM_Log } from '@futdevpro/fsm-dynamo'; import { DyNTS_ApiCall_Params, DyNTS_HttpOptions } from './api-call-params.control-model'; describe('| DyNTS_ApiCall_Params', () => { it('| should create an instance with default values', () => { const params = new DyNTS_ApiCall_Params({ name: 'testCall', type: DyFM_HttpCallType.get, endpoint: '/test/endpoint', baseUrl: 'https://example.com/api', }); expect(params.name).toBe('testCall'); expect(params.type).toBe(DyFM_HttpCallType.get); expect(params.endpoint).toBe('/test/endpoint'); expect(params.baseUrl).toBe('https://example.com/api'); expect(params.getFullResponse).toBe(false); expect(params.httpOptions).toEqual({}); }); it('| should create an instance with provided values', () => { const httpOptions = new DyNTS_HttpOptions({ responseType: DyFM_HttpResponseType.json, headers: { 'Content-Type': 'application/json' }, observe: 'body', reportProgress: true, withCredentials: true, }); const params = new DyNTS_ApiCall_Params({ name: 'testCall', type: DyFM_HttpCallType.post, endpoint: '/test/endpoint', baseUrl: 'https://example.com/api', getFullResponse: true, httpOptions: httpOptions, }); expect(params.name).toBe('testCall'); expect(params.type).toBe(DyFM_HttpCallType.post); expect(params.endpoint).toBe('/test/endpoint'); expect(params.baseUrl).toBe('https://example.com/api'); expect(params.getFullResponse).toBe(true); expect(params.httpOptions).toEqual(httpOptions); }); it('| should replace deprecated endpoint parameter braces', () => { spyOn(DyFM_Log, 'warn'); const params = new DyNTS_ApiCall_Params({ name: 'testCall', type: DyFM_HttpCallType.get, endpoint: '/test/{param}/endpoint', baseUrl: 'https://example.com/api', }); expect(DyFM_Log.warn).toHaveBeenCalledWith( 'WARNING: using {param} bracets in endpoint definitions will be deprecated... (/test/{param}/endpoint)' + '\n use :param instead ({pathparam} --> :pathparam)' ); expect(params.endpoint).toBe('/test/:param/endpoint'); }); it('| should extract pathParamKeys from endpoint with colons', () => { const params = new DyNTS_ApiCall_Params({ name: 'testCall', type: DyFM_HttpCallType.get, endpoint: '/test/:userId/:itemId', baseUrl: 'https://example.com/api', }); expect(params.pathParamKeys).toEqual(['userId', 'itemId']); }); it('| should handle endpoint without path parameters', () => { const params = new DyNTS_ApiCall_Params({ name: 'testCall', type: DyFM_HttpCallType.get, endpoint: '/test/endpoint', baseUrl: 'https://example.com/api', }); expect(params.pathParamKeys).toEqual([]); }); it('| should use global baseUrl when baseUrl is not provided', () => { spyOn(DyFM_Log, 'error'); const params = new DyNTS_ApiCall_Params({ name: 'testCall', type: DyFM_HttpCallType.get, endpoint: '/test/endpoint', } as any); expect(DyFM_Log.error).toHaveBeenCalled(); expect(params.baseUrl).toBeDefined(); }); it('| should handle missing endpoint', () => { spyOn(DyFM_Log, 'warn'); const params = new DyNTS_ApiCall_Params({ name: 'testCall', type: DyFM_HttpCallType.get, baseUrl: 'https://example.com/api', } as any); expect(DyFM_Log.warn).toHaveBeenCalled(); expect(params.endpoint).toBe(''); }); it('| should throw error when endpoint is invalid', () => { expect(() => { new DyNTS_ApiCall_Params({ name: 'testCall', type: DyFM_HttpCallType.get, endpoint: { includes: false } as any, baseUrl: 'https://example.com/api', }); }).toThrowError(/DyNTS_ApiCall_Params Error: something went wrong with the endpoint, it is provided but not valid/); }); }); describe('| DyNTS_HttpOptions', () => { it('| should create an instance with default values', () => { const options = new DyNTS_HttpOptions({}); expect(options.responseType).toBeUndefined(); expect(options.headers).toBeUndefined(); expect(options.observe).toBeUndefined(); expect(options.reportProgress).toBeUndefined(); expect(options.withCredentials).toBeUndefined(); }); it('| should create an instance with provided values', () => { const options = new DyNTS_HttpOptions({ responseType: DyFM_HttpResponseType.json, headers: { 'Content-Type': 'application/json' }, observe: 'body', reportProgress: true, withCredentials: true, }); expect(options.responseType).toBe(DyFM_HttpResponseType.json); expect(options.headers).toEqual({ 'Content-Type': 'application/json' }); expect(options.observe).toBe('body'); expect(options.reportProgress).toBe(true); expect(options.withCredentials).toBe(true); }); });