在我的传奇中,我有以下代码:
export function* removeProfile(auth, database, action){
try{
const url = `/users/${action.user.uid}`
const {ref} = database
const result = yield call([database, ref], url)
const {remove} = database.ref()
yield call([result, remove])
const deleteFunction = () => action.user.delete()
yield call([action.user, deleteFunction])
//yield action.user.delete() && database.ref(url).remove()
yield put(ActionCreator.removeProfileSuccess())
yield destroyAuth(auth)
}catch({message}){
yield put(ActionCreator.removeProfileFailure(message))
}
}
在我的测试中,我有这个:
describe('should test removeProfile', () => {
const actionMock = {
user: {
uid: undefined,
delete : jest.fn()
}
}
const deleteFunction = () => actionMock.user.delete()
const url = `/users/${actionMock.user.uid}`
const databaseMock = {
ref: jest.fn()
}
const {ref} = databaseMock
const removeFunction = () => databaseMock.ref
const authMock = {
delete: jest.fn()
}
const it = sagaHelper(removeProfile(authMock, databaseMock, actionMock))
it('should call api ref and remove from database', result => {
let newResult = ''
expect(result).toEqual(newResult = call([databaseMock, ref], url))
expect(newResult).toEqual(call([newResult, removeFunction]))
})
it('should call api delete user from authentication', result => {
expect(JSON.stringify(result)).toEqual(JSON.stringify(call([actionMock.user, deleteFunction])))
})
it('should put removeProfileSuccess', result => {
expect(result).toEqual(put(ActionCreator.removeProfileSuccess()))
})
})
我需要在测试的let newResult = ''
中取一个值,并传递给其他等号以完成测试,“应该调用api ref并从数据库中删除”。但是我的方法却行不通。如何将期望值的结果传递给使用此值进行新呼叫的另一个呼叫? let newResult = '' expect(result).toEqual(newResult = call([databaseMock, ref], url))
expect(newResult).toEqual(call([newResult, removeFunction]))