Я пытаюсь проверить, вызывается ли функция searchRestaurantsHelper
при вызове searchRestaurant
.
Проблема в том, что полученное количество раз всегда равно 0.
Вот test:
import axios from 'axios';
import Yelp from './Yelp';
import {
getRestaurantInfoHelper,
searchRestaurantsHelper,
searchDefaultRestaurantsHelper,
} from './utils';
jest.mock('./utils.js');
jest.mock('axios');
describe('Testing searchRestaurantsInfo', () => {
afterEach(() => {
jest.clearAllMocks();
});
test('searchRestaurantsInfo called once and returns something', async () => {
await Yelp.searchRestaurantsInfo('q_IoMdeM57U70GwqjXxGJw');
getRestaurantInfoHelper.mockImplementation(() => 'foo');
expect(getRestaurantInfoHelper).toHaveBeenCalledTimes(1);
await expect(
Yelp.searchRestaurantsInfo('q_IoMdeM57U70GwqjXxGJw')
).resolves.toEqual('foo');
});
test('axios.get called twice', async () => {
await Yelp.searchRestaurantsInfo('q_IoMdeM57U70GwqjXxGJw');
expect(axios.get).toHaveBeenCalledTimes(2);
});
});
describe('Testing searchRestaurants', () => {
afterEach(() => {
jest.clearAllMocks();
});
test('searchRestaurantsHelper called once', async () => {
axios.get.mockImplementationOnce(() => {
const response = { data: { businneses: ['foo'] } };
return Promise.resolve(response);
});
searchRestaurantsHelper.mockImplementation(() => 'foo');
await Yelp.searchRestaurants({
what: 'tacos',
where: 'rome',
sortBy: 'rating',
});
expect(searchRestaurantsHelper).toHaveBeenCalledTimes(1);
});
test('axios.get called once', async () => {
await Yelp.searchRestaurants({
what: 'tacos',
where: 'rome',
sortBy: 'rating',
});
expect(axios.get).toHaveBeenCalledTimes(1);
});
});
Я создаю макет топора ios, поскольку searchRestaurants
не запускается searchRestaurantsHelper
, если response.data.businesses.length === 0
. Затем я создаю фиктивную функцию для searchRestaurantsHelper
, но, видимо, она не вызывается.
Вот 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);
}
},
}
И searchRestaurantsHelper () в './utils'
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],
};
});
Может кто мне объяснить, что я делаю не так? Я застрял на какое-то время ... Спасибо за вашу помощь!