|
| 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 | +}); |
0 commit comments