3

我的应用程序是一个 Fluxible / React 应用程序。

我有以下试图测试 LoginForm 的规范。嵌入式组件已使用重新布线进行存根。我引用了 http://fluxible.io/api/components.html#testing

它(“渲染”)通过的第一个规范。但是,当我尝试按照注释代码中所示进行更多测试时,测试失败。

我无法断言 LoginForm 的状态或使用组件上的 TestUtils 触发模拟事件。有没有办法做到这一点?

import React from 'react/addons';;
import { createMockComponentContext } from 'fluxible/utils';
import createStore from 'fluxible/addons/createStore';

var rewire = require("rewire");
var rewireModule = require("../../helpers/rewire-module");

// stub inner components with LoginForm
// `rewire` instead of `require`
var LoginForm = rewire("../../../src/components/auth/login-form");

// Replace the required module with a stub component.
rewireModule(LoginForm, {
  FormattedMessage: React.createClass({
    render: function() { return <div />; }
  }),
  NavLink: React.createClass({
    render: function() { return <div />; }
  })
});

describe('LoginForm', function() {
  var context;
  var TestUtils;
  var provideContext;
  var connectToStores;
  var MockIntlStore;
  var MockAuthStore;
  var noop = function(){};
  var component;

  beforeEach(function(){

    MockIntlStore = createStore({
      storeName: 'IntlStore',
      getMessage: noop,
      getState: function(){
        return {}
      }
    });

    MockAuthStore = createStore({
      storeName: 'AuthStore'
    });

    context = createMockComponentContext({
      stores: [MockIntlStore, MockAuthStore]
    });

    // React must be required after window is set
    TestUtils = React.addons.TestUtils
    provideContext = require('fluxible/addons/provideContext');
    connectToStores = require('fluxible/addons/connectToStores');

    // Wrap with context provider and store connector
    LoginForm = provideContext(connectToStores(LoginForm, [MockIntlStore, MockAuthStore], function (stores) {
      return {
      };
    }));

    component = TestUtils.renderIntoDocument(
      <LoginForm context={context} />
    );

  });

  it("renders", function() {


    var foundComponent = TestUtils.findRenderedDOMComponentWithClass(
      component, 'login-form');

    expect(foundComponent).toBeDefined();
  });

  // TODO fluxible wraps components so we cant reach the inner component to assert on state and trigger event handlers

  // it("should have an initial state", function() {

  //   let initialState = {
  //     username: '',
  //     pass: ''
  //   }

  //   expect(component.state).toEqual(initialState);
  // });
});
4

2 回答 2

3

当您使用provideContextandconnectToStores时,您的组件将被包装。使用 找到组件是正确的TestUtils. findRenderedDOMComponentWithClass,只需使用 foundComponent 进行测试,这就是正在测试的内容。IE

...
var foundComponent = TestUtils.findRenderedDOMComponentWithClass(
  component, 'login-form');
expect(foundComponent.state).toEqual(initialState);
...
于 2015-06-25T05:30:34.997 回答
0

如果您仍在寻找解决方案:

    var tbg = React.createElement(x, { di: serviceLocator });
    var renderer = React.addons.TestUtils.createRenderer();
    var rtbg = renderer.render(tbg);

那么你的方法就在这里:

    renderer._instance._instance.myMethod

其中 myMethod 是组件的函数成员x

于 2015-12-15T13:23:56.190 回答