diff --git a/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.test.ts b/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.test.ts new file mode 100644 index 0000000..44e2200 --- /dev/null +++ b/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.test.ts @@ -0,0 +1,85 @@ +import { QueryClient } from '@tanstack/query-core'; +import { + afterEach, + beforeEach, + describe, + expect, + it, + vi, +} from 'vitest'; +import { + FontNetworkError, + FontResponseError, +} from '../../../lib/errors/errors'; + +vi.mock('$shared/api/queryClient', () => ({ + queryClient: new QueryClient({ + defaultOptions: { + queries: { + retry: 0, + gcTime: 0, + }, + }, + }), +})); + +vi.mock('../../../api', () => ({ + fetchProxyFonts: vi.fn(), +})); + +import { queryClient } from '$shared/api/queryClient'; +import { fetchProxyFonts } from '../../../api'; +import { UnifiedFontStore } from './unifiedFontStore.svelte'; + +const mockedFetch = fetchProxyFonts as ReturnType; + +describe('UnifiedFontStore.fetchFn error paths', () => { + let store: UnifiedFontStore; + + beforeEach(() => { + store = new UnifiedFontStore({ limit: 10 }); + }); + + afterEach(() => { + store.destroy(); + queryClient.clear(); + vi.resetAllMocks(); + }); + + it('sets isError and error getter when fetchProxyFonts throws', async () => { + mockedFetch.mockRejectedValue(new Error('network down')); + await store.refetch().catch((e: unknown) => e); + + expect(store.error).toBeInstanceOf(FontNetworkError); + expect((store.error as FontNetworkError).cause).toBeInstanceOf(Error); + expect(store.isError).toBe(true); + }); + + it('throws FontResponseError when response is falsy', async () => { + mockedFetch.mockResolvedValue(undefined); + + await store.refetch().catch((e: unknown) => e); + + expect(store.error).toBeInstanceOf(FontResponseError); + expect((store.error as FontResponseError).field).toBe('response'); + }); + + it('throws FontResponseError when response.fonts is missing', async () => { + mockedFetch.mockResolvedValue({ total: 0, limit: 10, offset: 0 }); + + await store.refetch().catch((e: unknown) => e); + + expect(store.error).toBeInstanceOf(FontResponseError); + expect((store.error as FontResponseError).field).toBe('response.fonts'); + }); + + it('throws FontResponseError when response.fonts is not an array', async () => { + mockedFetch.mockResolvedValue({ fonts: 'bad', total: 0, limit: 10, offset: 0 }); + + await store.refetch().catch((e: unknown) => e); + + expect(store.error).toBeInstanceOf(FontResponseError); + expect((store.error as FontResponseError).field).toBe('response.fonts'); + expect((store.error as FontResponseError).received).toBe('bad'); + }); +});