score:1
Accepted answer
the problem is what you expect
ed is not the mocked method. you should expect the mocked method like this:
expect(store.handleloginsubmit).tohavebeencalledtimes(0)
and similar to other expect
.
in the end of your test case, you should restore the mocked method by calling .mockrestore()
:
store.handleloginsubmit.mockrestore()
if after this, you still get issue with async call, here's my suggestion that makes your tests be more ...unit test: you should separate the test into 2 test cases, one to test if store.handleloginsubmit
is called after form is submitted, and the other one is to test what inside store.handleloginsubmit
,
test('.handleloginsubmit() called when form is submitted', () => {
jest.spyon(store, 'handleloginsubmit').mockimplementation(() => {})
const wrapper = shallow(<login store={store} />);
wrapper.find('form').simulate('submit');
expect(store.handleloginsubmit).tobecalled()
store.handleloginsubmit.mockrestore()
})
test('.handleloginsubmit()', async () => {
jest.spyon(service, 'validateuser')
.mockimplementation(() => promise.resolve(true))
await store.handleloginsubmit({preventdefault: () => {}})
expect(service.validateuser).tobecalled()
service.validateuser.mockrestore()
})
Source: stackoverflow.com
Related Query
- Jest - function not calling its child async function
- How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339
- React async / await is not waiting in the calling function - but the called function is executing correctly
- React ES6: calling async function from another function not defined
- How do I change state within a child component by calling function in its parent?
- Props defined by async function by Parent in UseEffect passed to a child component don't persist during its UseEffect's clean-up
- Jest test fails : TypeError: window.matchMedia is not a function
- Jest URL.createObjectURL is not a function
- Jest Expected mock function to have been called, but it was not called
- ReactJS: "this.props" is not a function when child component calls parent
- Calling async function inside non-async function in React-Native - Firebase
- JEST throws .finally is not a function
- Jest : TypeError: require(...) is not a function
- react calling a function in a child component
- Calling one async function inside another in redux-thunk
- Jest testing with Node - Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout
- Jest onSpy does not recognize React component function
- React onClick event not firing when function is passed to child
- In Jest test it says withRouter () is not a function
- Jest - assert async function throws test fails
- Mocking a function jest but jest calling original function
- Jest error TypeError: (0 , _jest.test) is not a function
- Why does calling the set function of a useState hook apply immediately in async functions?
- Jest failing with TypeError: (0 , _user.default) is not a function
- In React why does calling a function from another function in our code not work and display?
- ag-grid Jest testing - this.btFirst.insertAdjacentElement is not a function
- Jest mock async function from default import
- getInitialProps does not work with async function
- classnames_1.default is not a function when testing with Jest
- How to mock async function using jest framework?
More Query from same tag
- Create new Date/Time using separate Date and Time
- How do i use componentWillReceiveProps() correctly?
- Validate input with only number and decimal after dot
- Reducer not being called in my Redux setup
- Simulating a click on React with Enzyme not doing anything
- Storybook couldn't resolve fs
- onSelectSlot doesn't work in Mobile react Big calendar
- Understanding ReactJS Controlled form components
- Module not found: Error: Package path ./react/swiper-react is not exported from package
- Can we send the JSON obtained from an API as a child component, instead of each individual attribute of the object?
- Exporting values from a Component in React
- Request handling with React, Redux and Websocket
- Implement filter functionality using react.js
- Gatsby - page refresh corruption
- Change body background with data-theme attribute on click with React
- 3 conditional views in render
- Components do not change when a hooked element is Changed
- How to access fetched data through graphql query?
- How to Display Ticks and Labels for Month Numbers for 4 years in the X-Axis using Recharts
- How do I add user input inside an object array with useState?
- Loading in Datagrid throw Rendered more hooks than during the previous render
- How to change a function passed as a prop to React component?
- ReactJS Find Array Prevent Return Undefined
- How can I select a state property object name dinamically to change one of its properties(childs?) on react?
- When I submit the form in childComponent my page is refreshing in ReactJS
- Why isn't text-color working in Tailwind css + next.js project
- How do I convert componentDidMount() and componentWillUnmount to the useEffect()?
- Submit handler doesn't work correctly in ReactJS?
- How To Convert base64 String to PDF in React.js
- Integrating twig with react.js