Sherwin Mascarenhas
Sherwin Mascarenhas

Reputation: 11

Redux-React-Firebase Mock Testing

I would like to test my action creator, which takes a task and adds it to firestore and then another also deletes it from firestore. How would I test this code. I assume I need to do some mocking, but i'm not quite familiar with testing action creators with firebase code together, as well as mocking firebase in the first place. Could someone help with an example?

    return (dispatch, getState, {getFirebase, getFirestore}) => {
        // async code to the database
        const firestore = getFirestore();
        const profile = getState().firebase.profile;
        const authorId = getState().firebase.auth.uid;
        const ref = firestore.collection("my_collection").doc();
        let myId = ref.id;



        firestore.collection('tasks').add({
            ...task,
            authorFirstName: profile.firstName,
            authorLastName: profile.lastName,
            authorId: authorId,
            createdAt: new Date(),
            uid: myId
        }).then(() => {
            dispatch({type: 'CREATE_Task', task})
        }).catch((err) => {
            dispatch({type: 'CREATE_TASK_ERROR', err})
        });
    }
};

export const deleteTask = (task) => {
    return (dispatch, getState, {getFirebase, getFirestore}) => {
        // async code to the database
        const firestore = getFirestore();
        const profile = getState().firebase.profile;
        const authorId = getState().firebase.auth.uid;
        const id = task.id;
        firestore.collection('tasks').doc(id).delete()
    }
};```

Upvotes: 1

Views: 107

Answers (0)

Related Questions