i have a function named "exchange" that depends of another named "getQuote", that returns a value from an api. How can i make "getQuote" always return a specific value to test "exchange"
getQuote function:
async function getQuote(from_symbol: string, to_symbol: string): Promise<any>{
const options = {
method: 'GET',
url: 'https://alpha-vantage.p.rapidapi.com/query',
params: {
from_symbol: from_symbol,
function: 'FX_INTRADAY',
interval: '5min',
to_symbol: to_symbol,
outputsize: 'compact',
datatype: 'json'
},
headers: {
'X-RapidAPI-Key': API_KEY,
'X-RapidAPI-Host': 'alpha-vantage.p.rapidapi.com'
}
};
try{
let response = await axios.request(options);
let QuoteData = await response.data;
let current_time = (QuoteData["Meta Data"]["4. Last Refreshed"]);
let quote_data = (QuoteData["Time Series FX (5min)"][current_time]);
let quote_value: number = parseFloat(quote_data["1. open"]);
return quote_value
}catch(err){
console.log(err);
}
};
exchange function:
async exchange(cash: number, from_coin: string, to_coin: string): Promise<void>{
if(this.cash[from_coin] < cash) return;
this.removeCash(cash,from_coin);
let quote: number = await getQuote(from_coin, to_coin);
let converted_cash = cash * quote;
this.deposite(converted_cash, to_coin);
}
my unitary test:
describe("testing user methods", ()=>{
const user = new User({cash: {"BRL": 100, "USD": 0}});
it("exchange should work", ()=>{
let value = 100;
let from_coin = "BRL";
let to_coin = "USD";
user.exchange(value,from_coin,to_coin);
expect(user.cash["USD"]).toBe(19.2);
})
});
i tried to mock "axios" but it always returns undefined, i don't have idea what to do;