I'm trying to test if the function searchRestaurantsHelper is called when searchRestaurant is called.
The problem is the received number of times is always 0.
Here's the test:
describe('Testing searchRestaurants', () => {
afterEach(() => {
jest.clearAllMocks();
});
test('searchRestaurantsHelper called once', async () => {
axios.get.mockImplementationOnce(() => {
const response = { data: { businneses: ['foo'] } };
return Promise.resolve(response);
});
searchRestaurantsHelper.mockImplementationOnce(() => 'foo');
await Yelp.searchRestaurants({
what: 'tacos',
where: 'rome',
sortBy: 'rating',
});
expect(searchRestaurantsHelper).toHaveBeenCalledTimes(1);
});
// other tests
}
I create an axios mock since searchRestaurants wouldn't run searchRestaurantsHelper if response.data.businesses.length === 0. Then I create a mock function for searchRestaurantsHelper but apparently it's not being called.
Here's searchRestaurants()
const Yelp = {
// Returns restaurant search resuts
async searchRestaurants(text) {
try {
let response = await axios.get(
`https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?limit=12&term=${text.what}&location=${text.where}&sort_by=${text.sortBy}`,
{
headers: {
Authorization: `Bearer ${YELP_API_KEY}`,
'X-Requested-With': 'XMLHttpRequest',
'Access-Control-Allow-Origin': '*',
},
}
);
if (response.data.businesses.length === 0) {
return [];
}
return searchRestaurantsHelper(response);
} catch (e) {
console.log(e);
}
},
}
And searchRestaurantsHelper()
export const searchRestaurantsHelper = (response) =>
response.data.businesses.map((business) => {
return {
id: business.id,
image: business.image_url,
name: business.name,
url: business.url,
price: business.price,
phone: business.phone,
categories: business.categories[0].title,
address: business.location.display_address[0],
};
});
Can anyone explain to me what I'm doing wrong? I've been stuck for a while... Thanks for your help!
Aucun commentaire:
Enregistrer un commentaire