Я только что начал кодирование с помощью React, и я сделал свою первую страницу, теперь хочу добавить тест для нее. Я использую Жасмин и карму для моего теста. Когда я пытаюсь запустить тест, я получаю ошибку:Jasmine test for React дает ошибку и не запускает тесты
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')
});
});
Спасибо, что вернулись ко мне так быстро. Я попробовал ваше предложение, но теперь я получаю сообщение об ошибке: '' 'TypeError: element.props.data is undefined''' – CascadiaJS
@CascadiaJS возможно' data' должно быть определено явно, как в обновленном ответе –
Спасибо Кирилл Слатин, который его исправил! – CascadiaJS