enzyme

Testing React Functional Component with Hooks using Jest

笑着哭i 提交于 2019-11-30 13:51:06
问题 So I'm moving away from class based components to functional components but am stuck while writing test with jest/enzyme for the methods inside the functional components which explicitly uses hooks. Here is the stripped down version of my code. function validateEmail(email: string): boolean { return email.includes('@'); } const Login: React.FC<IProps> = (props) => { const [isLoginDisabled, setIsLoginDisabled] = React.useState<boolean>(true); const [email, setEmail] = React.useState<string>(''

How to spy componentWillMount using jest and enzyme

喜欢而已 提交于 2019-11-30 12:53:05
I am trying to test whether componentWillMount was called and for that my test is test('calls `componentWillMount` before rendering', () => { let fn = jest.fn(SomeComponent.prototype.componentWillMount) mount(<SomeComponent />) expect(fn).toHaveBeenCalled() }) But even though the componentWillMount method is called, the test does not pass. What am I missing here? I don't know if the other answers have helped with your question, but you shouldn't need to test componentWillMount. React should already do that testing for you. More relevant to your testing would be to test the functions or actions

How to unit test a react event handler that contains history.push using Jest and Enzyme?

十年热恋 提交于 2019-11-30 12:41:55
Given a simple component: export default class SearchForm extends Component { constructor(props) { super(props) this.state = { query: '' } } onSubmit = (event) => { event.preventDefault() history.push(`/results/${this.state.query}`, { query: this.state.query }) } render() { return ( <form onSubmit={this.onSubmit}> <input type="text" value={this.state.query} onChange={event => this.setState({ query: event.target.value })} /> <button>Search</button> </form> ) } } And the test: describe('SearchForm Component', () => { it('should navigate to results/query when submitted', () => { const wrapper =

eslint should be listed in the project's dependencies, not devDependencies

六月ゝ 毕业季﹏ 提交于 2019-11-30 11:37:28
问题 Either I don't understand dependencies vs. devDependencies in node 100% yet or eslint is just wrong here (not capable of analyzing this correctly): 3:1 error 'chai' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies 4:1 error 'chai-enzyme' should be listed in the project's dependencies, not devDependencies import/no-extraneous-dependencies 5:1 error 'enzyme' should be listed in the project's dependencies, not devDependencies import/no

Redux: How to test a connected component?

ⅰ亾dé卋堺 提交于 2019-11-30 11:28:24
问题 I am using Enzyme to unit test my React components. I understand that in order to test the raw unconnected component I'd have to just export it and test it (I've done that). I have managed to write a test for the connected component but I am really not sure if this's the right way and also what exactly would I want to test for the connected component. Container.jsx import {connect} from 'react-redux'; import Login from './Login.jsx'; import * as loginActions from './login.actions'; const

Passing an event object to enzyme .simulate

﹥>﹥吖頭↗ 提交于 2019-11-30 11:11:41
I am using Jest and Enzyme to test a React checkbox component. This is my test: it('triggers checkbox onChange event', () => { const configs = { default: true, label: 'My Label', element: 'myElement', } const checkbox = shallow( <CheckBox configs={configs} /> ) checkbox.find('input').simulate('click') }) I get this error however when running the test: TypeError: Cannot read property 'target' of undefined This is the input for my component: <div className="toggle-btn sm"> <input id={this.props.configs.element} className="toggle-input round" type="checkbox" defaultChecked={ this.props

Testing the `React.createRef` api with Enzyme

最后都变了- 提交于 2019-11-30 09:32:13
问题 I would like to test the following class, which uses the React.createRef api. A quick search didn't reveal any any examples of doing this though. Has anyone had success? How would I go about mocking the ref? Ideally I'd like to use shallow . class Main extends React.Component<Props, State> { constructor(props) { super(props); this.state = { contentY: 0, }; this.domRef = React.createRef(); } componentDidMount() { window.addEventListener('scroll', this.handleScroll); handleScroll(); }

Using Jest to mock a React component with props

让人想犯罪 __ 提交于 2019-11-30 08:30:40
I have a React component which contains some other components that depend on access to a Redux store etc., which cause issues when doing a full Enzyme mount. Let's say a structure like this: import ComponentToMock from './ComponentToMock'; <ComponentToTest> ...some stuff <ComponentToMock testProp="This throws a warning" /> </ComponentToTest> I want to use Jest's .mock() method to mock out the sub-component, so that it is not a concern for the test. I'm aware that I can mock out a straight component with something like: jest.mock('./ComponentToMock', () => 'ComponentToMock'); However, as this

Unit testing React click outside component

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 08:24:42
Using the code from this answer to solve clicking outside of a component: componentDidMount() { document.addEventListener('mousedown', this.handleClickOutside); } componentWillUnmount() { document.removeEventListener('mousedown', this.handleClickOutside); } setWrapperRef(node) { this.wrapperRef = node; } handleClickOutside(event) { if (this.wrapperRef && !this.wrapperRef.contains(event.target)) { this.props.actions.something() // Eg. closes modal } } I can't figure out how to unit test the unhappy path so the alert isn't run, what i've got so far: it('Handles click outside of component', () =>

Enzyme expects an adapter to be configured

。_饼干妹妹 提交于 2019-11-30 08:02:05
I created a new React application by create-react-app and I wanted to write a unit test to a component named "MessageBox" that I created in the application. This is the unit test that I wrote: import MessageBox from "../MessageBox"; import { shallow } from 'enzyme'; import React from 'react'; test('message box', () => { const app = {setState: jest.fn()}; const wrapper = shallow(<MessageBox app={app}/>); wrapper.find('button').at(0).simulate('click'); expect(app.setState).toHaveBeenLastCalledWith({modalIsOpen: false}); }); I also added a file under 'src' folder named 'setupTests.js' with the