Created
November 5, 2015 19:32
-
-
Save anttti/100717dfaebcc5e46c69 to your computer and use it in GitHub Desktop.
Testing React 0.14 components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import ReactTestUtils from "react-addons-test-utils"; | |
const { renderIntoDocument, scryRenderedDOMComponentsWithTag } = ReactTestUtils; | |
import { expect } from "chai"; | |
const Test = React.createClass({ | |
render() { | |
return ( | |
<div> | |
<span>Foo</span> | |
<span>Bar</span> | |
</div> | |
); | |
} | |
}); | |
const Test2 = () => { | |
return ( | |
<div> | |
<span>Foo</span> | |
<span>Bar</span> | |
</div> | |
); | |
}; | |
class Test3 extends React.Component { | |
render() { | |
return ( | |
<div> | |
<span>Foo</span> | |
<span>Bar</span> | |
</div> | |
); | |
} | |
} | |
describe("Test", () => { | |
it("should test createClass() components correctly", () => { | |
const component = renderIntoDocument( | |
<Test /> | |
); | |
const spans = scryRenderedDOMComponentsWithTag(component, "span"); | |
expect(spans.length).to.equal(2); | |
expect(spans[0].textContent).to.equal("Foo"); | |
expect(spans[1].textContent).to.equal("Bar"); | |
}); | |
it("should test pure components correctly", () => { | |
const component = renderIntoDocument( | |
<Test2 /> | |
); | |
const spans = scryRenderedDOMComponentsWithTag(component, "span"); | |
expect(spans.length).to.equal(2); | |
expect(spans[0].textContent).to.equal("Foo"); | |
expect(spans[1].textContent).to.equal("Bar"); | |
}); | |
it("should test es6 class-based components correctly", () => { | |
const component = renderIntoDocument( | |
<Test3 /> | |
); | |
const spans = scryRenderedDOMComponentsWithTag(component, "span"); | |
expect(spans.length).to.equal(2); | |
expect(spans[0].textContent).to.equal("Foo"); | |
expect(spans[1].textContent).to.equal("Bar"); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Components Test and Test3 pass their respective tests, but component Test2 fails:
(failing the spans.length test and stopping test execution there)