Created
May 5, 2018 16:45
-
-
Save timothyjellison/a9c9c2fdfb0b30aab5698dd92e901b24 to your computer and use it in GitHub Desktop.
Mocking a component constructor with Jest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An answer for this stackoverlow question: https://stackoverflow.com/questions/48831701/jest-how-to-mock-default-export-component-when-same-module-also-has-named-expor | |
import React from 'react'; | |
import { shallow } from 'enzyme'; | |
import App, { myUtilityFunction } from './App'; | |
jest.mock('./App'); | |
App.mockImplementation(() => { | |
return { | |
render: () => <div>MockApp</div> | |
}; | |
}); | |
myUtilityFunction.mockImplementation(() => 'foo'); | |
it('renders the mock React component', () => { | |
/** | |
* PASSES | |
*/ | |
expect(shallow(<App />).getElement()).not.toEqual(null); | |
}); | |
it('runs the utility method correctly', () => { | |
/** | |
* PASSES | |
*/ | |
expect(myUtilityFunction()).toEqual('foo'); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment