feat: add seedFontCache utility
This commit is contained in:
@@ -19,10 +19,13 @@ vi.mock('$shared/api/api', () => ({
|
||||
}));
|
||||
|
||||
import { api } from '$shared/api/api';
|
||||
import { queryClient } from '$shared/api/queryClient';
|
||||
import { fontKeys } from '$shared/api/queryKeys';
|
||||
import {
|
||||
fetchFontsByIds,
|
||||
fetchProxyFontById,
|
||||
fetchProxyFonts,
|
||||
seedFontCache,
|
||||
} from './proxyFonts';
|
||||
|
||||
const PROXY_API_URL = 'https://api.glyphdiff.com/api/v1/fonts';
|
||||
@@ -46,6 +49,7 @@ function mockApiGet<T>(data: T) {
|
||||
describe('proxyFonts', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(api.get).mockReset();
|
||||
queryClient.clear();
|
||||
});
|
||||
|
||||
describe('fetchProxyFonts', () => {
|
||||
@@ -168,4 +172,33 @@ describe('proxyFonts', () => {
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('seedFontCache', () => {
|
||||
test('should populate cache with multiple fonts', () => {
|
||||
const fonts = [
|
||||
createMockFont({ id: '1', name: 'A' }),
|
||||
createMockFont({ id: '2', name: 'B' }),
|
||||
];
|
||||
seedFontCache(fonts);
|
||||
expect(queryClient.getQueryData(fontKeys.detail('1'))).toEqual(fonts[0]);
|
||||
expect(queryClient.getQueryData(fontKeys.detail('2'))).toEqual(fonts[1]);
|
||||
});
|
||||
|
||||
test('should update existing cached fonts with new data', () => {
|
||||
const id = 'update-me';
|
||||
queryClient.setQueryData(fontKeys.detail(id), createMockFont({ id, name: 'Old' }));
|
||||
|
||||
const updated = createMockFont({ id, name: 'New' });
|
||||
seedFontCache([updated]);
|
||||
|
||||
expect(queryClient.getQueryData(fontKeys.detail(id))).toEqual(updated);
|
||||
});
|
||||
|
||||
test('should handle empty input arrays gracefully', () => {
|
||||
const spy = vi.spyOn(queryClient, 'setQueryData');
|
||||
seedFontCache([]);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
spy.mockRestore();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,13 +11,23 @@
|
||||
*/
|
||||
|
||||
import { api } from '$shared/api/api';
|
||||
import { queryClient } from '$shared/api/queryClient';
|
||||
import { fontKeys } from '$shared/api/queryKeys';
|
||||
import { buildQueryString } from '$shared/lib/utils';
|
||||
import type { QueryParams } from '$shared/lib/utils';
|
||||
import type { UnifiedFont } from '../../model/types';
|
||||
import type {
|
||||
FontCategory,
|
||||
FontSubset,
|
||||
} from '../../model/types';
|
||||
|
||||
/**
|
||||
* Normalizes cache by seeding individual font entries from collection responses.
|
||||
* This ensures that a font fetched in a list or batch is available via its detail key.
|
||||
*
|
||||
* @param fonts - Array of fonts to cache
|
||||
*/
|
||||
export function seedFontCache(fonts: UnifiedFont[]): void {
|
||||
fonts.forEach(font => {
|
||||
queryClient.setQueryData(fontKeys.detail(font.id), font);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy API base URL
|
||||
|
||||
Reference in New Issue
Block a user