-
Notifications
You must be signed in to change notification settings - Fork 70
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 #109 from HassanBahati/ft-useWaitForPendingWritesQ…
…uery
- Loading branch information
Showing
3 changed files
with
68 additions
and
3 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
46 changes: 46 additions & 0 deletions
46
packages/react/src/firestore/useWaitForPendingWritesQuery.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,46 @@ | ||
import React from "react"; | ||
import { describe, expect, test, beforeEach, vi } from "vitest"; | ||
import { renderHook, act, waitFor } from "@testing-library/react"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { | ||
expectFirestoreError, | ||
firestore, | ||
wipeFirestore, | ||
} from "~/testing-utils"; | ||
import { useWaitForPendingWritesQuery } from "./useWaitForPendingWritesQuery"; | ||
import { doc, setDoc } from "firebase/firestore"; | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { retry: false }, | ||
mutations: { retry: false }, | ||
}, | ||
}); | ||
|
||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
describe("useWaitForPendingWritesQuery", () => { | ||
beforeEach(async () => { | ||
queryClient.clear(); | ||
await wipeFirestore(); | ||
}); | ||
|
||
test("enters loading state when pending writes are in progress", async () => { | ||
const docRef = doc(firestore, "tests", "loadingStateDoc"); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useWaitForPendingWritesQuery(firestore, { | ||
queryKey: ["pending", "write", "loading"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
// Initiate a write without an await | ||
setDoc(docRef, { value: "loading-test" }); | ||
|
||
expect(result.current.isPending).toBe(true); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/react/src/firestore/useWaitForPendingWritesQuery.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,21 @@ | ||
import { useQuery, type UseQueryOptions } from "@tanstack/react-query"; | ||
import { | ||
type Firestore, | ||
type FirestoreError, | ||
waitForPendingWrites, | ||
} from "firebase/firestore"; | ||
|
||
type FirestoreUseQueryOptions<TData = unknown, TError = Error> = Omit< | ||
UseQueryOptions<TData, TError, void>, | ||
"queryFn" | ||
>; | ||
|
||
export function useWaitForPendingWritesQuery( | ||
firestore: Firestore, | ||
options: FirestoreUseQueryOptions<void, FirestoreError> | ||
) { | ||
return useQuery<void, FirestoreError, void>({ | ||
...options, | ||
queryFn: () => waitForPendingWrites(firestore), | ||
}); | ||
} |