Skip to content

Commit

Permalink
Add tests for TodoList component
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Goutay committed Feb 27, 2016
1 parent d9a60c9 commit 69707f0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -28,6 +28,7 @@
"css-loader": "^0.23.1",
"jsdom": "^8.0.4",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7",
"react-hot-loader": "^1.3.0",
"style-loader": "^0.13.0",
"webpack": "^1.12.14",
Expand Down
2 changes: 1 addition & 1 deletion src/components/TodoApp.jsx
Expand Up @@ -5,7 +5,7 @@ export default React.createClass({
render: function () {
return <div>
<section className="todoapp">
<TodoList todos={this.props.todos} />
<TodoList todos={this.props.todos} filter={this.props.filter} />
</section>
</div>
}
Expand Down
11 changes: 10 additions & 1 deletion src/components/TodoList.jsx
Expand Up @@ -4,10 +4,19 @@ import TodoItem from './TodoItem';

export default React.createClass({
mixins: [PureRenderMixin],
getItems: function () {
if (this.props.todos) {
return this.props.todos.filter(
(item) => this.props.filter === 'all' ||
item.get('status') === this.props.filter
);
}
return [];
},
render: function () {
return <section className="main">
<ul className="todo-list">
{this.props.todos.map(item =>
{this.getItems().map(item =>
<TodoItem key={item.get('text')}
text={item.get('text')} />
)}
Expand Down
4 changes: 3 additions & 1 deletion src/index.jsx
Expand Up @@ -10,9 +10,11 @@ const todos = List.of(
Map({id: 3, text: 'Immutable', status: 'completed', editing: false})
);

const filter = 'all';

require('../node_modules/todomvc-app-css/index.css');

ReactDOM.render(
<TodoApp todos={todos} />,
<TodoApp todos={todos} filter={filter} />,
document.getElementById('app')
);
61 changes: 61 additions & 0 deletions test/components/TodoList_spec.js
@@ -0,0 +1,61 @@
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import TodoList from '../../src/components/TodoList';
import {expect} from 'chai';
import {List, Map} from 'immutable';

const {renderIntoDocument,
scryRenderedDOMComponentsWithTag} = TestUtils;

describe('TodoList', () => {
it('renders a list with only the active items if the filter is active', () => {
const todos = List.of(
Map({id: 1, text: 'React', status: 'active'}),
Map({id: 2, text: 'Redux', status: 'active'}),
Map({id: 3, text: 'Immutable', status: 'completed'})
);
const filter = 'active';
const component = renderIntoDocument(
<TodoList filter={filter} todos={todos} />
);
const items = scryRenderedDOMComponentsWithTag(component, 'li');

expect(items.length).to.equal(2);
expect(items[0].textContent).to.contain('React');
expect(items[1].textContent).to.contain('Redux');
});

it('renders a list with only completed items if the filter is completed', () => {
const todos = List.of(
Map({id: 1, text: 'React', status: 'active'}),
Map({id: 2, text: 'Redux', status: 'active'}),
Map({id: 3, text: 'Immutable', status: 'completed'})
);
const filter = 'completed';
const component = renderIntoDocument(
<TodoList filter={filter} todos={todos} />
);
const items = scryRenderedDOMComponentsWithTag(component, 'li');

expect(items.length).to.equal(1);
expect(items[0].textContent).to.contain('Immutable');
});

it('renders a list with all the items', () => {
const todos = List.of(
Map({id: 1, text: 'React', status: 'active'}),
Map({id: 2, text: 'Redux', status: 'active'}),
Map({id: 3, text: 'Immutable', status: 'completed'})
);
const filter = 'all';
const component = renderIntoDocument(
<TodoList filter={filter} todos={todos} />
);
const items = scryRenderedDOMComponentsWithTag(component, 'li');

expect(items.length).to.equal(3);
expect(items[0].textContent).to.contain('React');
expect(items[1].textContent).to.contain('Redux');
expect(items[2].textContent).to.contain('Immutable');
});
});

0 comments on commit 69707f0

Please sign in to comment.