У меня есть компонент, как показано ниже
export default class ThreeDBody extends BaseComponent {
constructor(props) {
super(props);
this.getServiceCost = this.getServiceCost.bind(this);
}
getServiceCost(event) {
const textfieldProps = [ ...this.state.textfieldProps ];
const actualBandwidth = this.state.actualBandwidth;
let standard = parseInt(textfieldProps[0].value, 10);
let enhanced = parseInt(textfieldProps[1].value, 10);
let premium = parseInt(textfieldProps[2].value, 10);
const headerData = getClonedObject(this.state.headerData);
const contentData = this.state.contentData;
textfieldProps.forEach((bandWidth, index) => {
if (event.target.id === bandWidth.id) {
if (index === 1) {
if (parseInt(event.target.value, 10) + premium <= actualBandwidth) {
standard = actualBandwidth - parseInt(event.target.value, 10) - premium;
enhanced = parseInt(event.target.value, 10);
} else {
enhanced = actualBandwidth - premium;
standard = '0';
}
textfieldProps[index].value = enhanced ? enhanced : '0';
}
if (index === 2) {
if (parseInt(event.target.value, 10) + enhanced <= actualBandwidth) {
standard = actualBandwidth - parseInt(event.target.value, 10) - enhanced;
premium = parseInt(event.target.value, 10);
} else {
premium = actualBandwidth - enhanced;
standard = '0';
}
textfieldProps[2].value = premium ? premium : '0';
}
}
});
textfieldProps[0].value = standard ? standard : '0';
let terms = {
firstTerm: {
onOffCharge: '0',
rentalPerAnnumCharge: '0'
},
secondTerm: {
onOffCharge: '0',
rentalPerAnnumCharge: '0'
}
};
let params = {
id: event.target.id,
standard,
enhanced,
premium
};
let promise = this.delegateHandler(contentStyles.actions.getCharges, params, validateFunArgs);
promise.then((result) => {
console.log('result ', result);
contentData.forEach((term, index) => {
term.terms.firstTerm = result[index].firstTerm;
term.terms.secondTerm = result[index].secondTerm;
});
});
headerData.terms = terms;
contentData.map((term, index) => {
return term.bandwidth = textfieldProps[index].value;
});
this.setState({
textfieldProps
});
this.props.data.setContentData &&
this.delegateHandler(contentStyles.actions.setContentData, contentData, validateFunArgs);
this.props.data.setHeaderData &&
this.delegateHandler(contentStyles.actions.setHeaderData, headerData, validateFunArgs);
this.props.data.setData && this.delegateHandler(contentStyles.actions.setData, headerData, validateFunArgs);
}
render(){
return (
<div></div>
)
}
}
Я написал тестовый пример для функции getServiceCost как
it('invoke getServiceCost method', () => {
const event = { target: { id: '500', value: '1' } };
enzymeWrapper.instance().getServiceCost(event);
});
Функция делегатаHandler записывается как в файле утилит как
delegateHandler(propertyName, event, args) {
if (
this.props.data &&
this.props.data.hasOwnProperty(propertyName) &&
this.props.data[propertyName] instanceof Function
) {
console.log('fn passed');
let response =this.props.data[propertyName](
args instanceof Function
? args(event)
: Object.assign({}, { id: event.target.id, value: event.target[args] })
);
return response;
} else {
console.log('fn not passed');
}
}
debugLog(...data) {
if (window.LogInfo) {
window.LogInfo(...data);
} else {
console.log(...data);
}
}
ContentStyles, который используется так, как записано в другом файле как
const accordianProps = {
constStyles: {
actions:{
getCharges: function getCharges(params) {
console.log(params);
return new Promise((resolve, reject) => {
let cosPrices = [];
let standardPrice = {};
let enhancedPrice = {};
let premiumPrice = {};
cosPrices.push(standardPrice);
cosPrices.push(enhancedPrice);
cosPrices.push(premiumPrice);
standardPrice['firstTerm'] = {
onOffCharge: '20',
rentalPerAnnumCharge: '20'
};
enhancedPrice['firstTerm'] = {
onOffCharge: '30',
rentalPerAnnumCharge: '30'
};
premiumPrice['firstTerm'] = {
onOffCharge: '20',
rentalPerAnnumCharge: '30'
};
standardPrice['secondTerm'] = {
onOffCharge: '30',
rentalPerAnnumCharge: '40'
};
enhancedPrice['secondTerm'] = {
onOffCharge: '30',
rentalPerAnnumCharge: '40'
};
premiumPrice['secondTerm'] = {
onOffCharge: '50',
rentalPerAnnumCharge: '20'
};
resolve(cosPrices);
});
}
}
когда я пытаюсь запустить свой тестовый файл, я могу увидеть следующую ошибку .. может кто-нибудь сказать мне, что мне здесь не хватает?
<ThreeDBody/> › invoke getServiceCost method
TypeError: Cannot read property 'then' of undefined
at ThreeDBody.getServiceCost (src/components/Accordion/Content/ThreeD/ThreeDBody.jsx:103:11)