Sto provando a usare jest-cli per verificare se un componente reagisce contiene un altro componente nel suo output. Sto avendo problemi a capire come farlo.Come testare se un componente React contiene un altro componente con jest?
Qui sono i miei componenti:
DesignerPage componenti
[...]
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;
topbar Componente
/** @jsx React.DOM */
var React = require("react");
var TopBar = React.createClass({
render: function() {
return (
<nav className="top-bar">
</nav>
);
}
});
module.exports = TopBar;
Ora, voglio verificare se il componente DesignerPage contiene il componente topbar. Ecco quello che penso dovrebbe funzionare:
/** @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);
});
});
Ma non passa ... :(
$ ./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
Non ho usato JEST ma non vedo alcun problema evidente qui. Dalla lettura dei documenti, sembra che tu stia facendo le cose correttamente. Ho pensato di dirlo perché non hai molto traffico qui. –