From 456905d689ec0ebed7087ff6ce58c358fb0379f5 Mon Sep 17 00:00:00 2001 From: HassanBahati Date: Mon, 30 Sep 2024 12:45:11 +0300 Subject: [PATCH] feat(firestore): add useEnableNetworkMutation --- packages/react/src/firestore/index.ts | 2 +- .../useEnableNetworkMutation.test.tsx | 100 ++++++++++++++++++ .../src/firestore/useEnableNetworkMutation.ts | 21 ++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 packages/react/src/firestore/useEnableNetworkMutation.test.tsx create mode 100644 packages/react/src/firestore/useEnableNetworkMutation.ts diff --git a/packages/react/src/firestore/index.ts b/packages/react/src/firestore/index.ts index 717bdbb..3983a73 100644 --- a/packages/react/src/firestore/index.ts +++ b/packages/react/src/firestore/index.ts @@ -1,7 +1,7 @@ // useClearIndexedDbPersistenceMutation // useEnableIndexedDbPersistenceMutation // useDisableNetworkMutation -// useEnableNetworkMutation +export { useEnableNetworkMutation } from "./useEnableNetworkMutation"; // useRunTransactionMutation // useWaitForPendingWritesQuery // useWriteBatchCommitMutation (WriteBatch) diff --git a/packages/react/src/firestore/useEnableNetworkMutation.test.tsx b/packages/react/src/firestore/useEnableNetworkMutation.test.tsx new file mode 100644 index 0000000..79d6436 --- /dev/null +++ b/packages/react/src/firestore/useEnableNetworkMutation.test.tsx @@ -0,0 +1,100 @@ +import React from "react"; +import { describe, expect, test, beforeEach } 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 { useEnableNetworkMutation } from "./useEnableNetworkMutation"; +import { + doc, + getDoc, + disableNetwork, + setDoc, + enableNetwork, +} from "firebase/firestore"; + +const queryClient = new QueryClient({ + defaultOptions: { + queries: { retry: false }, + mutations: { retry: false }, + }, +}); + +const wrapper = ({ children }: { children: React.ReactNode }) => ( + {children} +); + +describe("useEnableNetworkMutation", () => { + beforeEach(async () => { + queryClient.clear(); + await disableNetwork(firestore); + await wipeFirestore(); + }); + + test("should successfully enable the Firestore network", async () => { + const { result } = renderHook(() => useEnableNetworkMutation(firestore), { + wrapper, + }); + + await act(() => result.current.mutate()); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + + // Verify that network operations successfully execute + const docRef = doc(firestore, "tests", "enabledNetwork"); + await setDoc(docRef, { foo: "bar" }); + + try { + const snapshot = await getDoc(docRef); + expect(snapshot?.exists()).toBe(true); + expect(snapshot?.data()?.foo).toBe("bar"); + } catch (error) { + expectFirestoreError(error, "unavailable"); + } + }); + + test("should correctly reset mutation state after operations", async () => { + const { result } = renderHook(() => useEnableNetworkMutation(firestore), { + wrapper, + }); + + await act(() => result.current.mutate()); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + + act(() => result.current.reset()); + + await waitFor(() => { + expect(result.current.isIdle).toBe(true); + expect(result.current.data).toBeUndefined(); + expect(result.current.error).toBeNull(); + }); + }); + + test("should work correctly when network is already enabled", async () => { + await enableNetwork(firestore); + + const { result } = renderHook(() => useEnableNetworkMutation(firestore), { + wrapper, + }); + + await act(() => result.current.mutate()); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + + // Verify that network operations successfully execute + const docRef = doc(firestore, "tests", "alreadyEnabledNetwork"); + await setDoc(docRef, { foo: "bar" }); + + try { + const snapshot = await getDoc(docRef); + expect(snapshot?.exists()).toBe(true); + expect(snapshot?.data()?.foo).toBe("bar"); + } catch (error) { + expectFirestoreError(error, "unavailable"); + } + }); +}); diff --git a/packages/react/src/firestore/useEnableNetworkMutation.ts b/packages/react/src/firestore/useEnableNetworkMutation.ts new file mode 100644 index 0000000..4cccb82 --- /dev/null +++ b/packages/react/src/firestore/useEnableNetworkMutation.ts @@ -0,0 +1,21 @@ +import { useMutation, type UseMutationOptions } from "@tanstack/react-query"; +import { + type FirestoreError, + type Firestore, + enableNetwork, +} from "firebase/firestore"; + +type FirestoreUseMutationOptions = Omit< + UseMutationOptions, + "mutationFn" +>; + +export function useEnableNetworkMutation( + firestore: Firestore, + options?: FirestoreUseMutationOptions +) { + return useMutation({ + ...options, + mutationFn: () => enableNetwork(firestore), + }); +}