Nordnet Academy
Given properties and state what would be the structure of a rendered tree?
Given an output of render(), is it possible to transition from state A to state B?
import React from 'react/addons';
// import React from 'react'; // only React 0.14.x
// import ReactDOM from 'react-dom';
// import TestUtils from 'react-addons-test-utils';
import { expect } from 'chai';
import Component from 'component';
const TestUtils = React.addons.TestUtils; // only React 0.13.x
describe('<Component />', () => {
let node;
beforeEach(() => {
const component = TestUtils.renderIntoDocument(<Component />);
// node = ReactDOM.findDOMNode(component); // only React 0.14.x
node = React.findDOMNode(component);
});
it('should render div', () => expect(node.tagName).to.equal('DIV'));
});
Pros
Cons
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import { expect } from 'chai';
import Component from 'component';
import ComponentList from 'component-list';
describe('<ComponentList />', () => {
let component;
const values = { 0: 'value_0', 1: 'value_1'};
beforeEach(() => component = TestUtils.renderIntoDocument(<ComponentList />));
it('shows a list of components', () => {
const list = TestUtils.findRenderedDOMComponentWithTag(component, 'ul');
expect(list.props.children.length).toEqual(2);
list.props.children.forEach((item, index) => {
expect(item.type).toEqual(Component);
expect(item.key).toEqual(index);
expect(item.ref).toEqual(`component_${index}`);
expect(item.props.value).toEqual(values[index]);
});
});
});
Pros
Cons
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import { expect } from 'chai';
import Component from 'component';
import ComponentList from 'component-list';
describe('<ComponentList />', () => {
let output;
beforeEach(() => {
const renderer = TestUtils.createRenderer();
renderer.render(<ComponentList />);
output = renderer.getRenderOutput();
});
it('shows a list of components', () => {
expect(output.props.children).toEqual([
<li><Component key="0" refs="component_0"/></li>,
<li><Component key="1" refs="component_1"/></li>
]);
});
});
Pros
Cons
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import { expect } from 'chai';
import ComponentList from 'component-list';
describe('<Component />', () => {
let component;
beforeEach(() => component = TestUtils.renderIntoDocument(<ComponentList />));
it('should render div', () => {
const listDOM = ReactDOM.findDOMNode(component.refs.list);
expect(listDOM.tagName).to.equal('UL')
});
});
// <input ref="input" />
var node = this.refs.input;
node.value = 'giraffe'
TestUtils.Simulate.change(node);
TestUtils.Simulate.keyDown(node, {key: "Enter", keyCode: 13, which: 13});
Pros
Cons
import React from 'react';
import { shallow } from 'enzyme';
import { expect } from 'chai';
import Component from 'component';
import ComponentList from 'component-list';
describe('<ComponentList />', () => {
let component;
beforeEach(() => component = shallow(<ComponentList />));
it('shows a list of components',
() => expect(component.find(Component)).to.have.length(2));
});
import React from 'react';
import { expect } from 'chai';
import Component from 'component';
import {
describeWithDOM,
mount,
spyLifecycle,
} from 'enzyme';
describeWithDOM('<Foo />', () => {
let component;
beforeEach(() => {
spyLifecycle(Foo);
component = mount(<Component />);
});
it('calls componentDidMount',
() => expect(Component.prototype.componentDidMount.calledOnce).to.equal(true));
});