How to mock firebase firestore using jest with @react-native-firebase/firestore

Viewed 417

I'm new to testing and I'm trying to fake a document update call using Jest. To do so I'm using react-native-testing-library and react-native-firebase.

Below is my test suite. I have one input, and when I'm changing its value the document should be updated

import firestore from "@react-native-firebase/firestore"

jest.mock("@react-native-firebase/firestore", () => {
        return () => ({
            collection: jest.fn(() => ({
                doc: jest.fn(() => ({
                    update: jest.fn(),
                })),
            })),
       });
 });

describe("<EditInfosStudy>", () => {
    const willSave = true;
    const setDidChanged = jest.fn();

    test("will update", async () => {
        const { getByTestId } = render(
            <EditInfosStudy willSave={willSave} setDidChanged={setDidChanged} />
        );

        const input = getByTestId("EditInfosStudy_TextInput");
        fireEvent.changeText(input, "study text");
        expect(input.props.value).toBe("study text");

       //  expect(firestore().collection('collection').doc('doc').update({})).toHaveBeenCalled() 
       //  this is where i'm stuck
    });
});

After Running the test Jest indicate a console.log from the function (below) to confirm that the document was updated and the test passes.

What I don't understand is how do I check if the document were updated on the last line using the expect method expect(firestore()…) I’m really confused on the whole mocking part, any help would be greatly appreciated!

belove is the function being called :

if (localChange && willSave) {
        (async () => {
            try {
                await firestore()
                    .collection("users")
                    .doc(user.uid)
                    .update({ study });
                setUser({ study });
                console.log("study updated");
            } catch (err) {
                console.error(err);
            }
        })();
    }
0 Answers
Related