我正在尝试测试以下功能:
export function* signInWithEmail({ payload: { email, password } }) {
try {
const { user } = yield auth.signInWithEmailAndPassword(email, password);
yield call(getSnapShotFromUserAuth, user);
} catch (error) {
yield put(signInFailure(error));
}
}
[我已经研究了redux-saga-test-plan
以及其他Saga测试库,但是如果不使用Saga效果调用Firebase函数,似乎无法弄清楚该如何测试。
我需要一种方法来同时模拟firebase函数,以便能够测试此行是否被称为yield call(getSnapShotFromUserAuth, user);
必须将jest.spyOn
用于firebase功能,然后验证下一步。答案如下:
it("should call getSnapShotFromUserAuth", () => {
let userData = {
user: {
id: 1,
name: "jon",
},
};
let { user } = userData;
jest
.spyOn(auth, "signInWithEmailAndPassword")
.mockImplementation(() => userData);
let saga = testSaga(signInWithEmail, { payload });
saga
.next()
.next({ user })
.call(getSnapShotFromUserAuth, user)
.next()
.isDone();
});