diff --git a/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.test.ts b/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.test.ts index cf19a6c..7b59d37 100644 --- a/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.test.ts +++ b/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.test.ts @@ -1,4 +1,5 @@ import { QueryClient } from '@tanstack/query-core'; +import { tick } from 'svelte'; import { afterEach, beforeEach, @@ -331,3 +332,41 @@ describe('filter setters', () => { expect(store.params.sort).toBe('popularity'); }); }); + +describe('filter change resets pagination', () => { + let store: UnifiedFontStore; + + beforeEach(async () => { + store = new UnifiedFontStore({ limit: 10 }); + // Let the initial effect run so #previousFilterParams is set. + // Without this, the first filter change is treated as initialisation, not a reset. + await tick(); + }); + + afterEach(() => { + store.destroy(); + queryClient.clear(); + vi.resetAllMocks(); + }); + + it('resets offset to 0 when a filter changes', async () => { + store.setParams({ offset: 20 }); + + store.setSearch('roboto'); + await tick(); + + expect(store.params.offset).toBe(0); + }); + + it('clears accumulated fonts when a filter changes', async () => { + const fonts = generateMockFonts(3); + mockedFetch.mockResolvedValue(makeResponse(fonts)); + await store.refetch(); + expect(store.fonts).toHaveLength(3); + + store.setSearch('roboto'); + await tick(); + + expect(store.fonts).toHaveLength(0); + }); +});