Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react/src/firestore/index.ts
Original file line number Diff line number Diff line change
@@ -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";
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),
});
}
Loading