My axios request polling created for Cypress is not actually polling the request. Could someone please advise how can we fix the problem ?
Note: For some reason I would like to do this with axios only!
In the subscriptionChange.spec.js
file..
helperFunctions
.checkSubscriptionId(apiEndpointUrl, tokenDetails)
.then((subscriptionId) => {
console.log("Sub id data::" + subscriptionId);
if (subscriptionId != null) {
cy.request({
method: "POST",
url:
apiEndpointUrl +
`/someurl/v1/subscription/${subscriptionId}/change`,
headers: {
Authorization: "Bearer " + tokenDetails,
},
body: {
userId: userId,
planCode: "plus_annual",
},
}).then((resp) => {
console.log("Change Subscription response ::" + resp);
const planName = resp.body.subscriptionChange.plan.name;
const planCode = resp.body.subscriptionChange.plan.code;
expect(resp.status).to.eq(200);
expect(planCode).to.contain("plus_annual");
expect(planName).to.contain("Plus Annual");
});
}
});
helperFunctions.js
class helperFunctions {
checkSubscriptionId = async (apiEndpointUrl, tokenDetails, retryNo = 0) => {
const options = {
headers: {
Authorization: "Bearer " + tokenDetails,
},
};
return await axios
.get(apiEndpointUrl + "/someurl/v1/user/info", options)
.then((res) => {
console.log("RESPONSE ==== : ", res);
const subscriptionId =
res.data.user.subscription.gateway_subscription_id;
if (retryNo >= NUM_RETRIES) {
throw new Error(
`Subscription not found after ${NUM_RETRIES} retries`
);
}
// If it is found, return
if (subscriptionId || retryNo >= NUM_RETRIES) {
return;
}
return new Cypress.Promise((resolve) => {
setTimeout(() => {
console.log("Retrying ::" + retryNo);
resolve(
this.checkSubscriptionId(apiEndpointUrl, tokenDetails, retryNo + 1)
);
}, 2000);
});
})
.catch((err) => {
console.log("ERROR: ====", err);
});
};
}