Skip to content

Instantly share code, notes, and snippets.

@anttti
Created November 5, 2015 19:32
Show Gist options
  • Save anttti/100717dfaebcc5e46c69 to your computer and use it in GitHub Desktop.
Save anttti/100717dfaebcc5e46c69 to your computer and use it in GitHub Desktop.
Testing React 0.14 components
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");
});
})
@anttti
Copy link
Author

anttti commented Nov 5, 2015

Components Test and Test3 pass their respective tests, but component Test2 fails:

  1) Test should test pure components correctly:

      AssertionError: expected 0 to equal 2
      + expected - actual

      -0
      +2

      at Context.<anonymous> (test/Entry.test.jsx:55:33)

(failing the spans.length test and stopping test execution there)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment