-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7957c1f
commit df9d503
Showing
4 changed files
with
114 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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,30 @@ | ||
import { useQuery } from '@tanstack/vue-query'; | ||
import _isEmpty from 'lodash/isEmpty'; | ||
import { computeQueryOverrides } from '@/helpers/computeQueryOverrides'; | ||
import { fetchDocById } from '@/helpers/query/utils'; | ||
import { FAMILY_QUERY_KEY } from '@/constants/queryKeys'; | ||
import { FIRESTORE_COLLECTIONS } from '@/constants/firebase'; | ||
|
||
/** | ||
* Family Query | ||
* | ||
* Query designed to fetch a single family record by its ID. | ||
* | ||
* @param {String} familyId – The ID of the family to fetch. | ||
* @param {QueryOptions|undefined} queryOptions – Optional TanStack query options. | ||
* @returns {UseQueryResult} The TanStack query result. | ||
*/ | ||
const useFamilyQuery = (familyId, queryOptions = undefined) => { | ||
// Ensure all necessary data is loaded before enabling the query. | ||
const conditions = [() => !_isEmpty(familyId)]; | ||
const { isQueryEnabled, options } = computeQueryOverrides(conditions, queryOptions); | ||
|
||
return useQuery({ | ||
queryKey: [FAMILY_QUERY_KEY, familyId], | ||
queryFn: () => fetchDocById(FIRESTORE_COLLECTIONS.FAMILIES, familyId), | ||
enabled: isQueryEnabled, | ||
...options, | ||
}); | ||
}; | ||
|
||
export default useFamilyQuery; |
This file contains 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,69 @@ | ||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
import * as VueQuery from '@tanstack/vue-query'; | ||
import { nanoid } from 'nanoid'; | ||
import { withSetup } from '@/test-support/withSetup.js'; | ||
import { fetchDocById } from '@/helpers/query/utils'; | ||
import useFamilyQuery from './useFamilyQuery'; | ||
|
||
vi.mock('@/helpers/query/utils', () => ({ | ||
fetchDocById: vi.fn().mockImplementation(() => []), | ||
})); | ||
|
||
vi.mock('@tanstack/vue-query', async (getModule) => { | ||
const original = await getModule(); | ||
return { | ||
...original, | ||
useQuery: vi.fn().mockImplementation(original.useQuery), | ||
}; | ||
}); | ||
|
||
describe('useFamilyQuery', () => { | ||
let queryClient; | ||
|
||
beforeEach(() => { | ||
queryClient = new VueQuery.QueryClient(); | ||
}); | ||
|
||
afterEach(() => { | ||
queryClient?.clear(); | ||
}); | ||
|
||
it('should call query with correct parameters when fetching a specific family', () => { | ||
const familyId = nanoid(); | ||
|
||
vi.spyOn(VueQuery, 'useQuery'); | ||
|
||
withSetup(() => useFamilyQuery(familyId), { | ||
plugins: [[VueQuery.VueQueryPlugin, { queryClient }]], | ||
}); | ||
|
||
expect(VueQuery.useQuery).toHaveBeenCalledWith({ | ||
queryKey: ['family', familyId], | ||
queryFn: expect.any(Function), | ||
enabled: expect.objectContaining({ | ||
_value: true, | ||
}), | ||
}); | ||
|
||
expect(fetchDocById).toHaveBeenCalledWith('families', familyId); | ||
}); | ||
|
||
it('should allow the query to be disabled via the passed query options', () => { | ||
const familyId = nanoid(); | ||
const queryOptions = { enabled: false }; | ||
|
||
vi.spyOn(VueQuery, 'useQuery'); | ||
|
||
withSetup(() => useFamilyQuery(familyId, queryOptions), { | ||
plugins: [[VueQuery.VueQueryPlugin, { queryClient }]], | ||
}); | ||
|
||
expect(VueQuery.useQuery).toHaveBeenCalledWith({ | ||
queryKey: ['family', familyId], | ||
queryFn: expect.any(Function), | ||
enabled: expect.objectContaining({ | ||
_value: false, | ||
}), | ||
}); | ||
}); | ||
}); |
This file contains 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 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