Skip to content

Commit

Permalink
feat(react/firestore): add useEnableNetworkMutation (#147)
Browse files Browse the repository at this point in the history
* feat(react): add useEnableNetworkMutation

* _
  • Loading branch information
HassanBahati authored Feb 10, 2025
1 parent 2b53a1f commit a4acc27
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react/src/firestore/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { useClearIndexedDbPersistenceMutation } from "./useClearIndexedDbPersistenceMutation";
export { useDisableNetworkMutation } from "./useDisableNetworkMutation";
// useEnableNetworkMutation
export { useEnableNetworkMutation } from "./useEnableNetworkMutation";
export { useWaitForPendingWritesQuery } from "./useWaitForPendingWritesQuery";
export { useRunTransactionMutation } from "./useRunTransactionMutation";
export { useWriteBatchCommitMutation } from "./useWriteBatchCommitMutation";
Expand Down
66 changes: 66 additions & 0 deletions packages/react/src/firestore/useEnableNetworkMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
21 changes: 21 additions & 0 deletions packages/react/src/firestore/useEnableNetworkMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type UseMutationOptions, useMutation } from "@tanstack/react-query";
import {
type Firestore,
type FirestoreError,
enableNetwork,
} from "firebase/firestore";

type FirestoreUseMutationOptions<TData = unknown, TError = Error> = Omit<
UseMutationOptions<TData, TError, void>,
"mutationFn"
>;

export function useEnableNetworkMutation(
firestore: Firestore,
options?: FirestoreUseMutationOptions<void, FirestoreError>
) {
return useMutation<void, FirestoreError>({
...options,
mutationFn: () => enableNetwork(firestore),
});
}

0 comments on commit a4acc27

Please sign in to comment.