2

I am working with React + Redux (with thunk) and I am using Jest for testing. I have the next code:

export const startUploading = (file) => {

    return async(dispatch, getState) => {

        const {active:activeNote} = getState().notes;

        Swal.fire({
            title: 'Uploading....',
            text: 'Please wait...',
            allowOutsideClick: false,
            showConfirmButton: false,
            willOpen: () => {
                Swal.showLoading()
            }
        });

        const fileUrl = await fileUpload(file);
        activeNote.url = fileUrl;

        //for pass tests!?
        await dispatch(startSaveNote(activeNote));**

        Swal.close();

    }

};

and

export const startSaveNote = (note) => {

    return async(dispatch, getState) => {

        try {
            const {uid} = getState().auth;

            if(!note.url) delete note.url;
    
            const noteToFirestore = {...note};
            delete noteToFirestore.id;

            await db.doc(`${uid}/journal/notes/${note.id}`)
                        .update(noteToFirestore);

            dispatch(refreshNote(note.id, note));
            Swal.fire('Saved', note.title, 'success');
        }
        catch(e) {
            console.log(e);
        }

    }

};

The test is

    test('should update the url of the entry - startUploading', async() => {

        //const file = new File([], 'pic_testing.jpg');
        await store.dispatch(startUploading(null));

        const docRef = await db.doc(`/TESTING_UID/journal/notes/lirFobR2rpJijw0OcpOV`).get();
        expect(docRef.data().url).toBe('https://test.com.vs.ps');

    
    })

The test is passing, but I had that to use await in the line await dispatch(startSaveNote(activeNote)); (it means that I had that modify my source code for that the test works). Without the await operator (in the previous line), the test is not passing because the line await db.doc(${uid}/journal/notes/${note.id}).update(noteToFirestore); is not working.

My question is: Which is the correct way for use await with dispatch functions? Should I do it every time that the action that I dispatch is asynchronous? Or never should to use the operator and it is a jest bug?

Many thanks in advance!

0 Answers0