-
Notifications
You must be signed in to change notification settings - Fork 70
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
Add useNamedQuery hook #107
Draft
HassanBahati
wants to merge
4
commits into
invertase:next
Choose a base branch
from
HassanBahati:add-useNamedQuery-hook
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6267733
feat(firestore): add useNamedQuery hook
HassanBahati e49f96f
refactor(firestore): return snapshot for useNamedQuery
HassanBahati 7d9ffc1
Merge branch 'next' into add-useNamedQuery-hook
HassanBahati 085c66f
refactor(firestore): remove unnecessary promises being created in use…
HassanBahati 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import React, { type ReactNode } from "react"; | ||
import { describe, expect, test, beforeEach } from "vitest"; | ||
import { useNamedQuery } from "./useNamedQuery"; | ||
import { renderHook, waitFor } from "@testing-library/react"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { | ||
collection, | ||
addDoc, | ||
query, | ||
where, | ||
DocumentData, | ||
QuerySnapshot, | ||
} from "firebase/firestore"; | ||
|
||
import { | ||
expectFirestoreError, | ||
firestore, | ||
wipeFirestore, | ||
} from "~/testing-utils"; | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}); | ||
|
||
const wrapper = ({ children }: { children: ReactNode }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
describe("useNamedQuery", () => { | ||
beforeEach(async () => await wipeFirestore()); | ||
|
||
test("returns correct data for empty collection", async () => { | ||
const collectionRef = collection(firestore, "tests"); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useNamedQuery(collectionRef, { | ||
queryKey: ["named", "empty"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data?.empty).toBe(true); | ||
expect(result.current.data?.docs.length).toBe(0); | ||
}); | ||
|
||
test("returns correct data for non-empty collection", async () => { | ||
const collectionRef = collection(firestore, "tests"); | ||
|
||
await addDoc(collectionRef, { value: 10 }); | ||
await addDoc(collectionRef, { value: 20 }); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useNamedQuery(collectionRef, { | ||
queryKey: ["named", "non-empty"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data?.empty).toBe(false); | ||
expect(result.current.data?.docs.length).toBe(2); | ||
}); | ||
|
||
test("handles complex queries", async () => { | ||
const collectionRef = collection(firestore, "tests"); | ||
|
||
await addDoc(collectionRef, { category: "A", value: 10 }); | ||
await addDoc(collectionRef, { category: "B", value: 20 }); | ||
await addDoc(collectionRef, { category: "A", value: 30 }); | ||
|
||
const complexQuery = query(collectionRef, where("category", "==", "A")); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useNamedQuery(complexQuery, { | ||
queryKey: ["named", "complex"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data?.empty).toBe(false); | ||
expect(result.current.data?.docs.length).toBe(2); | ||
}); | ||
|
||
test("handles restricted collections appropriately", async () => { | ||
const restrictedCollectionRef = collection( | ||
firestore, | ||
"restrictedCollection" | ||
); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useNamedQuery(restrictedCollectionRef, { | ||
queryKey: ["named", "restricted"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => expect(result.current.isError).toBe(true)); | ||
|
||
expectFirestoreError(result.current.error, "permission-denied"); | ||
}); | ||
|
||
test("uses custom transform function", async () => { | ||
const collectionRef = collection(firestore, "tests"); | ||
|
||
await addDoc(collectionRef, { value: 10 }); | ||
await addDoc(collectionRef, { value: 20 }); | ||
|
||
const customTransform = (snapshot: QuerySnapshot<DocumentData>) => { | ||
return snapshot.docs?.map((doc) => doc.data().value); | ||
}; | ||
|
||
const { result } = renderHook( | ||
() => | ||
useNamedQuery<number[]>(collectionRef, { | ||
queryKey: ["named", "transform"], | ||
firestore: { | ||
transform: customTransform, | ||
}, | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
expect(result?.current?.data).toEqual([10, 20]); | ||
}); | ||
|
||
test("returns pending state initially", async () => { | ||
const collectionRef = collection(firestore, "tests"); | ||
|
||
await addDoc(collectionRef, { value: 10 }); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useNamedQuery(collectionRef, { | ||
queryKey: ["named", "pending"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
// Initially isPending should be true | ||
expect(result.current.isPending).toBe(true); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data?.empty).toBe(false); | ||
expect(result.current.data?.docs.length).toBe(1); | ||
}); | ||
}); |
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,35 @@ | ||
import { useQuery, type UseQueryOptions } from "@tanstack/react-query"; | ||
import { | ||
type FirestoreError, | ||
getDocs, | ||
type Query, | ||
type QuerySnapshot, | ||
type DocumentData, | ||
} from "firebase/firestore"; | ||
|
||
type FirestoreUseQueryOptions<TData = unknown, TError = Error> = Omit< | ||
UseQueryOptions<TData, TError>, | ||
"queryFn" | ||
> & { | ||
firestore?: { | ||
transform?: (snapshot: QuerySnapshot<DocumentData>) => TData; | ||
}; | ||
}; | ||
|
||
export function useNamedQuery<TData = QuerySnapshot<DocumentData>>( | ||
query: Query, | ||
options: FirestoreUseQueryOptions<TData, FirestoreError> | ||
) { | ||
const { firestore, ...queryOptions } = options; | ||
|
||
return useQuery<TData, FirestoreError>({ | ||
...queryOptions, | ||
queryFn: async () => { | ||
const snapshot = await getDocs(query); | ||
if (firestore?.transform) { | ||
return firestore.transform(snapshot); | ||
} | ||
return snapshot as TData; | ||
}, | ||
}); | ||
} |
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.
I don't this hook is right... it should return a Query (or null) based on https://firebase.google.com/docs/reference/js/firestore_.md#namedquery_6438876