CascadiaJS
CascadiaJS

Reputation: 2495

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

I have just started coding with React and I've made my first page, now I want to add a test for it. I'm using Jasmine and karma for my test. When I try and run the test, I get the error:

TypeError: this.props.data is undefined

I've searched the web and I can't find anything, so any help would be really appreciated. You can see my full project on my github repo github.com/mrbgit/short-stories/tree/react-tests Thanks!

My test file looks like this:

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')
    });
});

Upvotes: 1

Views: 639

Answers (2)

danillouz
danillouz

Reputation: 6371

You need to pass a data Object into your component when rendering it into the DOM using react test utils. This is how I usually test my components:

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);
  });
});

In my example I'm using chaijs for assertions.

Upvotes: 0

Kirill Slatin
Kirill Slatin

Reputation: 6143

Try this

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);
});

Upvotes: 2

Related Questions