我試圖用jest-cli來測試一個反應組件是否包含其輸出中的另一個組件。我無法弄清楚如何做到這一點。如何測試一個React組件是否包含另一個帶有笑話的組件?
這裏是我的部件:
DesignerPage組件
[...]
var TopBar = require('../components/layout/TopBar.js');
var DesignerPage = React.createClass({
getInitialState: function() {
var state = {
};
return state;
},
render: function() {
return (
<div>
<TopBar />
</div>
)
}
});
module.exports = DesignerPage;
頂欄組件
/** @jsx React.DOM */
var React = require("react");
var TopBar = React.createClass({
render: function() {
return (
<nav className="top-bar">
</nav>
);
}
});
module.exports = TopBar;
現在,我想測試DesignerPage組件是否包含頂欄組件。以下是我認爲應該工作:
/** @jsx React.DOM */
jest.dontMock('../../src/js/pages/DesignerPage.js');
describe('DesignerPage', function() {
it('should contain a TopBar', function() {
var React = require('react/addons');
var DesignerPage = require('../../src/js/pages/DesignerPage.js');
var TestUtils = React.addons.TestUtils;
// Render a DesignerPage into the document
var page = TestUtils.renderIntoDocument(
<DesignerPage />
);
// Verify that a TopBar is included
var topbar = TestUtils.scryRenderedComponentsWithType(page, 'TopBar');
expect(topbar.length).toBe(1);
});
});
但它不通過... :(
$ ./node_modules/jest-cli/bin/jest.js DesignerPage
Found 1 matching test...
FAIL __tests__/pages/DesignerPage-test.js (4.175s)
● DesignerPage › it should contain a TopBar
- Expected: 0 toBe: 1
at Spec.<anonymous> (__tests__/pages/DesignerPage-test.js:16:27)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
1 test failed, 0 test passed (1 total)
Run time: 6.462s
我還沒有使用JEST,但我在這裏沒有看到任何明顯的問題。從閱讀文檔看來,你正在做的事情是正確的。只是以爲我會提到,因爲你沒有在這裏獲得很多的流量。 – 2014-10-12 18:15:13