Код: Выделить всё
describe('App', () => {
it('succeeds fetching data',
async () => { const promise = Promise.resolve({ data: { hits: stories,},
});
axios.get.mockImplementationOnce(() => promise);
render();
expect(screen.queryByText(/Loading/)).toBeInTheDocument();
await waitFor(async () => await promise);
expect(screen.queryByText(/Loading/)).toBeNull();
});
});
Код: Выделить всё
React.useEffect(() => {
handleFetchStories();
}, [url);
Код: Выделить всё
// handleFetchStories
const handleFetchStories = React.useCallback(async () => {
dispatchStories({ type: 'STORIES_FETCH_INIT' });
const result = await axios.get(url);
dispatchStories({ type: 'STORIES_FETCH_SUCCESS',
payload: result.data.hits,
});
try {
const result = await axios.get(url);
dispatchStories({ type: 'STORIES_FETCH_SUCCESS',
payload: result.data.hits,});
}
catch {
dispatchStories({ type: 'STORIES_FETCH_FAILURE' });
}
}, [url]);
// StoriesReducer
const storiesReducer = (state, action) => {
switch (action.type) {
case 'STORIES_FETCH_INIT':
return { ...state,
isLoading: true,
isError: false,
};
case 'STORIES_FETCH_SUCCESS':
return { ...state,
isLoading: false,
isError: false,
data: action.payload,
};
case 'STORIES_FETCH_FAILURE':
return { ...state,
isLoading: false,
isError: true,
};
case 'REMOVE_STORY':
return { ...state,
data: state.data.filter( (story) => action.payload.objectID !== story.objectID ),
};
default:
throw new Error();
}
};
< /code>
Моя проблема с этой строкой: < /p>
await waitFor (async () => await promise)работает код? < /P>
Подробнее здесь: https://stackoverflow.com/questions/796 ... to-waitfor
Мобильная версия