score:0
assuming you import the getinitialdata
function from somewhere, a mock for that would look like this
jest.mock("./getinitialdata", () =>
jest.fn().mockresolvedvalue(initialresponsemock)
);
// rest of your test in here
the concept you are missing is that you need to use mockresolvedvalue
when mocking async code.
https://blog.jimmydc.com/mock-asynchronous-functions-with-jest/
score:2
this is the way i use the jest to test something after the api has finished fetching.
create mock function, (may be in other file to be used by all test cases, change it the way you want to setup)
const mockapi = new promise((resolve, reject) => {
settimeout(() => {
resolve('foo');
}, 300);
});
define apis
import * as apis from '~/api/[api_path]';
const mockgetinitialdata = (params = {}) =>
jest
.spyon(apis, 'getinitialdata')
.mockimplementation(() => mockapi(params));
test case
it('initial data loading failed', async () => {
mockgetinitialdata({
error: { errors: {name: 'invalid name' } }
});
});
Source: stackoverflow.com
Related Query
- Jest Unit test case for React functional component Api call
- Sending props to a React component for a Jest unit test
- React unit test case for a component having a LInk
- Jest Unit Test for React Hooks component with act() error
- How to write a test case for a simple React component using Jest and Enzyme
- How to write test case coverage in Jest and Enzyme for indexOf used inside React Component
- jest test case failing for React Component
- React Js API call Unit Test Case
- Unit Test For Functional Component React
- How to write test case for ErrorBoundary in React using Jest / Enzyme
- Update React Context using a REST Api call in a functional component
- How can I write a unit test for a react component that calls reduxjs's mapStateToProps?
- How to test the state of a functional component in React with Jest (No Enzyme)
- React Ref's offsetheight, always returns '0' for jest test case even after the content is loaded
- React - how do I unit test an API call in Jest?
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- react jest unit test case TypeError: Cannot read property 'then' of undefined
- How to mock async call in React functional component using jest
- How to test react functional component correctly using jest and enzyme?
- How to test a handle function call with a react functional component with hooks
- get state from redux react component in jest unit test
- How to test react functional component async call
- Is there a way to unit test an innaccessible callback function that is called from a child React component using jest
- How to Account for Google reCaptcha in Jest Unit Test with NextJS API
- Test case for checking blob response in React Jest
- Jest test case for UseEffect hooks in react JS
- How to test api call in react component and expect the view change after success of api call?
- Trying to unit test a React component using Jest and Enzyme
- How to unit test a React functional component using hooks useEffect,useDispatch and useSelector and Redux?
- My jest unit test case is failing for Material UI multi select
More Query from same tag
- React - wrap multiple elements in parent node
- React tests are passing with incorrect props
- Typescript error: Property 'remove' does not exist on type 'EventTarget'
- checkbox internal state overriding prop
- Components displayed vertically after adding DatePicker
- React - set property of object by setState
- How to disable click event while fetching the data in reactjs witout jquery?
- Use variables declared in the webpack config inside js file
- Material UI DateRangePicker how to pick year to year only
- Deck.gl (Mapbox) StaticMap doesn't resize - overwrites everything on the screen
- Better Approach to successive dispatches?
- How a script with type "text/babel" is transpiled when put on the HTML page?
- How to assign value to a state inside a map function of a reducer
- Component Re-renders after clicking NavLink & loads same data Multiple times
- How to install SSL for the following setup (React Frontend + Nodejs Backend + Custom Domain Heroku)
- Using Lambda/API Gateway in ReactJS
- creating common header and sidebar across all the page using react router
- Automaticly update component when item delete
- Dependant Select Components not showing the Selected Value in Input Field
- Can I disable one of the tabs in the bottomtabnavigator? React Native
- How to create a proxy to access an external API?
- Bug: React does not recognize the allowTransparency prop on a DOM element
- React does not rerun App() function when switching routes
- Axios not sending authorization header - ReactJS
- Extracting Data From API
- Setting ReactNative's NativeModules.RNDeviceInfo with Jest
- React video rendering
- React/Firebase -> DocumentReference.set() called with invalid data. Unsupported field value: undefined
- Jest test Create React APP index.js with new version 18
- Cant pass array of objects to setPeople function in React