diff --git a/app-catalog/src/helpers/catalog.test.ts b/app-catalog/src/helpers/catalog.test.ts new file mode 100644 index 0000000000..c37d095109 --- /dev/null +++ b/app-catalog/src/helpers/catalog.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it, vi } from 'vitest'; +import { CatalogLists } from './catalog'; + +const { mockFetchCatalogs } = vi.hoisted(() => ({ + mockFetchCatalogs: vi.fn(), +})); + +vi.mock('../api/catalogs', () => ({ + fetchCatalogs: mockFetchCatalogs, +})); + +describe('CatalogLists', () => { + it('does not throw for a catalog Service with no annotations', async () => { + mockFetchCatalogs.mockResolvedValue({ + items: [ + { + metadata: { name: 'my-catalog', namespace: 'default' }, + spec: { ports: [{ name: 'http', port: 8080 }] }, + }, + ], + }); + + const catalogs = await CatalogLists(); + + expect(catalogs).toHaveLength(1); + expect(catalogs[0].uri).toBe('http://my-catalog.default:8080'); + }); + + it('still reads annotation values when present', async () => { + mockFetchCatalogs.mockResolvedValue({ + items: [ + { + metadata: { + name: 'my-catalog', + namespace: 'default', + annotations: { 'catalog.headlamp.dev/uri': 'https://charts.example.com' }, + }, + spec: { ports: [{ name: 'http', port: 8080 }] }, + }, + ], + }); + + const catalogs = await CatalogLists(); + + expect(catalogs[0].uri).toBe('https://charts.example.com'); + }); +}); diff --git a/app-catalog/src/helpers/catalog.ts b/app-catalog/src/helpers/catalog.ts index e21ab2d54a..61fa50ff32 100644 --- a/app-catalog/src/helpers/catalog.ts +++ b/app-catalog/src/helpers/catalog.ts @@ -37,8 +37,9 @@ export function CatalogLists() { for (let i = 0; i < response.items.length; i++) { let serviceUri = ''; const metadata = response.items[i].metadata; - if (ANNOTATION_URI in metadata.annotations) { - serviceUri = metadata.annotations[ANNOTATION_URI]; + const annotations = metadata.annotations ?? {}; + if (ANNOTATION_URI in annotations) { + serviceUri = annotations[ANNOTATION_URI]; } // Using the first port @@ -48,14 +49,10 @@ export function CatalogLists() { } let catalogDisplayName = ''; - if ( - ANNOTATION_DISPLAY_NAME in metadata.annotations && - metadata.annotations[ANNOTATION_DISPLAY_NAME] !== '' - ) { - catalogDisplayName = metadata.annotations[ANNOTATION_DISPLAY_NAME]; + if (ANNOTATION_DISPLAY_NAME in annotations && annotations[ANNOTATION_DISPLAY_NAME] !== '') { + catalogDisplayName = annotations[ANNOTATION_DISPLAY_NAME]; } else { - catalogDisplayName = - ANNOTATION_NAME in metadata.annotations ? metadata.annotations[ANNOTATION_NAME] : ''; + catalogDisplayName = ANNOTATION_NAME in annotations ? annotations[ANNOTATION_NAME] : ''; } // Represents a catalog with its metadata and URI. @@ -66,7 +63,7 @@ export function CatalogLists() { displayName: catalogDisplayName, metadataName: metadata.name, namespace: metadata.namespace, - protocol: metadata.annotations[ANNOTATION_PROTOCOL], + protocol: annotations[ANNOTATION_PROTOCOL], uri: serviceUri, };