reactjs-testutils

Testing React component onClick event with multiple actions in it

巧了我就是萌 提交于 2020-02-06 05:16:15
问题 Can't figure it out how to test onClick function with multiple actions in it. onButtonClick = function(action){ this.props.otherAction(); action(); } ... <Button bsStyle="success" bsSize="small" onClick={onButtonClick.bind(this,someAction}> Something </Button> And the test I currently have is like this. const onButtonClick = function (action) { actions.otherAction(); action(); }; it('Should include a button with multiple actions on onClick event.', function () { const button =

Mocking file input in React TestUtils

蹲街弑〆低调 提交于 2020-01-03 07:06:05
问题 I have a component with a following render f-tion: render: function() { <input type="file" name: this.props.name, className={this.props.className} onChange={this.props.handleChange} accept={this.props.accept}/> } State is managed by a container which uploads the file server-side using jquery AJAX call: getInitialState: function() { return { uploaded: false }; } handleChange: function(event) { event.preventDefault(); var file = event.target.files[0]; if (!file) { return; } var reader = new

nextState on componentWillUpdate not correct while testing with Jest (using also react-router wrapper)

核能气质少年 提交于 2020-01-03 06:00:09
问题 I am using Jest 0.4.0. I have a component wrapped into this (from react-router docs): var stubRouterContext = (Component, props, stubs) => { function RouterStub() { } Object.assign(RouterStub, { makePath () {}, makeHref () {}, transitionTo () {}, replaceWith () {}, goBack () {}, getCurrentPath () {}, getCurrentRoutes () {}, getCurrentPathname () {}, getCurrentParams () {}, getCurrentQuery () {}, isActive () {}, getRouteAtDepth() {}, setRouteComponentAtDepth() {} }, stubs) return React

what is adapter in enzyme

纵饮孤独 提交于 2019-12-23 18:53:27
问题 Any documentation on what's the purpose of adapter in enzyme testing library. import { configure } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; configure({ adapter: new Adapter() }); 回答1: Any documentation on what's the purpose of adapter in enzyme testing library. The closest it gets is to say "You will need to install enzyme along with an Adapter corresponding to the version of react (or other UI Component library) you are using". The docs mostly just explain how to

Jest issue - ReactElement.js: Failed to get mock metadata

谁都会走 提交于 2019-12-23 12:15:41
问题 I've a very simple test /** @jsx React.DOM */ var store = require('../simpleStore'); var TestUtils = require('react/addons').addons.TestUtils; describe("my tests",function(){ it('simple test',function(){ var x=10, y=20; expect(x).not.toBe(y); }) }) Test works fine as long as I dont require TestUtils. The moment I add any node_module reference, I start seeing below error Using Jest CLI v0.8.0, jasmine1 FAIL build/stores/__tests__/simple-test.js ● Runtime Error Error: ../stores/__tests__/simple

Change element size using Jest

别等时光非礼了梦想. 提交于 2019-12-23 07:27:14
问题 I have following code in my component var rect = ReactDOM.findDOMNode(this).getBoundingClientRect(); I use d3js and render graph in the component. But when I run test there are any svg tags. I assume that it happens because all rect's fields equals 0. Here is output for console.log(rect) in browser: ClientRect {top: 89, right: 808, bottom: 689, left: 8, width: 800…} and when I run test: { bottom: 0, height: 0, left: 0, right: 0, top: 0, width: 0 } So is there a way to set size of the element?

Testing for mouse wheel events

心不动则不痛 提交于 2019-12-22 04:56:06
问题 I've set up a simple function to handle mouse wheel events on a menu component I built. The component works fine, I'm trying to write a unit test around it and that is giving me an issue. component handler: handleWheel: function (event) { (event.deltaY < 0 ) ? this.moveUp() : this.moveDown(); return false; } this.moveUp() / this.moveDown() simply sets the state of firstIndex The issue is that when I try to write a test for this functionality I can't get it to work. I'm almost 100% certain it

React component using jQuery without require - jest unit tests

笑着哭i 提交于 2019-12-22 04:09:56
问题 I have a very simple React mixin which uses jQuery to trigger an event MyMixin = { trackStructEvent: function () { args = Array.prototype.slice.call(arguments); $('body').trigger('myEvent', args); } module.exports = MyMixin This is imported into the main site as part of a new set of components using browserify. As the main site holding these components will always include jQuery, I don't want to require jQuery with browserify, as it will be duplicated. This isn't an issue in terms of

React TestUtils, how can I simulate document mouseMove?

一曲冷凌霜 提交于 2019-12-10 18:01:00
问题 I want to use TestUtils.Simulate.mouseMove on the document . I have a component Dragger that adds a mouseMove event listener to the document . Here is an incomplete version: // Dragger.js 'use strict'; var React = require('react'); export default React.createClass({ propTypes: { handleDrag: React.PropTypes.func // callback set by parent }, getInitialState: function() { return {dragging: false} }, componentDidUpdate: function(props, state) { // if (this.state.dragging && !state.dragging) {

Simulate keydown on document for JEST unit testing

女生的网名这么多〃 提交于 2019-12-09 02:51:45
问题 Using JEST to unit test a component that has a keydown listener attached to the document. How can I test this in JEST ? How do I simulate the keydown event on the document? I need the event listener to be on the document since it is supposed to respond the keyboard action irrespective of the focussed element. EDIT: The question here is about simulating the event on the document or the document.body. All the examples are regarding an actual DOM node, that works fine but the document does not.