In my code, I have a function that handles API requests called "private" and several other functions that initiate specific requests with configuration objects. For example, in the requestUploadStatementFile
file.
I want to test these public functions, but I'm unsure how to mock the private function using Jest, specifically the requestWithAutoTokenRenew
function.
/**
* An API wrapper that automatically renews JWT tokens once they expire
*
* @param {Object} requestConfig Request configuration object
* @returns {Promise}
*/
const requestWithAutoTokenRenew = async requestConfig => {
const session = await doGetSession();
const sessionToken = session.idToken.jwtToken;
const { url, method, params, payload } = requestConfig;
const requestObj = {
url,
method,
headers: {
Accept: "application/json",
Authorization: sessionToken,
"Content-Type": "application/json"
},
data: payload,
...params
};
return axios.request(requestObj).then(response => response.data);
};
/**
* Upload bank or credit card statement for parsing
*
* @param {Object} file Statement PDF file needs to be parsed
*/
export const requestUploadStatementFile = file => {
const requestConfig = {
url: URL_UPLOAD,
method: "POST",
payload: file
};
return requestWithAutoTokenRenew(requestConfig);
};