enzyme

Jest spyOn function called

孤人 提交于 2019-11-28 05:17:58
I'm trying to write a simple test for a simple React component, and I want to use Jest to confirm that a function has been called when I simulate a click with enzyme. According to the Jest docs, I should be able to use spyOn to do this: spyOn . However, when I try this, I keep getting TypeError: Cannot read property '_isMockFunction' of undefined which I take to mean that my spy is undefined. My code looks like this: import React, { Component } from 'react'; import logo from './logo.svg'; import './App.css'; class App extends Component { myClickFunc = () => { console.log('clickity clickcty') }

How to mock React component methods with jest and enzyme

半世苍凉 提交于 2019-11-28 04:51:42
I have a react component(this is simplified in order to demonstrate the issue): class MyComponent extends Component { handleNameInput = (value) => { this.searchDish(value); }; searchDish = (value) => { //Do something } render() { return(<div></div>) } } Now I want to test that handleNameInput() calls searchDish with the provided value. In order to do this I would like to create a jest mock function that replaces the component method. Here is my test case so far: it('handleNameInput', () => { let wrapper = shallow(<MyComponent/>); wrapper.searchDish = jest.fn(); wrapper.instance()

Mocha will not recognise JSX

人走茶凉 提交于 2019-11-27 19:29:18
问题 I am trying to update my unit tests by using mocha and enzyme. The code that I am testing is in ES6, using JSX and React. I have been unable to get mocha to not error on the JSX in my test script. Test script: import React from 'react'; import assert from 'assert'; import { shallow } from 'enzyme'; import SamplePageMain from '../SamplePageMain'; describe('<SamplePageMain />', () => { var samplePage = shallow(<SamplePageMain />); it('should render', function () { assert.notEqual(samplePage,

Passing an event object to enzyme .simulate

只愿长相守 提交于 2019-11-27 17:22:21
问题 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

How to make Jest wait for all asynchronous code to finish execution before expecting an assertion

房东的猫 提交于 2019-11-27 17:12:05
问题 I am writing an integration test for for a React application, i.e. a test that tests many components together, and I want to mock any calls to external services. The issue is that the test seems to execute before the async callback is executed causing my tests to fail. Is there anyway around this? Can I somehow wait for call async code to finish? Here is some bad pseudo code to illustrate my point. I would like to test that when I mount Parent, its Child component render the content that came

Using Jest to spy on method call in componentDidMount

北城以北 提交于 2019-11-27 11:40:42
I recently wanted to test that some custom method gets conditionally called in the componentDidMount method of a React component. componentDidMount() { if (this.props.initOpen) { this.methodName(); } } I'm using Jest as my testing framework, which includes jest.fn() for mocks/spies. I've read that this would be fairly trivial to test with Sinon, by doing something like the following: sinon.spy(Component.prototype, "methodName"); const wrapper = mount(<Component {...props} />); expect(wrapper.instance().methodName).toHaveBeenCalled(); I'm trying to recreate this with Jest like so: Component

Enzyme - How to access and set <input> value?

天大地大妈咪最大 提交于 2019-11-27 11:02:21
问题 I'm confused about how to access <input> value when using mount . Here's what I've got as my test: it('cancels changes when user presses esc', done => { const wrapper = mount(<EditableText defaultValue="Hello" />); const input = wrapper.find('input'); console.log(input.render().attr('value')); input.simulate('focus'); done(); }); The console prints out undefined . But if I slightly modify the code, it works: it('cancels changes when user presses esc', done => { const wrapper = render(

When should you use render and shallow in Enzyme / React tests?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 10:27:02
prior to posting this question, I tried to search in sqa stackexchange but I found no post about shallow and render there, so I hope someone can help me out here. When should I use shallow and render in testing react components? Based on the airbnb docs, I've made some opinions on the difference of the two: Since shallow is testing components as a unit , so it should be used for 'parent' components. (ex. Tables, Wrappers, etc.) Render is for child components. The reason I asked this question, is that I'm having a hard time to figure out which one I should use (though the docs say that they're

Testing with React's Jest and Enzyme when simulated clicks call a function that calls a promise

北城余情 提交于 2019-11-27 09:29:34
React v15.1.0 Jest v12.1.1 Enzyme v2.3.0 I'm trying to figure out how to test a component that calls a promise in a function invoked by a click. I was expecting Jest's runAllTicks() function to help me out here, but it doesn't seem to be executing the promise. Component: import React from 'react'; import Promise from 'bluebird'; function doSomethingWithAPromise() { return new Promise((resolve) => { setTimeout(() => { resolve(); }, 50); }); } export default class AsyncTest extends React.Component { constructor(props) { super(props); this.state = { promiseText: '', timeoutText: '' }; this

Enzyme simulate an onChange event

不羁岁月 提交于 2019-11-27 09:01:17
I'm testing a react component with Mocha and Enzyme. Here is the component (shortened for simplicity of course): class New extends React.Component { // shortened for simplicity handleChange(event) { // handle changing state of input const target = event.target; const value = target.value; const name = target.name this.setState({[name]: value}) } render() { return( <div> <form onSubmit={this.handleSubmit}> <div className="form-group row"> <label className="col-2 col-form-label form-text">Poll Name</label> <div className="col-10"> <input className="form-control" ref="pollName" name="pollName"