「もっと読む」/「もっと読む」機能でマークアップの長いブロックを装飾する非常に単純な React.js コンポーネントがあります。
Jest が動作するいくつかのテストがありますが、DOM 要素の高さが元のコンテンツのサイズまで増加していると断言できません。
Jest テスト環境では、getDOMNode().scrollHeight を呼び出しても何も返されないようです。
コードと失敗したテストを含むリポジトリへのリンクは次のとおりです: https://github.com/mguterl/react-jest-dom-question
以下は、同じ問題を示すコードとテストの簡略版です。
簡素化されたコード
var ReadMore = React.createClass({
getInitialState: function() {
return {
style: {
height: '0px'
}
}
},
render: function() {
return (
<div>
<div ref='content' className='read-more__content' style={this.state.style} dangerouslySetInnerHTML={{__html: this.props.content}} />
<a onClick={this.expand} href='#'>Expand</a>
</div>
);
},
expand: function() {
// This call to scrollHeight doesn't return anything when testing.
var height = this.refs.content.getDOMNode().scrollHeight;
this.setState({
style: {
height: height
}
});
}
});
テスト
jest.dontMock('../ReadMore');
global.React = require('react/addons');
var TestUtils = React.addons.TestUtils;
var ReadMore = require('../ReadMore');
describe('ReadMore', function() {
var readMore;
var content;
var link;
beforeEach(function() {
readMore = TestUtils.renderIntoDocument(
<ReadMore collapsedHeight='0px' content='<p>Hello World</p>' />
);
content = TestUtils.findRenderedDOMComponentWithClass(
readMore, 'read-more__content');
link = TestUtils.findRenderedDOMComponentWithTag(
readMore, 'a');
});
it('starts off collapsed', function() {
expect(content.getDOMNode().getAttribute('style')).toEqual('height:0px;');
});
it('expands the height to fit the content', function() {
TestUtils.Simulate.click(link);
expect(content.getDOMNode().getAttribute('style')).toEqual('height:100px;');
});
});