-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react/firestore): add useEnableNetworkMutation (#147)
* feat(react): add useEnableNetworkMutation * _
- Loading branch information
1 parent
2b53a1f
commit a4acc27
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/react/src/firestore/useEnableNetworkMutation.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
} |