score:4
this is how i have managed to spy on imported functions using jest.
import everything that is imported in the file you're testing.
mock it in the beforeeach
, you can use more complex mocking if you need to return values or whatever.
in the aftereach
call jest.clearallmocks()
to reset all the functions to normal to stop any mocking falling through to other tests.
putting it all together it looks something like this.
import shallow from 'enzyme'
import * as store from './store' // this should be the actual path from the test file to the import
describe('mocking', () => {
beforeeach(() => {
jest.spyon(store, 'handlecancel')
jest.spyon(store, 'create')
})
aftereach(() => {
jest.clearallmocks();
})
test.only('should handle cancel button click', () => {
const wrapper = shallow(<createform />);
const cancelbutton = wrapper.find('button').at(1);
cancelbutton.simulate('click');
expect(store.handlecancel).tobecalled();
})
})
also, in case you need to mock a default import you can do so like this. jest.spyon(store, 'default')
score:0
you forgot to tell jest how to mock the store module, in your case it is just undefined
.
const store = require('../../src/store');
jest.mock('../../src/store', () =>({
handlecancel: jest.fn()
}));
test.only('should handle cancel button click', () => {
const wrapper = shallow(<createform />);
const cancelbutton = wrapper.find('button').at(1);
cancelbutton.simulate('click');
expect(store.default.handlecancel).tobecalled();//i'm not sure about the default here
});
with this solution you tell jest to mock the store with an object that has the handlecancel
function which is a jest spy. on this spy you can then test that it was called.
Source: stackoverflow.com
Related Query
- Mock a dependency of a React component using Jest
- Using Jest to mock a React component with props
- How to mock async call in React functional component using jest
- How to mock properly with Jest a React component using render props
- How to mock imported React higher order component using jest & enzyme?
- How to mock a method from a functional component in React using jest and Enzyme
- How to mock React component methods with jest and enzyme
- Testing React Functional Component with Hooks using Jest
- How to mock history.push with the new React Router Hooks using Jest
- How do I get an attribute of an element nested in a React component using Jest and/or Enzyme?
- How to mock out sub-components when unit testing a React component with Jest
- mock useDispatch in jest and test the params with using that dispatch action in functional component
- Jest -- Mock a function called inside a React Component
- How do you mock a react component with Jest that has props?
- Testing an error thrown by a React component using testing-library and jest
- How to mock API calls made within a React component being tested with Jest
- Unit testing react component that makes ajax calls using JEST
- React component using jQuery without require - jest unit tests
- Mock a non-default function using Jest and React
- How to mock a React component lifecycle method with Jest and Enzyme?
- Jest Manual Mocks with React and Typescript: mock an ES6 class dependency
- Jest - mock fat arrow function within React component
- Mock setInterval method in react using jest test cases
- How to test below component in React using JEST
- Jest testing React component using Electron-Remote in ElectronJS
- React - Jest Mock an inner component
- Jest Testing Meteor React Component which is using withTracker in Container (meteor/react-meteor-data)
- How to replace a React component with a mock when testing with Jest
- Using StorybookJS in a component library package with React as peer dependency
- Jest mock async calls inside react component
More Query from same tag
- Correct way to update sate object field in React
- is filter best way to remove items in array? (react.js)
- Unable to render React page after refresh, instead showing raw data
- Reactjs and Pokeapi JSON
- Amplify for Govcloud?
- ReactJS - Unable to run babel code without serving HTTP files
- How to create insertion point to mount styles in shadow dom for MUI material v5 in React custom element
- Rerendering mapped array based on order
- Using React-Router to link within a page
- React cannot read object of custom js file
- How to retain checked value when filtering list
- Click onClick twice in <Link> to update state
- How does Facebook's React JSX transformer work?
- create-react-app is not working and it's taking forever, any suggestions?
- How to have the whole object act as optionValue in a SelctInput in react-admin?
- How to get returned data from one function in react
- React native button click move to another screen
- React: How can I watch for property changes of my UseRef variable (UseEffect doesn't work)
- Using react-router-dom
- Find duplicated element in array of objects
- products is not a function after destructuring
- How to add React tooltip on start of empty input field
- Can not provide/test saga when using a reselect selector
- React doesn't update the view even when Redux state is changed
- Uncaught TypeError: transactions.map is not a function in reacts JS
- Cannot save email in React.js + Express
- Cannot read property '' of undefined (react)?
- Getting SSR to work with the React Context API
- How to pass child index to parent
- Is there any way to use event object inside React's componentDidMount method?