#reactjs #redux #jestjs #thunk
Вопрос:
Я работаю с React Redux (с thunk) и использую Jest для тестирования. У меня есть следующий код:
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();
}
};
и
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);
}
}
};
Тест заключается в
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');
})
Тест проходит, но я должен был использовать ожидание в строке ожидание отправки(startSaveNote(activeNote)); (это означает, что я изменил свой исходный код, чтобы тест работал). Без оператора await (в предыдущей строке) тест не проходит, потому что строка await db.doc( ${uid}/journal/notes/${note.id}
).обновление(noteToFirestore); не работает.
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!