【问题标题】:React.js test fails on finding componentsReact.js 测试在查找组件时失败
【发布时间】:2016-02-11 13:52:01
【问题描述】:

我有一个基于 React、Redux 和 React-router 构建的应用程序。我正在使用 React TestUtils 编写测试,我发现从测试中您可以在下面看到。 第一个期望有效:expect(nav).to.have.length(1); 但是第二个expect(modal).to.have.length(1);

失败:

AssertionError: 预期 [] 的长度为 1 但得到了 0

App.js:

import React, { Component, cloneElement, PropTypes } from 'react';
import ContactsList from './contactsList';
import Nav from './nav';
import Modal from './modal';
import Header from './header';
import HomeIndex from './homeIndex';
import ErrorBox from './errorBox';
import ImmutablePropTypes from 'react-immutable-proptypes';

export default class App extends Component {
	render = () => {
		const { actions, contacts, router } = this.props;
		return (
			<div>
				<Nav />
				<div className="container">
					<ErrorBox error={contacts.getIn(['error', 'errorMessage'])} show={contacts.getIn(['error', 'showError'])} />
					<Header />
					<div className="contacts-list-container">
						<ContactsList contacts={contacts} />
						<Modal open={contacts.get('showSpinner')} />
						{ cloneElement(this.props.children || <HomeIndex/>, { contacts: contacts ,
								addContact: actions.addContactReq, 
								getContact: actions.getContact, 
								contact: contacts.get('contact'), 
								router: router,
								deleteContact: actions.deleteContact,
								editContact: actions.editContact }) }
					</div>
				</div>				
			</div>
		);
	}
}

App.propTypes = {
	actions: PropTypes.object.isRequired,
	contacts: ImmutablePropTypes.map.isRequired,
	router: PropTypes.object.isRequired
};

应用规范.js:

import React from 'react';
import { renderIntoDocument, scryRenderedDOMComponentsWithTag } from 'react-addons-test-utils';
import { expect } from 'chai';
import App from '../components/app';
import { Map } from 'immutable';

describe('app', () => {

	it('renders properly', () => {
		const component = renderIntoDocument(
			<App  actions={{}} router={{}} contacts={ Map({
				showSpinner: false,
				error: Map({
					showError: false,
					errorMessage: ''
				}),
				contacts: Map(),
				contact: Map()
				}) } />
		);

		const nav = scryRenderedDOMComponentsWithTag(component, 'Nav');
		const modal = scryRenderedDOMComponentsWithTag(component, 'Modal');
		expect(nav).to.have.length(1);
		expect(modal).to.have.length(1);    		
	});
});

【问题讨论】:

    标签: javascript reactjs mocha.js chai


    【解决方案1】:

    scrying 需要一个组件引用 iirc。所以:

    import Modal from './components/modal';
    
    // Then later:
    const modal = scryRenderedComponentsWithType(component, Modal);
    

    这将寻找实际组件的实例。

    【讨论】:

      【解决方案2】:

      scryRenderedDOMComponentsWithTag 在组件 DOM 中查找实际的 HTML 元素。你想使用scryRenderedComponentsWithType 来查找渲染的 React 组件:

      const modal = scryRenderedComponentsWithType(component, Modal);
      

      https://facebook.github.io/react/docs/test-utils.html#scryrenderedcomponentswithtype

      您的Nav 调用可能有效,因为您的Nav React 组件中有一个&lt;nav&gt; HTML 元素。

      【讨论】:

      • 好吧,你是对的。第二个问题是它不适用于 react v0.14 中引入的新无状态组件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      相关资源
      最近更新 更多