test(UnifiedFontStore): add filter change reset tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { QueryClient } from '@tanstack/query-core';
|
import { QueryClient } from '@tanstack/query-core';
|
||||||
|
import { tick } from 'svelte';
|
||||||
import {
|
import {
|
||||||
afterEach,
|
afterEach,
|
||||||
beforeEach,
|
beforeEach,
|
||||||
@@ -331,3 +332,41 @@ describe('filter setters', () => {
|
|||||||
expect(store.params.sort).toBe('popularity');
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user