diff --git a/packages/react/src/firestore/index.ts b/packages/react/src/firestore/index.ts index ec75c7b1..9b54af62 100644 --- a/packages/react/src/firestore/index.ts +++ b/packages/react/src/firestore/index.ts @@ -1,7 +1,7 @@ export { useClearIndexedDbPersistenceMutation } from "./useClearIndexedDbPersistenceMutation"; // useEnableIndexedDbPersistenceMutation export { useDisableNetworkMutation } from "./useDisableNetworkMutation"; -// useEnableNetworkMutation +export { useEnableNetworkMutation } from "./useEnableNetworkMutation"; export { useWaitForPendingWritesQuery } from "./useWaitForPendingWritesQuery"; export { useRunTransactionMutation } from "./useRunTransactionMutation"; export { useWriteBatchCommitMutation } from "./useWriteBatchCommitMutation"; diff --git a/packages/react/src/firestore/useEnableNetworkMutation.test.tsx b/packages/react/src/firestore/useEnableNetworkMutation.test.tsx new file mode 100644 index 00000000..18adc446 --- /dev/null +++ b/packages/react/src/firestore/useEnableNetworkMutation.test.tsx @@ -0,0 +1,66 @@ +import { act, renderHook, waitFor } from "@testing-library/react"; +import { + doc, + disableNetwork, + getDocFromServer, + setDoc, +} from "firebase/firestore"; +import { beforeEach, describe, expect, test, vi } from "vitest"; +import { + expectFirestoreError, + firestore, + wipeFirestore, +} from "~/testing-utils"; +import { useEnableNetworkMutation } from "./useEnableNetworkMutation"; +import { queryClient } from "../../utils"; +import { wrapper } from "../../utils"; + +describe("useEnableNetworkMutation", () => { + beforeEach(async () => { + queryClient.clear(); + await disableNetwork(firestore); + await wipeFirestore(); + }); + + test("should successfully enable the Firestore network", async () => { + const docRef = doc(firestore, "tests", "useEnableNetworkMutation"); + const mockData = { library: "tanstack-query-firebase" }; + + const { result } = renderHook(() => useEnableNetworkMutation(firestore), { + wrapper, + }); + + // Enable the network + await act(() => result.current.mutate()); + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + + await setDoc(docRef, mockData); + + const fetchedDoc = await getDocFromServer(docRef); + + expect(fetchedDoc.exists()).toBe(true); + expect(fetchedDoc.data()).toEqual(mockData); + }); + + test("handles mutation options correctly", async () => { + const onSuccessMock = vi.fn(); + const onErrorMock = vi.fn(); + + const { result } = renderHook( + () => + useEnableNetworkMutation(firestore, { + onSuccess: onSuccessMock, + onError: onErrorMock, + }), + { wrapper } + ); + + await act(() => result.current.mutate()); + + await waitFor(() => { + expect(result.current.isSuccess).toBe(true); + expect(onSuccessMock).toHaveBeenCalled(); + expect(onErrorMock).not.toHaveBeenCalled(); + }); + }); +}); diff --git a/packages/react/src/firestore/useEnableNetworkMutation.ts b/packages/react/src/firestore/useEnableNetworkMutation.ts new file mode 100644 index 00000000..dcd6dd54 --- /dev/null +++ b/packages/react/src/firestore/useEnableNetworkMutation.ts @@ -0,0 +1,21 @@ +import { type UseMutationOptions, useMutation } from "@tanstack/react-query"; +import { + type Firestore, + type FirestoreError, + enableNetwork, +} from "firebase/firestore"; + +type FirestoreUseMutationOptions = Omit< + UseMutationOptions, + "mutationFn" +>; + +export function useEnableNetworkMutation( + firestore: Firestore, + options?: FirestoreUseMutationOptions +) { + return useMutation({ + ...options, + mutationFn: () => enableNetwork(firestore), + }); +}