-
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 useGetCountFromServerQuery hook #105
Merged
Ehesp
merged 8 commits into
invertase:next
from
HassanBahati:add-useGetCountFromServerQuery-hook
Sep 30, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9b19f2a
feat(firestore): add useGetCountFromServerQuery hook
HassanBahati 7056b89
docs: Update README.md
Ehesp 1a2243a
refactor(firestore): remove optional chaining from snapshot
HassanBahati 0a50dcf
Merge branch 'next' into add-useGetCountFromServerQuery-hook
HassanBahati 868b34b
Merge branch 'invertase:main' into add-useGetCountFromServerQuery-hook
HassanBahati f09546a
refactor(firestore): return snapshot for useGetCountFromServerQuery
HassanBahati a6a7dda
refactor(firestore): return snapshot for useGetCountFromServerQuery
HassanBahati 90795ac
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
124 changes: 124 additions & 0 deletions
124
packages/react/src/firestore/useGetCountFromServerQuery.test.tsx
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,124 @@ | ||
import React, { type ReactNode } from "react"; | ||
import { describe, expect, test, beforeEach } from "vitest"; | ||
import { useGetCountFromServerQuery } from "./useGetCountFromServerQuery"; | ||
import { renderHook, waitFor } from "@testing-library/react"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { collection, addDoc, query, where } 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("useGetCountFromServerQuery", () => { | ||
beforeEach(async () => await wipeFirestore()); | ||
|
||
test("returns correct count for empty collection", async () => { | ||
const collectionRef = collection(firestore, "tests"); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useGetCountFromServerQuery(collectionRef, { | ||
queryKey: ["count", "empty"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data?.data().count).toBe(0); | ||
}); | ||
|
||
test("returns correct count for non-empty collection", async () => { | ||
const collectionRef = collection(firestore, "tests"); | ||
|
||
await addDoc(collectionRef, { foo: "bar1" }); | ||
await addDoc(collectionRef, { foo: "bar2" }); | ||
await addDoc(collectionRef, { foo: "bar3" }); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useGetCountFromServerQuery(collectionRef, { | ||
queryKey: ["count", "non-empty"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data?.data().count).toBe(3); | ||
}); | ||
|
||
test("handles complex queries", async () => { | ||
const collectionRef = collection(firestore, "tests"); | ||
|
||
await addDoc(collectionRef, { category: "A", value: 1 }); | ||
await addDoc(collectionRef, { category: "B", value: 2 }); | ||
await addDoc(collectionRef, { category: "A", value: 3 }); | ||
await addDoc(collectionRef, { category: "C", value: 4 }); | ||
|
||
const complexQuery = query(collectionRef, where("category", "==", "A")); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useGetCountFromServerQuery(complexQuery, { | ||
queryKey: ["count", "complex"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
expect(result.current.data?.data().count).toBe(2); | ||
}); | ||
|
||
test("handles restricted collections appropriately", async () => { | ||
const collectionRef = collection(firestore, "restrictedCollection"); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useGetCountFromServerQuery(collectionRef, { | ||
queryKey: ["count", "restricted"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
await waitFor(() => expect(result.current.isError).toBe(true)); | ||
|
||
expectFirestoreError(result.current.error, "permission-denied"); | ||
}); | ||
|
||
test("returns pending state initially", async () => { | ||
const collectionRef = collection(firestore, "tests"); | ||
|
||
await addDoc(collectionRef, { foo: "bar" }); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useGetCountFromServerQuery(collectionRef, { | ||
queryKey: ["count", "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?.data().count).toBe(1); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/react/src/firestore/useGetCountFromServerQuery.ts
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,41 @@ | ||
import { useQuery, type UseQueryOptions } from "@tanstack/react-query"; | ||
import { | ||
type AggregateField, | ||
type AggregateQuerySnapshot, | ||
getCountFromServer, | ||
type FirestoreError, | ||
type Query, | ||
type DocumentData, | ||
} from "firebase/firestore"; | ||
|
||
type FirestoreUseQueryOptions<TData = unknown, TError = Error> = Omit< | ||
UseQueryOptions<TData, TError>, | ||
"queryFn" | ||
>; | ||
|
||
export function useGetCountFromServerQuery< | ||
AppModelType = DocumentData, | ||
DbModelType extends DocumentData = DocumentData | ||
>( | ||
query: Query<AppModelType, DbModelType>, | ||
options: FirestoreUseQueryOptions< | ||
AggregateQuerySnapshot< | ||
{ count: AggregateField<number> }, | ||
AppModelType, | ||
DbModelType | ||
>, | ||
FirestoreError | ||
> | ||
) { | ||
return useQuery< | ||
AggregateQuerySnapshot< | ||
{ count: AggregateField<number> }, | ||
AppModelType, | ||
DbModelType | ||
>, | ||
FirestoreError | ||
>({ | ||
...options, | ||
queryFn: () => getCountFromServer(query), | ||
}); | ||
} |
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.
there is optional chaining at
![image](https://private-user-images.githubusercontent.com/65954740/371857043-6c7731dd-10ad-4a85-bf32-f201fefb8c40.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkyMzA2MjcsIm5iZiI6MTczOTIzMDMyNywicGF0aCI6Ii82NTk1NDc0MC8zNzE4NTcwNDMtNmM3NzMxZGQtMTBhZC00YTg1LWJmMzItZjIwMWZlZmI4YzQwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEwVDIzMzIwN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWU0YzgzYTIyODAyYTBiZmJiZjkwMzgwODQ5ZmEwOWQ2ODBiMWI1MWRlMTY0ZTI1YjhiY2Q3MGYxZTIyYjgxMTImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.DmRH-OwwmYcR2l3eT1uMAwQbcVUSTNyOZ1krmM67uVk)
data?.data()
because useQuery initializes with undefined data before the query resolvesThere 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.
That's OK, it's because typescript isn't inferring isSuccess with data (since it's inside of waitFor).