React 的 Jasmine 测试给出了错误并且没有 运行 测试

Jasmine test for React gives error and doesn't run tests

我刚开始使用 React 编码,并且制作了第一页,现在我想为其添加一个测试。我正在使用 Jasmine 和 karma 进行测试。当我尝试 运行 测试时,出现错误:

TypeError: this.props.data is undefined

我在网上搜索过,但找不到任何内容,因此非常感谢您的帮助。你可以在我的 github repo github.com/mrbgit/short-stories/tree/react-tests 上看到我的完整项目,谢谢!

我的测试文件如下所示:

var React = require('react/addons');
var Story = require('../../app/js/components/story.jsx');
var TestUtils = React.addons.TestUtils;
var testUtilsAdditions = require('react-testutils-additions');
describe('Story component', function () {
    var component;
    afterEach(function () {
        // if (component && testUtilsAdditions.isCompositeComponent(component) && component.isMounted()) {
        //   React.unmountComponentAtNode(component.getDOMNode().parent);
        // }
    });
    beforeEach(function () {
        component = TestUtils.renderIntoDocument(React.createElement(Story));
        component.props.data.storyTitle = 'front end test title';
        component.props.data.author = 'front end author';
        component.props.data.storyText = 'front end story text';
        console.log('LLLLLLLLL', component);
    });
    it('should display a story', function () {
        expect(component.props.data).toBeDefined();
        expect(component.props.data.storyTitle).toBeDefined();
        expect(component.props.data.storyTitle).toBe('front end test title');
        expect(component.props.data.author).toBe('front end author');
        expect(component.props.data.storyText).toBe('front end story text')
    });
});

试试这个

beforeEach(function () {
    var element = React.createElement(Story);
    element.props.data = {
        storyTitle: 'front end test title',
        author : 'front end author',
        storyText : 'front end story text'
    };
    component = TestUtils.renderIntoDocument(element);
    console.log('LLLLLLLLL', component);
});

在使用 React 测试实用程序将 data 对象渲染到 DOM 中时,您需要将其传递到组件中。这就是我通常测试组件的方式:

var React = require('react');
var ReactAddons = require('react/addons').addons;
var TestUtils = ReactAddons.TestUtils;
var Story = require('../../app/js/components/story');
var expect = require('chai').expect;

describe('<Story />', function () {
  before(function () {

    // Mock data.
    this.storyData = {
      author: 'Test Author',
      storyTitle: 'Test title.',
      storyText: 'This is a test story.'
    };

    // Render component with mock data.
    this.component = TestUtils.renderIntoDocument(
      <Story data={ this.storyData } />
    );
  });

  it('receives story data', function () {
    var componentData = this.component.props.data;

    expect(componentData).to.exist;

    expect(componentData.storyTitle).exist;
    expect(componentData.storyTitle).to.equal(this.storyData.storyTitle);

    expect(componentData.author).to.exist;
    expect(componentData.author).to.equal(this.storyData.author);

    expect(componentData.storyText).to.exist;
    expect(componentData.storyText)to.equal(this.storyData.storyText);
  });
});

在我的示例中,我使用 chaijs 进行断言。