-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/compt 40 create query factory #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
a-elkhiraooui-ciscode
merged 4 commits into
develop
from
feat/COMPT-40-create-query-factory
Apr 6, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e2e42d5
feat(COMPT-40): implement createQuery factory
a-elkhiraooui-ciscode 88574f8
chore: sync package-lock.json with @tanstack/react-query addition
a-elkhiraooui-ciscode 027f74a
chore: switch from pnpm to npm, sync lockfile
a-elkhiraooui-ciscode ae4c5ce
chore: fix prettier formatting across all files
a-elkhiraooui-ciscode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './components'; | ||
| export * from './hooks'; | ||
| export * from './utils'; | ||
| export * from './query'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { renderHook, waitFor } from '@testing-library/react'; | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import React from 'react'; | ||
| import { createQuery } from './createQuery'; | ||
|
|
||
| function makeWrapper() { | ||
| const queryClient = new QueryClient({ | ||
| defaultOptions: { queries: { retry: false } }, | ||
| }); | ||
| return ({ children }: { children: React.ReactNode }) => ( | ||
| <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
| ); | ||
| } | ||
|
|
||
| describe('createQuery', () => { | ||
| it('returns a QueryDefinition with queryKey, queryFn, and useQuery', () => { | ||
| const def = createQuery( | ||
| (id: string) => ['user', id] as const, | ||
| async (id: string) => ({ id, name: 'Alice' }), | ||
| ); | ||
|
|
||
| expect(typeof def.queryKey).toBe('function'); | ||
| expect(typeof def.queryFn).toBe('function'); | ||
| expect(typeof def.useQuery).toBe('function'); | ||
| }); | ||
|
|
||
| it('queryKey returns the correct readonly tuple given params', () => { | ||
| const def = createQuery( | ||
| (id: string) => ['user', id] as const, | ||
| async (id: string) => ({ id }), | ||
| ); | ||
|
|
||
| expect(def.queryKey('123')).toEqual(['user', '123']); | ||
| }); | ||
|
|
||
| it('queryKey produces a stable reference for the same params', () => { | ||
| const def = createQuery( | ||
| (id: number) => ['item', id] as const, | ||
| async (id: number) => id, | ||
| ); | ||
|
|
||
| const first = def.queryKey(1); | ||
| const second = def.queryKey(1); | ||
| expect(first).toEqual(second); | ||
| }); | ||
|
|
||
| it('queryFn calls the fetcher with the given params', async () => { | ||
| const fetcher = vi.fn(async (id: string) => ({ id, name: 'Bob' })); | ||
| const def = createQuery((id: string) => ['user', id] as const, fetcher); | ||
|
|
||
| const result = await def.queryFn('42'); | ||
|
|
||
| expect(fetcher).toHaveBeenCalledWith('42'); | ||
| expect(result).toEqual({ id: '42', name: 'Bob' }); | ||
| }); | ||
|
|
||
| it('queryFn return type is inferred from the fetcher (TData inference)', async () => { | ||
| const def = createQuery( | ||
| (n: number) => ['count', n] as const, | ||
| async (n: number) => ({ value: n * 2 }), | ||
| ); | ||
|
|
||
| const result = await def.queryFn(5); | ||
| // TypeScript infers result as { value: number } | ||
| expect(result.value).toBe(10); | ||
| }); | ||
|
|
||
| it('useQuery hook resolves data from the fetcher', async () => { | ||
| const fetcher = vi.fn(async (id: string) => ({ id, name: 'Carol' })); | ||
| const def = createQuery((id: string) => ['user', id] as const, fetcher); | ||
|
|
||
| const { result } = renderHook(() => def.useQuery('1'), { | ||
| wrapper: makeWrapper(), | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
| expect(result.current.data).toEqual({ id: '1', name: 'Carol' }); | ||
| expect(fetcher).toHaveBeenCalledWith('1'); | ||
| }); | ||
|
|
||
| it('useQuery hook shows loading state before data resolves', () => { | ||
| let resolve: (v: { value: number }) => void; | ||
| const fetcher = vi.fn( | ||
| () => | ||
| new Promise<{ value: number }>((res) => { | ||
| resolve = res; | ||
| }), | ||
| ); | ||
| const def = createQuery((n: number) => ['lazy', n] as const, fetcher); | ||
|
|
||
| const { result } = renderHook(() => def.useQuery(99), { | ||
| wrapper: makeWrapper(), | ||
| }); | ||
|
|
||
| expect(result.current.isLoading).toBe(true); | ||
| // Resolve to avoid open handles | ||
| resolve!({ value: 99 }); | ||
| }); | ||
|
|
||
| it('useQuery respects enabled: false and does not call fetcher', () => { | ||
| const fetcher = vi.fn(async (id: string) => ({ id })); | ||
| const def = createQuery((id: string) => ['item', id] as const, fetcher); | ||
|
|
||
| const { result } = renderHook(() => def.useQuery('x', { enabled: false }), { | ||
| wrapper: makeWrapper(), | ||
| }); | ||
|
|
||
| expect(fetcher).not.toHaveBeenCalled(); | ||
| expect(result.current.fetchStatus).toBe('idle'); | ||
| }); | ||
|
|
||
| it('useQuery uses the key from keyFn — different params produce different keys', async () => { | ||
| const fetcher = vi.fn(async (id: string) => ({ id })); | ||
| const def = createQuery((id: string) => ['entity', id] as const, fetcher); | ||
|
|
||
| const wrapper = makeWrapper(); | ||
|
|
||
| const { result: r1 } = renderHook(() => def.useQuery('a'), { wrapper }); | ||
| const { result: r2 } = renderHook(() => def.useQuery('b'), { wrapper }); | ||
|
|
||
| await waitFor(() => expect(r1.current.isSuccess).toBe(true)); | ||
| await waitFor(() => expect(r2.current.isSuccess).toBe(true)); | ||
|
|
||
| expect(r1.current.data).toEqual({ id: 'a' }); | ||
| expect(r2.current.data).toEqual({ id: 'b' }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { useQuery as useTanstackQuery } from '@tanstack/react-query'; | ||
| import type { UseQueryOptions, UseQueryResult } from '@tanstack/react-query'; | ||
|
|
||
| export type QueryKeyFn<TParams> = (params: TParams) => readonly unknown[]; | ||
| export type QueryFetcher<TParams, TData> = (params: TParams) => Promise<TData>; | ||
|
|
||
| type UseQueryShorthandOptions<TData> = Omit<UseQueryOptions<TData, Error>, 'queryKey' | 'queryFn'>; | ||
|
|
||
| export interface QueryDefinition<TParams, TData> { | ||
| queryKey: QueryKeyFn<TParams>; | ||
| queryFn: QueryFetcher<TParams, TData>; | ||
| useQuery: ( | ||
| params: TParams, | ||
| options?: UseQueryShorthandOptions<TData>, | ||
| ) => UseQueryResult<TData, Error>; | ||
| } | ||
|
|
||
| export function createQuery<TParams, TData>( | ||
| keyFn: QueryKeyFn<TParams>, | ||
| fetcher: QueryFetcher<TParams, TData>, | ||
| ): QueryDefinition<TParams, TData> { | ||
| return { | ||
| queryKey: keyFn, | ||
| queryFn: fetcher, | ||
| useQuery(params, options) { | ||
| return useTanstackQuery<TData, Error>({ | ||
| queryKey: keyFn(params), | ||
| queryFn: () => fetcher(params), | ||
| ...options, | ||
| }); | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './createQuery'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test name claims the query key reference is stable for the same params, but the assertion only checks deep equality (
toEqual). Also,keyFnas written returns a new array each call, so a true referential-stability assertion (toBe) would fail. Consider either updating the assertion/test to reflect referential stability (and implementing memoization), or renaming the test to only assert structural equality.