Skip to content
Merged
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
55 changes: 55 additions & 0 deletions hooks/useRecentSearches.type-compiler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, it, expect, expectTypeOf } from 'vitest';
import { useRecentSearches, STORAGE_KEY, MAX_SEARCHES } from './useRecentSearches';

// Infer the hook's public API directly from its implementation.
type UseRecentSearchesReturn = ReturnType<typeof useRecentSearches>;

describe('useRecentSearches — TypeScript compiler validation & schema constraints', () => {
it('1. exposes STORAGE_KEY and MAX_SEARCHES with correct primitive types', () => {
expectTypeOf(STORAGE_KEY).toBeString();
expectTypeOf(MAX_SEARCHES).toBeNumber();

expect(typeof STORAGE_KEY).toBe('string');
expect(typeof MAX_SEARCHES).toBe('number');
});

it('2. useRecentSearches accepts no arguments', () => {
expectTypeOf(useRecentSearches).parameters.toEqualTypeOf<[]>();

expect(useRecentSearches.length).toBe(0);
});

it('3. returns the expected public API shape', () => {
expectTypeOf<UseRecentSearchesReturn>().toEqualTypeOf<{
searches: string[];
addSearch: (query: string) => void;
clearSearches: () => void;
removeSearch: (query: string) => void;
}>();

expectTypeOf<UseRecentSearchesReturn['searches']>().toEqualTypeOf<string[]>();
});

it('4. enforces function parameter and return types', () => {
expectTypeOf<UseRecentSearchesReturn['addSearch']>().parameters.toEqualTypeOf<[string]>();
expectTypeOf<UseRecentSearchesReturn['addSearch']>().returns.toBeVoid();

expectTypeOf<UseRecentSearchesReturn['removeSearch']>().parameters.toEqualTypeOf<[string]>();
expectTypeOf<UseRecentSearchesReturn['removeSearch']>().returns.toBeVoid();

expectTypeOf<UseRecentSearchesReturn['clearSearches']>().parameters.toEqualTypeOf<[]>();
expectTypeOf<UseRecentSearchesReturn['clearSearches']>().returns.toBeVoid();
});

it('5. verifies required fields and inferred property types', () => {
expectTypeOf<UseRecentSearchesReturn>().toHaveProperty('searches');
expectTypeOf<UseRecentSearchesReturn>().toHaveProperty('addSearch');
expectTypeOf<UseRecentSearchesReturn>().toHaveProperty('removeSearch');
expectTypeOf<UseRecentSearchesReturn>().toHaveProperty('clearSearches');

expectTypeOf<UseRecentSearchesReturn['searches']>().items.toEqualTypeOf<string>();

expect(STORAGE_KEY.length).toBeGreaterThan(0);
expect(MAX_SEARCHES).toBeGreaterThan(0);
});
});
Loading