Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useEnableNetworkMutation #112

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
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 @@
// useClearIndexedDbPersistenceMutation
// useEnableIndexedDbPersistenceMutation
// useDisableNetworkMutation
// useEnableNetworkMutation
export { useEnableNetworkMutation } from "./useEnableNetworkMutation";
// useRunTransactionMutation
// useWaitForPendingWritesQuery
// useWriteBatchCommitMutation (WriteBatch)
Expand Down
100 changes: 100 additions & 0 deletions packages/react/src/firestore/useEnableNetworkMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -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 }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests don't make sense (I don't think) - should it throw, or work?

}
});
});
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 { useMutation, type UseMutationOptions } from "@tanstack/react-query";
import {
type FirestoreError,
type Firestore,
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