Skip to content

Commit a4acc27

Browse files
authored
feat(react/firestore): add useEnableNetworkMutation (#147)
* feat(react): add useEnableNetworkMutation * _
1 parent 2b53a1f commit a4acc27

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

packages/react/src/firestore/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { useClearIndexedDbPersistenceMutation } from "./useClearIndexedDbPersistenceMutation";
22
export { useDisableNetworkMutation } from "./useDisableNetworkMutation";
3-
// useEnableNetworkMutation
3+
export { useEnableNetworkMutation } from "./useEnableNetworkMutation";
44
export { useWaitForPendingWritesQuery } from "./useWaitForPendingWritesQuery";
55
export { useRunTransactionMutation } from "./useRunTransactionMutation";
66
export { useWriteBatchCommitMutation } from "./useWriteBatchCommitMutation";
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { act, renderHook, waitFor } from "@testing-library/react";
2+
import {
3+
doc,
4+
disableNetwork,
5+
getDocFromServer,
6+
setDoc,
7+
} from "firebase/firestore";
8+
import { beforeEach, describe, expect, test, vi } from "vitest";
9+
import {
10+
expectFirestoreError,
11+
firestore,
12+
wipeFirestore,
13+
} from "~/testing-utils";
14+
import { useEnableNetworkMutation } from "./useEnableNetworkMutation";
15+
import { queryClient } from "../../utils";
16+
import { wrapper } from "../../utils";
17+
18+
describe("useEnableNetworkMutation", () => {
19+
beforeEach(async () => {
20+
queryClient.clear();
21+
await disableNetwork(firestore);
22+
await wipeFirestore();
23+
});
24+
25+
test("should successfully enable the Firestore network", async () => {
26+
const docRef = doc(firestore, "tests", "useEnableNetworkMutation");
27+
const mockData = { library: "tanstack-query-firebase" };
28+
29+
const { result } = renderHook(() => useEnableNetworkMutation(firestore), {
30+
wrapper,
31+
});
32+
33+
// Enable the network
34+
await act(() => result.current.mutate());
35+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
36+
37+
await setDoc(docRef, mockData);
38+
39+
const fetchedDoc = await getDocFromServer(docRef);
40+
41+
expect(fetchedDoc.exists()).toBe(true);
42+
expect(fetchedDoc.data()).toEqual(mockData);
43+
});
44+
45+
test("handles mutation options correctly", async () => {
46+
const onSuccessMock = vi.fn();
47+
const onErrorMock = vi.fn();
48+
49+
const { result } = renderHook(
50+
() =>
51+
useEnableNetworkMutation(firestore, {
52+
onSuccess: onSuccessMock,
53+
onError: onErrorMock,
54+
}),
55+
{ wrapper }
56+
);
57+
58+
await act(() => result.current.mutate());
59+
60+
await waitFor(() => {
61+
expect(result.current.isSuccess).toBe(true);
62+
expect(onSuccessMock).toHaveBeenCalled();
63+
expect(onErrorMock).not.toHaveBeenCalled();
64+
});
65+
});
66+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { type UseMutationOptions, useMutation } from "@tanstack/react-query";
2+
import {
3+
type Firestore,
4+
type FirestoreError,
5+
enableNetwork,
6+
} from "firebase/firestore";
7+
8+
type FirestoreUseMutationOptions<TData = unknown, TError = Error> = Omit<
9+
UseMutationOptions<TData, TError, void>,
10+
"mutationFn"
11+
>;
12+
13+
export function useEnableNetworkMutation(
14+
firestore: Firestore,
15+
options?: FirestoreUseMutationOptions<void, FirestoreError>
16+
) {
17+
return useMutation<void, FirestoreError>({
18+
...options,
19+
mutationFn: () => enableNetwork(firestore),
20+
});
21+
}

0 commit comments

Comments
 (0)