-
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.
Merge branch 'next' into ft-add-useWriteBatchCommitMutation
- Loading branch information
Showing
5 changed files
with
146 additions
and
2 deletions.
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
56 changes: 56 additions & 0 deletions
56
packages/react/src/firestore/useDisableNetworkMutation.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,56 @@ | ||
import React from "react"; | ||
import { describe, expect, test, beforeEach, vi } 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 { useDisableNetworkMutation } from "./useDisableNetworkMutation"; | ||
import { | ||
doc, | ||
enableNetwork, | ||
getDocFromServer, | ||
} 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("useDisableNetworkMutation", () => { | ||
beforeEach(async () => { | ||
queryClient.clear(); | ||
await enableNetwork(firestore); | ||
await wipeFirestore(); | ||
}); | ||
|
||
test("should successfully disable the Firestore network", async () => { | ||
const { result } = renderHook(() => useDisableNetworkMutation(firestore), { | ||
wrapper, | ||
}); | ||
|
||
await act(() => result.current.mutate()); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
// Verify that network operations fail | ||
const docRef = doc(firestore, "tests", "someDoc"); | ||
|
||
try { | ||
await getDocFromServer(docRef); | ||
throw new Error( | ||
"Expected the network to be disabled, but Firestore operation succeeded." | ||
); | ||
} catch (error) { | ||
expectFirestoreError(error, "unavailable"); | ||
} | ||
}); | ||
}); |
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 { useMutation, type UseMutationOptions } from "@tanstack/react-query"; | ||
import { | ||
disableNetwork, | ||
type FirestoreError, | ||
type Firestore, | ||
} from "firebase/firestore"; | ||
|
||
type FirestoreUseMutationOptions<TData = unknown, TError = Error> = Omit< | ||
UseMutationOptions<TData, TError, void>, | ||
"mutationFn" | ||
>; | ||
|
||
export function useDisableNetworkMutation( | ||
firestore: Firestore, | ||
options?: FirestoreUseMutationOptions<void, FirestoreError> | ||
) { | ||
return useMutation<void, FirestoreError>({ | ||
...options, | ||
mutationFn: () => disableNetwork(firestore), | ||
}); | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/react/src/firestore/useWaitForPendingWritesQuery.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,46 @@ | ||
import React from "react"; | ||
import { describe, expect, test, beforeEach, vi } 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 { useWaitForPendingWritesQuery } from "./useWaitForPendingWritesQuery"; | ||
import { doc, setDoc } 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("useWaitForPendingWritesQuery", () => { | ||
beforeEach(async () => { | ||
queryClient.clear(); | ||
await wipeFirestore(); | ||
}); | ||
|
||
test("enters loading state when pending writes are in progress", async () => { | ||
const docRef = doc(firestore, "tests", "loadingStateDoc"); | ||
|
||
const { result } = renderHook( | ||
() => | ||
useWaitForPendingWritesQuery(firestore, { | ||
queryKey: ["pending", "write", "loading"], | ||
}), | ||
{ wrapper } | ||
); | ||
|
||
// Initiate a write without an await | ||
setDoc(docRef, { value: "loading-test" }); | ||
|
||
expect(result.current.isPending).toBe(true); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/react/src/firestore/useWaitForPendingWritesQuery.ts
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 { useQuery, type UseQueryOptions } from "@tanstack/react-query"; | ||
import { | ||
type Firestore, | ||
type FirestoreError, | ||
waitForPendingWrites, | ||
} from "firebase/firestore"; | ||
|
||
type FirestoreUseQueryOptions<TData = unknown, TError = Error> = Omit< | ||
UseQueryOptions<TData, TError, void>, | ||
"queryFn" | ||
>; | ||
|
||
export function useWaitForPendingWritesQuery( | ||
firestore: Firestore, | ||
options: FirestoreUseQueryOptions<void, FirestoreError> | ||
) { | ||
return useQuery<void, FirestoreError, void>({ | ||
...options, | ||
queryFn: () => waitForPendingWrites(firestore), | ||
}); | ||
} |