I am using React for my app. I am trying make one food delivery app. In my app there is multiple restaurants. Some of the restaurants have delivery time and some of them don't have. All the restaurants have next 7 days date.
Some of the restaurant does not have first two days delivery time but after that the restaurant has delivery time for rest of the week. Some restaurant does not have delivery times at all for next seven days.
I want to create an helper function which will check if in the restaurant data have delivery time or not.
PS: I am trying to learn UNIT testing. It would be awesome if you show me how to do unit testing of my helper function.
This is my sample data where there is dates and have delivery time:
{
deliveryMethod: "PICKUP",
deliverySlots: [
{
date: "2021年08月04日",
deliveryTimes: [],
},
{
date: "2021年08月05日",
deliveryTimes: [],
},
{
date: "2021年08月06日",
deliveryTimes: [],
},
{
date: "2021年08月07日",
deliveryTimes: [
{
time: "16:30-17:00",
validFrom: "2020年08月07日",
validUntil: "2023年08月07日",
},
{
time: "18:00-19:00",
validFrom: "2020年08月07日",
validUntil: "2023年08月07日",
},
],
},
],
}
This is my sample data where there is dates and no delivery time:
{
deliveryMethod: "PICKUP",
deliverySlots: [
{
date: "2021年08月04日",
deliveryTimes: [],
},
{
date: "2021年08月05日",
deliveryTimes: [],
},
{
date: "2021年08月06日",
deliveryTimes: [],
},
{
date: "2021年08月07日",
deliveryTimes: [],
},
],
}
After passing the value to function, then I stuck how check is there any delivery times exist or not.
const deliveryArea = {
deliveryMethod: "PICKUP",
deliverySlots: [
{
date: "2021-08-04",
deliveryTimes: [],
},
{
date: "2021-08-05",
deliveryTimes: [],
},
{
date: "2021-08-06",
deliveryTimes: [],
},
{
date: "2021-08-07",
deliveryTimes: [
{
time: "16:30-17:00",
validFrom: "2020-08-07",
validUntil: "2023-08-07",
},
{
time: "18:00-19:00",
validFrom: "2020-08-07",
validUntil: "2023-08-07",
},
],
},
{
date: "2021-08-08",
deliveryTimes: [
{
time: "16:30-17:00",
validFrom: "2020-08-08",
validUntil: "2023-08-08",
},
{
time: "18:00-19:00",
validFrom: "2020-08-07",
validUntil: "2023-08-07",
},
],
},
{
date: "2021-08-09",
deliveryTimes: [],
},
{
date: "2021-08-10",
deliveryTimes: [
{
time: "16:30-17:00",
validFrom: "2020-08-10",
validUntil: "2023-08-10",
},
{
time: "18:00-19:00",
validFrom: "2020-08-10",
validUntil: "2023-08-10",
},
],
},
],
};
const helperFunction = (deliveryArea) => {
console.log(deliveryArea); // in here i stuck
};
helperFunction(deliveryArea);
-
Regarding your "PS": This is a bit much of an ask. Stack Overflow is here to help with specific issues you may encounter, but I don't think anyone will be willing to provide a full tutorial here. (Also, a quetion should cover one specific problem only) Choose a unit testing framework and there should be a documentation and additional online material on how to work with it. Then in order to test this specific function, just provide two example objects like in this question, and make sure that the function returns true or false as expected.Sarah Groß– Sarah Groß2021年08月05日 08:46:41 +00:00Commented Aug 5, 2021 at 8:46
1 Answer 1
You can use Array.prototype.some() in order to check if there exists at least one slot where deliverytimes is not empty:
const deliveryArea = { deliveryMethod: "PICKUP", deliverySlots: [{date: "2021-08-04", deliveryTimes: [],},{date: "2021-08-05", deliveryTimes: [],},{date: "2021-08-06", deliveryTimes: [],},{date: "2021-08-07", deliveryTimes: [{startTime: 600,time: "16:30-17:00", validFrom: "2020-08-07", validUntil: "2023-08-07", },{startTime: 660,time: "18:00-19:00", validFrom: "2020-08-07", validUntil: "2023-08-07", },],},{date: "2021-08-08", deliveryTimes: [],},{date: "2021-08-09", deliveryTimes: [],},{date: "2021-08-10", deliveryTimes: [{startTime: 600,time: "16:30-17:00", validFrom: "2020-08-10", validUntil: "2023-08-10", },{startTime: 660,time: "18:00-19:00", validFrom: "2020-08-10", validUntil: "2023-08-10", },],},],};
const deliveryAreaWithNoDeliveryTimes = { deliveryMethod: "PICKUP", deliverySlots: [{date: "2021-08-04", deliveryTimes: [],},{date: "2021-08-05", deliveryTimes: [],},{date: "2021-08-06", deliveryTimes: [],},{date: "2021-08-07", deliveryTimes: [],},],};
// will return true if there are any delivery times defined, false otherwise
const hasDeliveryTimes = (deliveryArea) => {
return deliveryArea.deliverySlots.some((slot) => {
return slot.deliveryTimes.length;
});
};
console.log(hasDeliveryTimes(deliveryArea));
console.log(hasDeliveryTimes(deliveryAreaWithNoDeliveryTimes));
3 Comments
! to negate is absolutely fine... Or just switch your return statements.!... You could put it in front of the returned value inside the function: return slot.deliveryTimes.length; but then the function name should rather be doesNotHaveDeliveryTimes...