score:1
Did you try selecting the button by text :
describe('RecipeSearch', () => {
test('test clicking the button triggers the onSubmit function', () => {
const onSubmit = jest.fn();
render(<RecipeSearch onSubmit={onSubmit} />);
userEvent.selectOptions(screen.getByRole('combobox'), 'Sour');
userEvent.click(screen.getByText('Search'));
expect(onSubmit).toHaveBeenCalled();
});
});
I'm not sure how getByRole handles a second argument in your first try, but getByText should work.
score:3
Creating an onSubmit
mock and passing it as a prop won't work since the onSubmit
callback is internal to the component and not a prop - you don't have access to it from the test.
Rather than testing if the onSubmit
has been called, you should test the result of triggering the submit event. Which in this case could mean verifying that the axios
request is made.
See How do I test axios in Jest? for examples on how to mock axios
in your test.
Source: stackoverflow.com
Related Query
- how to test button that call submit form using jest and react testing library
- How to properly test React Native Modals using Jest and Native Testing Library
- how to test Routes in react using jest and react testing library
- How to Test React's Error Boundary using Jest and React Testing Library
- Test onClick of a button is called when there is no props passed down to it using testing library react and jest
- How to test conditionally rendered string using React testing library and jest
- How to test a className with the Jest and React testing library
- How to unit test a react event handler that contains history.push using Jest and Enzyme?
- How can I find and click a button that has no text using React Testing Library?
- How to test redux state update with react testing library and jest
- How to test axios api using jest and react testing library?
- How to call API and stores results in a state when a form submit is clicked using react hooks
- How to mock a nested component import in react js and test using react testing library
- Asserting prop call for form submit in React using React Testing Library
- How can I mock an imported React hook/module and test that it's being called properly on different test cases using Jest
- Unable to spyon click on button using jest and react testing library for a component testcase
- How to write a test with Jest and React testing library to check if anchor-tag <a /> exist
- how to test component while passing boolean value with react testing library and jest
- Jest + React Testing Library: how do I mock out a method from some library that only affects one test block
- How to submit form component in modal dialogue using antd react component library
- How to test form submission in React with Jest and Enzyme? Cannot read property 'preventDefault' of undefined
- Jest and React Testing Library, how to test if an element is hidden?
- How can I test functions that are provided by context using jest and react-testing-library?
- How to test API calls in react using jest and enzyme?
- How to test form submit with jest and enzyme in react?
- how can I test if useState hook has been called with jest and react testing library?
- Unit test form submission with data using react testing library
- How to test onClick props of a button using Jest and Enzyme
- How to write test cases for tabs in material-ui using react testing library
- How to test a React component that has Router, Redux and two HOCs... with Jest and Enzyme?
More Query from same tag
- How handle Fetch failures in Redux's action creators
- Rendering array pictures from calling API in react.js
- Problem hosting react app at sub route of website - multiple websites one IP address NGINX
- Typescript issue with onClick with styled-components
- NPM install --global command Proxy Issue
- React dropdown function triggers all dropdowns instead of selected menu
- TypeError: Cannot read property 'map' of undefined while fetching data from the server
- nodejs & react: render another HTML file upon request
- How do I make a number quickly rise from zero when rendering a page in React.js?
- React Maximum update depth exceeded in functional component
- What's the proper way to pass dependencies between components in React?
- React Redux: How to call multiple dependent actions in sequence
- my page is not rendering with different id on the same page when i use link in react components
- Redirect to nested route with react-router v4
- How to combine 2 arrays into 1 React
- how to hide play button until image loaded in javascript
- How to get distinct value using lodash in ReactJS?
- Axios does not update state immediately
- Changing to '@mui/material' makes the components not rendered
- How to achieve callbacks in Redux-Saga?
- how to deploy successfully React app with nextjs to AWS Amplify?
- Only one function getting result
- ReactJS SyntaxError: Unexpected token
- Unable to do operations on specific thing
- Reactjs - SetTimeout with state change re-render
- How to add slide animation on multiple buttons in react?
- How to load web worker using webpack's worker loader
- Can not import module: You may need an appropriate loader
- changes to state issued from custom hook not causing re-render even though added to useEffect
- Stuck on adding image url and normal url in props