@apistudio/apim-cli
Version:
CLI for API Management Products
147 lines (121 loc) • 3.7 kB
text/typescript
/**
* Copyright Super iPaaS Integration LLC, an IBM Company 2024
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import { getRefsFromRouteAsset } from './route-kind-helper.js';
import { BaseAsset } from '../../../model/assets-model.js';
jest.mock('../../common/message-helper.js', () => ({
showWarning: jest.fn(),
}));
describe('getRefsFromRouteAsset', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should return AssetCacheModel with default endpoint ref', () => {
const routeAsset: BaseAsset = {
spec: {
'default-endpoint': { $ref: 'path/to/default-endpoint' },
'loadbalance-endpoints': [],
'conditional-endpoints': [],
},
} as unknown as BaseAsset;
const result = getRefsFromRouteAsset(routeAsset);
expect(result).toEqual([
{ kind: 'HTTPEndpoint', ref: 'path/to/default-endpoint', isNewlyAdded: true },
]);
});
it('should return AssetCacheModel with loadbalance-endpoints refs', () => {
const routeAsset: BaseAsset = {
spec: {
'default-endpoint': {},
'loadbalance-endpoints': [
{ $ref: 'path/to/loadbalance-endpoint1' },
{ $ref: 'path/to/loadbalance-endpoint2' },
],
'conditional-endpoints': [],
},
} as unknown as BaseAsset;
const result = getRefsFromRouteAsset(routeAsset);
expect(result).toEqual([
{
kind: 'HTTPEndpoint',
ref: 'path/to/loadbalance-endpoint1',
isNewlyAdded: true,
},
{
kind: 'HTTPEndpoint',
ref: 'path/to/loadbalance-endpoint2',
isNewlyAdded: true,
},
]);
});
it('should return AssetCacheModel with conditional-endpoints refs', () => {
const routeAsset: BaseAsset = {
spec: {
'default-endpoint': {},
'loadbalance-endpoints': [],
'conditional-endpoints': [
{ endpoint: { $ref: 'path/to/conditional-endpoint1' } },
{ endpoint: { $ref: 'path/to/conditional-endpoint2' } },
],
},
} as unknown as BaseAsset;
const result = getRefsFromRouteAsset(routeAsset);
expect(result).toEqual([
{
kind: 'HTTPEndpoint',
ref: 'path/to/conditional-endpoint1',
isNewlyAdded: true,
},
{
kind: 'HTTPEndpoint',
ref: 'path/to/conditional-endpoint2',
isNewlyAdded: true,
},
]);
});
it('should return empty array when there are no endpoints', () => {
const routeAsset: BaseAsset = {
spec: {
'default-endpoint': {},
'loadbalance-endpoints': [],
'conditional-endpoints': [],
},
} as unknown as BaseAsset;
const result = getRefsFromRouteAsset(routeAsset);
expect(result).toEqual([]);
});
it('should handle route asset with missing spec properties', () => {
const routeAsset: BaseAsset = {
spec: {},
} as unknown as BaseAsset;
const result = getRefsFromRouteAsset(routeAsset);
expect(result).toEqual([]);
});
it('should return AssetCacheModel with mock-endpoint ref', () => {
const routeAsset: BaseAsset = {
spec: {
"default-endpoint": {},
"loadbalance-endpoints": [],
"conditional-endpoints": [],
"mock-endpoint": { $ref: 'path/to/mock-endpoint' },
},
} as unknown as BaseAsset;
const result = getRefsFromRouteAsset(routeAsset);
expect(result).toEqual([
{ kind: 'MockEndpoint', ref: 'path/to/mock-endpoint', isNewlyAdded: true },
]);
});
it('should return empty array when mock-endpoint exists but lacks a $ref', () => {
const routeAsset: BaseAsset = {
spec: {
"default-endpoint": {},
"loadbalance-endpoints": [],
"conditional-endpoints": [],
"mock-endpoint": {},
},
} as unknown as BaseAsset;
const result = getRefsFromRouteAsset(routeAsset);
expect(result).toEqual([]);
});
});