Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { renderHook, waitFor } from '@testing-library/react';
import { coFetch } from '@console/shared/src/utils/console-fetch';
import {
getConsoleRequestHeaders,
normalizeConsoleHeaders,
} from '@console/shared/src/utils/console-fetch-utils';
import useCatalogItems from '../useCatalogItems';

jest.mock('@console/shared/src/hooks/usePoll', () => {
const { useEffect } = jest.requireActual('react');
return {
usePoll: function usePoll(callback: () => void) {
useEffect(() => {
callback();
}, [callback]);
},
};
});

jest.mock('@console/shared/src/utils/console-fetch', () => ({
coFetch: jest.fn(),
}));

jest.mock('@console/shared/src/utils/console-fetch-utils', () => ({
getConsoleRequestHeaders: jest.fn(),
normalizeConsoleHeaders: jest.fn(),
}));

jest.mock('../../utils/catalog-item', () => ({
normalizeCatalogItem: jest.fn((item) => item),
}));

const coFetchMock = jest.mocked(coFetch);

describe('useCatalogItems', () => {
beforeEach(() => {
jest.resetAllMocks();
jest.mocked(getConsoleRequestHeaders).mockReturnValue({});
jest.mocked(normalizeConsoleHeaders).mockReturnValue({});
});

it('starts with loaded=false before fetch resolves', () => {
coFetchMock.mockReturnValue(new Promise(() => {}));

const { result } = renderHook(() => useCatalogItems());

expect(result.current[1]).toBe(false);
});

it('sets loaded=true after a successful fetch', async () => {
coFetchMock.mockResolvedValue({
status: 200,
headers: { get: () => 'Thu, 01 Jan 2026 00:00:00 GMT' },
json: () => Promise.resolve([]),
} as unknown as Response);

const { result } = renderHook(() => useCatalogItems());

await waitFor(() => expect(result.current[1]).toBe(true));
expect(result.current[2]).toBe('');
});

it('sets loaded=true after a fetch error', async () => {
coFetchMock.mockRejectedValue(new Error('Network error'));

const { result } = renderHook(() => useCatalogItems());

await waitFor(() => expect(result.current[1]).toBe(true));
expect(result.current[2]).toContain('Network error');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import useCatalogItems from './useCatalogItems';

type UseCatalogCategories = () => [CatalogCategory[], boolean, string];
const useCatalogCategories: UseCatalogCategories = () => {
const [items, loading, error] = useCatalogItems();
const [items, loaded, error] = useCatalogItems();
const categories = useMemo(() => {
if (loading || error) {
if (!loaded || error) {
return [];
}
return _.uniq(
Expand All @@ -23,9 +23,9 @@ const useCatalogCategories: UseCatalogCategories = () => {
tags: [id, label],
};
});
}, [error, items, loading]);
}, [error, items, loaded]);

return [categories, loading, error];
return [categories, loaded, error];
};

export default useCatalogCategories;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type OLMCatalogItemData = {
type UseCatalogItems = () => [CatalogItem<OLMCatalogItemData>[], boolean, string];
const useCatalogItems: UseCatalogItems = () => {
const [olmCatalogItems, setOLMCatalogItems] = useState<OLMCatalogItem[]>([]);
const [loading, setLoading] = useState(true);
const [loaded, setLoaded] = useState(false);
const [error, setError] = useState('');
const [lastModified, setLastModified] = useState('');
const abortControllerRef = useRef<AbortController>();
Expand Down Expand Up @@ -54,12 +54,12 @@ const useCatalogItems: UseCatalogItems = () => {
if (olmItems !== null) {
setOLMCatalogItems(olmItems);
}
setLoading(false);
setLoaded(true);
})
.catch((err) => {
if (err.name === 'AbortError') return;
setError(err.toString());
setLoading(false);
setLoaded(true);
});
}, [headers]);

Expand All @@ -73,7 +73,7 @@ const useCatalogItems: UseCatalogItems = () => {

const items = useMemo(() => olmCatalogItems.map(normalizeCatalogItem), [olmCatalogItems]);

return [items, loading, error];
return [items, loaded, error];
};

export default useCatalogItems;
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@
}
}
]
},
"flags": {
"disallowed": ["OLMV1_ENABLED"]
}
},
{
Expand Down