Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app-catalog/src/helpers/catalog.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
17 changes: 7 additions & 10 deletions app-catalog/src/helpers/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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,
};

Expand Down