酵素を使用してReact/Redux成分を試験する方法を学んでいます。コンポーネントはアプリレベルの状態を小道具とみなします。私は、テストを実行すると、私はエラーを取得:undefined
としてログインし、以下のテストファイル内wrapper
の私にconsole.logと酵素を用いたReact/Redux Testing
Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components).
TypeError: Cannot read property 'contextTypes' of undefined
。
ここに私の設定に何か問題があり、これを理解しようと数時間を費やしていることがわかります。誰も私がインポートしているコンポーネントを使用しようとしている方法で何かを見ることができますか?なぜそれがundefined
であるかわかりません。助けや洞察力に感謝します!変更後に編集
BackendDisplay.js
import React from 'react';
import { connect } from 'react-redux';
import moment from 'moment';
var BackendDisplay = React.createClass({
render() {
const { username, node_version, app_path, timestamp } = this.props.loginState;
const dateTime = moment(timestamp).format('MMMM Do YYYY, h:mm:ss a');
return (
<div>
<h1>Welcome, {username}!</h1>
<p><span className="bold">Node Version:</span> {node_version}</p>
<p><span className="bold">Application Path:</span> {app_path}</p>
<p><span className="bold">Date/Time:</span> {dateTime}</p>
</div>
);
}
});
const mapStateToProps = function(store) {
return store;
}
module.exports = connect(mapStateToProps)(BackendDisplay);
BackendDisplay.test.js
'use strict';
import React from 'react';
import {shallow} from 'enzyme';
import { connect } from 'react-redux';
import { BackendDisplay } from '../components/BackendDisplay';
describe('<BackendDisplay />',() => {
it('Correctly displays username, node_version, app_path, and timestamp',() => {
const wrapper = shallow(<BackendDisplay />);
console.log(wrapper);
});
});
: BackendDisplay.js
import React from 'react';
import { connect } from 'react-redux';
import moment from 'moment';
var BackendDisplay = React.createClass({
render() {
const { username, node_version, app_path, timestamp } = this.props.loginState;
const dateTime = moment(timestamp).format('MMMM Do YYYY, h:mm:ss a');
return (
<div>
<h1>Welcome, {username}!</h1>
<p><span className="bold">Node Version:</span> {node_version}</p>
<p><span className="bold">Application Path:</span> {app_path}</p>
<p><span className="bold">Date/Time:</span> {dateTime}</p>
</div>
);
}
});
const mapStateToProps = function(store) {
return store;
}
// module.exports = connect(mapStateToProps)(BackendDisplay);
export default connect(mapStateToProps)(BackendDisplay);
BackendDisplay.test.js
'use strict';
import React from 'react';
import {shallow} from 'enzyme';
import { connect } from 'react-redux';
import store from '../store';
import { Provider } from 'react-redux';
import ConnectedBackendDisplay, {BackendDisplay} from '../components/BackendDisplay';
describe('<BackendDisplay />',() => {
it('Correctly displays username, node_version, app_path, and timestamp',() => {
const wrapper = shallow(
<Provider store={store}>
<BackendDisplay />
</Provider>
);
console.log(wrapper.find(BackendDisplay));
expect(wrapper.find(BackendDisplay).length).to.equal(1);
});
});
エラーメッセージ:
TypeError: Enzyme::Selector expects a string, object, or Component Constructor
あなたの迅速な応答をありがとう!これは、店舗に接続されているコンポーネントをテストする標準的な方法ですか?あなたは通常、装飾されていないコンポーネント用に別々のファイルを作成しますか?その場合は、このファイルをどこに配置しますか?テスト初心者を助けてくれてありがとう:) – MizzKFizzle
私はこれを私の答えに追加します。言い換えれば、このコンポーネントのテストはすべて同じテストファイル内にありません。 – therewillbecode
Yikes。あらゆる種類の新しいエラー。今朝闘争バス。 – MizzKFizzle