score:3
Accepted answer
Without actually testing it, I believe this is what you need:
it('handleChange() should set state.term', () => {
const togglePosition = jest.fn();
const event = { target: { value: 'test' } };
const wrapper = shallow(<Child togglePosition={togglePosition} />);
wrapper.find('#target-input').simulate('change', event);
const state = wrapper.instance().state;
expect(state).toEqual({ loading: 'loading', term: 'test' });
expect(togglePosition).toHaveBeenCalledWith('test');
})
Mock the passed function: const togglePosition = jest.fn();
, and test the condition/response: expect(togglePosition).toHaveBeenCalledWith('test');
.
Source: stackoverflow.com
Related Query
- How to test function which returns a random React component in Jest
- Using Jest and Enzyme, how do I test a function passed in through props?
- How to test with jest and typescript with types a basic react function component
- How do I test a React component that calls a function on its parent, which changes props on the component?
- How to test a return value of a mock function in jest
- How to correctly create a test in ReactJs with Jest when the function uses an if statement
- How do I test string that is being passed as props jest enzyme ReactJS
- Jest | Enzyme how to test if function inside a component is being called during onClick event?
- Unit test reactjs component function with Jest and Enzyme
- How to write test for the HTML element being passed as a prop to a component a and a function is being called from that
- How can I test a function of a function component that uses React.useState() using jest enzyme testing?
- How to test an async function which does an axios request to an api using JEST in a React application
- How to mock function which is called twice jest
- Jest - how to mock a function which is invoked in module creation
- How to test a ReactJS function which uses hooks and does not return JSX
- How do I mock a async function in a component when testing reactjs with enzyme and jest?
- How to test a function that updates state in a component with jest
- How do I mock a function being used by my React Component in Jest testing?
- How to test if state of a component is changed by the function using jest and Enzyme in React TDD
- How to test a function call after state receive value inside expression in React function component using Jest
- How to test changes made by onClick event that calls a setState function, which is passed from another component and changes UI?
- How to write test case for a component which is calling a method using jest
- How to do component testing reactjs which is passed to HOC?
- how to change jest mock function return value in each test?
- How to mock React component methods with jest and enzyme
- Figuring out how to mock the window size changing for a react component test
- Test a React Component function with Jest
- How to pass a function as an argument to a ReactJS component in TypeScript
- How to mock out sub-components when unit testing a React component with Jest
- How to unit test useEffect cleanUp return function using Jest and Enzyme
More Query from same tag
- react-router: Cannot read property 'push' of undefined
- How to get divs to respond to screen resizing like imgs?
- Uable to make a req from one server to another - "Error: Request failed with status code 401"
- React router chunk bundle not fetched when visited that route
- How to assign variable id or classname to a div in ReactJS
- React + Redux subscribe to action outside of mapStateToProps
- Transform moment timestamp into different format string
- how to pause animation in multiple elements on hover on one element?
- Is there a way to integrate Django with Next.js?
- javascript find() in nested array
- regular expression to validate a pattern
- conditionally render !important inline style
- React Controlled Component Doesn't Rerender/Update Like I Expect
- Using animations on components with render
- React Hooks: setState hook inside map function
- How can I bind a button rendered by a React component to a function in that component?
- Material-UI: Change autofill background color in TextField
- ERROR in ./src/client-app/index.js - Module build failed: Error: Cannot find module '@babel/core'
- Why does the form not receive select data in AntD?
- React-typescript and rendering array of components
- Minified React error in react while navigation one component to anther
- How to Fit Boundaries and Center React Google Maps
- Why is my React show/hide label not updating correctly?
- Update array index value in react
- Trying to preview image before uploading
- useEffect tries to read property of object before the state was updated in React
- Open Pop Up on Click Outside of Map
- Routes not rendering with react-router-dom
- how to remove wrapper div in react js?
- How to get absolute URL in production?