简体   繁体   English

我可能是一个简单的玩笑/反应错误

[英]Probably a simple jest/react error on my part

I am trying to mock a component that has the following structure: 我正在尝试模拟具有以下结构的组件:

//A_Form.js
import React from "react";

export default React.createClass({
    displayname: "A_Form",

    updateState: function() {
        // I want to mock this function as it opens a web connection
    }    
    render: function() {
        return ( 
            <form>
                <div className='class1'>
                    <label htmlFor="name"> name </label>
                </div>
            </form>
    }

I'm trying to test this with jest using the following: 我正在尝试使用以下方法来对此进行测试:

//A_Form.spec.js
import React
import {A_Form} from './A_Form';

function mock_A_Form() {
    var A_Form = React.createElement(require.requireActual("./A_Form");
    return {
        ... A_Form,
        updateState: jest.fn( () => { return; } )
    }
jest.mock("./A_Form",() => { return mock_A_Form(); } );

describe("A_Form test",function() {
    var ReactTestUtils = require('react-addons-test-utils');
    var a_form = React.createElement(require.requireMock("./A_Form");

    it("checking",function() {
        var n = "name";
        var rendered_a_form = ReactTestUtils.renderIntoDocument(a_form);
        var my_name = ReactTestUtils.findRenderedDOMComponentWithTag(rendered_a_form,"label");
        expect(my_name.textContent).toBe(n);
    });
});

I think I have included all of the relevant code. 我想我已经包含了所有相关代码。 I am stuck with I call renderIntoDocument as it returns "Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.". 我坚持使用renderIntoDocument,因为它返回“不变违规:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:对象。”

As I am rather new to Jest and React any push in reasonable direction would be appreciated. 由于我对Jest和React相当陌生,因此请向合理的方向努力。

Ah ok, I think I see what's going on: 好的,我想我是怎么回事:

var a_form = React.createElement(require.requireMock("./A_Form");

looks to be the line that's throwing the error. 看起来是抛出错误的行。 require.requireMock("./A_Form") produces an object ( React.createElement requires a component or string): require.requireMock("./A_Form")产生一个对象( React.createElement需要一个组件或字符串):

{
    ... A_Form,
    updateState: jest.fn( () => { return; } )
}

If I understand correctly, you want to return a Component, with the updateState method overwritten. 如果我理解正确,则想返回一个Component,而updateState方法被覆盖。 However, what you've ended up doing is producing an object with all the methods and fields of A_Form (if that, I don't think you can unpack an object like that - for me, at least, Babel complains about that unpacking) and an updateState method. 但是,您最终要做的是使用A_Form所有方法和字段生成一个对象(如果那样的话,我认为您无法解包这样的对象-至少对我而言,Babel抱怨这种解包)和一个updateState方法。

Unfortunately, there's not really a good, easy way to just do method overrides in React - take a look at this post , I think it'll point you in the right direction. 不幸的是,在React中并没有真正好的简单方法来覆盖方法-看一下这篇文章 ,我认为它将为您指明正确的方向。 At a high level, instead of overriding, you'll just be supplying methods to be overriden to the component when you create them. 在较高的层次上,您不会提供替代方法,而只是提供了创建方法时要覆盖组件的方法。

There are also libraries for "true" OO in React... although it seems kind of excessive. 在React中也有一些用于“真正的” OO的

You can try something like 您可以尝试类似

return React.createComponent({ ...A_Form, updateState: () => {}); 

not sure if it'll work, though. 不确定是否可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM