Skip to content

Commit

Permalink
Merge pull request #109 from HassanBahati/ft-useWaitForPendingWritesQ…
Browse files Browse the repository at this point in the history
…uery
  • Loading branch information
Ehesp authored Oct 7, 2024
2 parents 507880a + 7560be0 commit c54215d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
4 changes: 1 addition & 3 deletions packages/react/src/firestore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ export { useClearIndexedDbPersistenceMutation } from "./useClearIndexedDbPersist
// useEnableIndexedDbPersistenceMutation
// useDisableNetworkMutation
// useEnableNetworkMutation
export { useWaitForPendingWritesQuery } from "./useWaitForPendingWritesQuery";
export { useRunTransactionMutation } from "./useRunTransactionMutation";
// useWaitForPendingWritesQuery
// useWriteBatchCommitMutation (WriteBatch)
export { useDocumentQuery } from "./useDocumentQuery";
export { useCollectionQuery } from "./useCollectionQuery";
// useGetCountFromServerQuery
export { useGetAggregateFromServerQuery } from "./useGetAggregateFromServerQuery";
export { useGetCountFromServerQuery } from "./useGetCountFromServerQuery";
// useGetAggregateFromServerQuery
// useNamedQuery
46 changes: 46 additions & 0 deletions packages/react/src/firestore/useWaitForPendingWritesQuery.test.tsx
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 packages/react/src/firestore/useWaitForPendingWritesQuery.ts
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),
});
}

0 comments on commit c54215d

Please sign in to comment.