score:107
The method can be mocked in this way:
it('handleNameInput', () => {
let wrapper = shallow(<MyComponent/>);
wrapper.instance().searchDish = jest.fn();
wrapper.update();
wrapper.instance().handleNameInput('BoB');
expect(wrapper.instance().searchDish).toBeCalledWith('BoB');
})
You also need to call .update on the wrapper of the tested component in order to register the mock function properly.
The syntax error was coming from the wrong assingment (you need to assign the method to the instance). My other problems were coming from not calling .update()
after mocking the method.
score:0
@Miha's answer worked with a small change:
it('handleNameInput', () => {
let wrapper = shallow(<MyComponent/>);
const searchDishMock = jest.fn();
wrapper.instance().searchDish = searchDishMock;
wrapper.update();
wrapper.instance().handleNameInput('BoB');
expect(searchDishMock).toBeCalledWith('BoB');
})
score:14
Needs to be replaced wrapper.update();
with wrapper.instance().forceUpdate();
Source: stackoverflow.com
Related Query
- How to mock React component methods with jest and enzyme
- How to mock React component events with jest and enzyme
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- How to mock a React component lifecycle method with Jest and Enzyme?
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- How to test styles and media queries rendered by a React component with Jest and/or Enzyme
- Mock a custom service with jest and enzyme for a react component
- How to mock a method from a functional component in React using jest and Enzyme
- How to mock out sub-components when unit testing a React component with Jest
- How do you mock a react component with Jest that has props?
- How to mock API calls made within a React component being tested with Jest
- Mock React useRef or a function inside a functional component with enzyme and jest?
- Testing debounced function in React component with Jest and Enzyme
- How to test with jest and typescript with types a basic react function component
- React testing state of component with Jest and Enzyme
- How to test a React component that has Router, Redux and two HOCs... with Jest and Enzyme?
- How to replace a React component with a mock when testing with Jest
- How to test functions in React with Jest and Enzyme
- testing custom react methods with jest and enzyme
- Test an whole table React component cells with jest and Enzyme
- How to mock properly with Jest a React component using render props
- How to mock a React Component static method with Jest
- How to mock React Context for testing component with jest
- How to test a React component that update over time with Jest and Enzyme?
- Jest with React - How do you render an entire component and test it?
- How can I test if child React component was rendered based on a URL change using Jest and Enzyme
- How to test async handleSubmit(e) in react with jest and enzyme
- Using React Jest Enzyme - How to pass a constructor object to my component with Shallow?
- How to mock <AppContenxt.Consumer> in React using Jest and Enzyme
- Testing component function in React with Enzyme and Jest
More Query from same tag
- Prevent pasting -, +, --, ++, +- into input number
- Strange arrow function parameters behavior
- menuItem conditional rendering of item with boolean value
- Getting old values from variables and state
- Converting a dynamic HTML page to React/JSX
- Checkbox toggle in react
- How to handle Formik's `handleChange` prop?
- How to Change Value of Provider in React Hooks
- A filter method in React isn't working while a map is
- Does Storybook support async imports?
- How to use wavesurfer.js in React?
- How to slice JSON data in ReactJS?
- What is the Typescript type to accept any kind of React Component as prop?
- Create React App - Unable to set window object before test runs
- Using Material-UI System with Default Components
- Failed to execute goal maven-antrun-plugin - Connection timed out on GitHub Actions
- How can you play an audio file in React without the use of a button?
- Add edit fields on table row on click in reactjs without using react-table library
- Open Specific Menu in ReactJS
- how to pass pk using django and react
- CSS transform moves click handler?
- Getting TypeError: Cannot read property 'prototype' of undefined after modifying form
- Cypress get length of a table dynamically
- Unhandled Rejection outside Promise?
- How to fix "SyntaxError: Invalid or unexpected token @import"?
- React not re-rendering after a change in state, how do I setState of child components?
- React functional parent component sharing common onChange functions without redundant code
- How can I get return True with multiple condition?
- How do I render same component depending on the page the user is currently on?
- How to map through array of objects and create a new array of objects with a new added property using react and javascript?