我必须测试这条条件线,但是我不知道该怎么办,我尝试执行的所有操作都会出错或不会更改我的覆盖率百分比测试。
基本上,如果没有顺序,则必须在其他屏幕(列表)中显示一条消息('There is no orders'
),或者必须显示('Received orders'
)。
如果您可以帮助我,就是这种特殊情况:
.then((responseJson) => {
if (responseJson.length == 0) {
this.setState({ received_order_message: 'There is no orders' });
}
else {
this.setState({ received_order_message: 'There is orders' });
}
})
这是感兴趣的代码的一部分:
class OrderedProducts extends Component {
constructor(props) {
super(props);
this.state = {
orders: [''],
buyer_orders: [''],
refreshing: false,
my_order_message: 'My orders',
received_order_message: 'Received orders',
};
}
componentDidMount(){
this.loadOrders();
}
loadOrders = async () => {
const {state} = this.props.navigation;
var token = state.params ? state.params.token : undefined;
var user = jwt_decode(token);
const orders_screen_path = `${process.env.ORDERS}/api/orders_screen/`;
fetch(orders_screen_path, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
'user_id': user.user_id,
'token': token,
}),
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
if (responseJson.length == 0) {
this.setState({ received_order_message: 'There is no orders' });
}
else {
this.setState({ received_order_message: 'There is orders' });
}
if (responseJson.length > 1) {
responseJson.sort((order1, order2) => {
return (order1.date - order2.date);
}).reverse();
}
this.setState({ orders: responseJson });
})
.catch((error) => {
console.error(error);
});
谢谢。