我有一个由saga效果调用组成的函数我希望覆盖整个函数而不会丢失任何代码行如何在这里测试条件
export function* fetchFromSource() {
const dataTypeName = mapDataTypes(dataType);
Iif (dataTypeName.length === 0) {
return;
}
yield put(sourceActions.onRdsmSourcePlantRequestStarted());
}
我如何使用jest测试dataTypeName.length 这是我的mapDataTypes单元测试方法
it('should return appropriate dataType when mapDataTypes triggered', () => {
const expected = 'Items';
const actionDataType = action.payload.name;
expect(expected).toEqual(saga.mapDataTypes(actionDataType));
});
这是我的下一个测试方法
it('should return onRdsmSourcePlantRequestStarted action', () => {
const expectedAction = {
type: 'rdsm/sourceView/ON_RDSM_SOURCE_PLANT_REQUEST_STARTED',
};
const dataTypeName = '';
const genNext = generator.next(dataTypeName);
expect(genNext.value).toEqual(put(expectedAction));
});
这里测试传递以验证put调用而不输入if块。 我的问题是如何验证if块
答案 0 :(得分:0)
可能你应该改变你的传奇的实现,并使mapDataTypes
调用声明:
const dataTypeName = yield call(mapDataTypes, dataType)
。
然后你可以这样测试它:
it('should end saga when there is no dataTypeName', () => {
const dataTypeName = '';
expect(generator.next().value).toEqual(call(mapDataTypes, dataType));
expect(generator.next(dataTypeName).done).toBeTruthy();
});
it('should return onRdsmSourcePlantRequestStarted action', () => {
const expectedAction = {
type: 'rdsm/sourceView/ON_RDSM_SOURCE_PLANT_REQUEST_STARTED',
};
const dataTypeName = 'something';
expect(generator.next().value).toEqual(call(mapDataTypes, dataType));
expect(generator.next(dataTypeName).value).toEqual(put(expectedAction));
});
答案 1 :(得分:0)
测试else块
it('should return onRdsmSourcePlantRequestStarted action', () => {
const expectedAction = {
type: 'rdsm/sourceView/ON_RDSM_SOURCE_PLANT_REQUEST_STARTED',
};
const dataTypeName = 'test';
expect(generator.next(dataTypeName).value).toEqual(put(expectedAction));
});
测试if block
it('should return undefined ', () => {
const dataTypeName = '';
expect(generator.next(dataTypeName).value).toBe(undefined));
});