-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from HassanBahati/add-useGetCountFromServerQu…
…ery-hook
- Loading branch information
Showing
4 changed files
with
167 additions
and
2 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
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)); | ||
Check failure on line 39 in packages/react/src/firestore/useGetCountFromServerQuery.test.tsx GitHub Actions / testpackages/react/src/firestore/useGetCountFromServerQuery.test.tsx > useGetCountFromServerQuery > returns correct count for empty collection
|
||
|
||
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); | ||
Check failure on line 61 in packages/react/src/firestore/useGetCountFromServerQuery.test.tsx GitHub Actions / testpackages/react/src/firestore/useGetCountFromServerQuery.test.tsx > useGetCountFromServerQuery > returns correct count for non-empty collection
|
||
}); | ||
|
||
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), | ||
}); | ||
} |