【问题标题】:reactJs test mocked component input and reponse, where to start?reactJs jest mocked 组件输入和响应,从哪里开始?
【发布时间】:2015-07-09 11:22:49
【问题描述】:

我正在尝试在我的 react 应用程序中测试邮政编码搜索组件。下面是我的组件和单击处理程序,它使用超级代理从 api 获取数据。我有 karma、jasmine 和 webpack 设置并运行基本组件测试,但我如何模拟数据和用户输入?我有一个数据文件中的所有 json。我可以使用 stubbing 和 mocking 获得一个简单的示例设置吗?

render: function () {
    return (
        <div className="mainContent add-school-page">
              <Loader loaded={this.state.loaded}>
            <div className="content has-header">
                <div className="list">
                    <label className="item item-input item-stacked-label">
                        <span className="input-label">Postcode</span>
                        <input ref="postcode" type="text" placeholder="A12 3BC"/>
                    </label>
                </div>
                <div className="padding">
                    <button className="button button-block button-positive clickable"
                        onClick={this.searchByPostcode}>
                        Find School
                    </button>
                </div>
                <SearchResults results={this.state.results} />
                <br />
                <br />
            </div>
            </Loader>
        </div>
    );
},

searchByPostcode: function() {
    var postcode = React.findDOMNode(this.refs.postcode).value;
    var url = OsaApiService.buildRequestUrl('find_schools_postcode', [postcode]);
    fetch(url)
        .then(function (response) {
            return response.json();
        }).then(function (json) {
            this.setState({
                results: json.data,
            });
        }.bind(this)).catch(function (ex) {
            console.log(ex);
        });
}

谁能告诉我如何开始?

我尝试过 Jest,但测试需要很长时间才能完成,而作为监视任务需要很长时间。

【问题讨论】:

    标签: javascript unit-testing mocking reactjs


    【解决方案1】:

    这是一个示例测试,它使用Sinon.js 创建一个假服务器来响应您的请求。它还可以用于存根函数,本测试通过在被测组件上对setState 的调用存根来证明这一点(存根setSate 可能很愚蠢,但我只是想提供一个示例)

    var React = require('react/addons'),
        TestUtils = React.addons.TestUtils,
        ComponentUnderTest = require('./component.js'); //whatever component you are testing
    
    describe('ComponentUnderTest', function () {
    
        it('retrieves data and sets state when button is pressed', function () {
            //setups
            var server = sinon.fakeServer.create();
            server.respondWith('GET', 'find_schools_postcode', 'json_string');
            var component = TestUtils.renderIntoDocument(<ComponentUnderTest />);
            var button = React.findDOMNode(component.refs.button); //you will need to add this ref
            var setStateStub = sinon.stub(component, 'setState'); //you don't have to stub this, you could just check that state was set afterwards
    
            TestUtils.Simulate.click(button);
            server.respond(); //tells the server to respond
    
            //expectations
            expect(setStateStub.calledOnce).toBe(true);
            expect(setStateStub.calledWith({results : 'json_string'})).toBe(true);
            //expect(component.state.results).toEqual('json_string'); //if you did not stub setState
        });
    
    });
    

    如果您想在您的测试套件中使用 Sinon.js,我建议您将其包含在您的 Karma 配置文件中:

    frameworks: ['jasmine', 'sinon'],
    

    希望这会有所帮助!

    【讨论】:

    • 非常感谢。我将如何测试子组件搜索结果?哪个显示结果状态?或者任何其他引用不同组件的页面?
    • 我可以在上面的代码中模拟加载器组件吗?当我收到错误时,ReferenceError: Loader is not defined
    • 是的。看看这个:github.com/tommyh/jasmine-react - 特别是“用测试替身替换组件的子组件”部分。我在我的项目中也做了很多。
    猜你喜欢
    • 2010-12-18
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多