我需要帮助来嘲笑选择器。
我不确定如何将模拟状态传递给getSelectedDriver
选择器以获取driver
值?
运行测试时,我遇到下一个错误:
Saga
export function* fetchUpdateDriver(action: IUpdateDriverAction): SagaIterator {
const {
payload: { driverId, landlinePhoneNumber, mobilePhoneNumber }
} = action;
const driver = yield select(getSelectedDriver); // how to mock this?
if (driver) {
const { response, error } = yield call(updateDriver, {
currentDriver: driver,
driverId,
landlinePhoneNumber,
mobilePhoneNumber
});
if (response) {
yield put(
fetchUpdateDriverDetailsSucceeded({
landlinePhoneNumber,
mobilePhoneNumber
})
);
} else {
yield put(fetchUpdateDriverDetailsFailed({ error }));
}
}
}
export function* watchFetchUpdateDriverDetails(): SagaIterator {
yield takeEvery(ACTION_TYPES.FETCH_UPDATE_DRIVER_REQUEST, fetchUpdateDriver);
}
Test
describe('driver sagas', () => {
const { watchFetchUpdateDriverDetails } = require('..');
describe('watchFetchDriverNotWorking', () => {
it('should dispatch fetchUpdateDriverDetailsSucceeded if there is a response', async () => {
const payload = parseResponse({
header: {
success: true
}
});
mockUpdateDriverDetailsRequestService.mockResolvedValue(payload);
await expectSaga(watchFetchUpdateDriverDetails)
.dispatch(
fetchUpdateDriverDetails({
driverId: '123',
landlinePhoneNumber: '123',
mobilePhoneNumber: '123'
})
)
.put(
fetchUpdateDriverDetailsSucceeded({
landlinePhoneNumber: '123',
mobilePhoneNumber: '123'
})
)
.silentRun();
});
});
});
我找到了一种使用提供程序来模拟选择器的方法。顺便说一句。我正在使用saga-test-plan库。
await expectSaga(watchFetchUpdateDriverDetails)
.dispatch(
fetchUpdateDriverDetails({
driverId: '123',
landlinePhoneNumber: '123',
mobilePhoneNumber: '123'
})
)
.provide({ // mocking the select function
select({ selector }, next) {
if (selector === getSelectedDriver) {
return true;
}
return next();
}
})
.put(
fetchUpdateDriverDetailsSucceeded({
landlinePhoneNumber: '123',
mobilePhoneNumber: '123'
})
)
.silentRun();
});