Skip to content

Commit

Permalink
Adding created and completed timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleydb committed Dec 20, 2016
1 parent 1f34c9a commit b833541
Show file tree
Hide file tree
Showing 5 changed files with 702 additions and 14 deletions.
19 changes: 16 additions & 3 deletions app/components/Todo.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
var React = require('react');
var moment = require('moment');

var Todo = React.createClass({
render: function() {
var {id, text, time, complete} = this.props;
var {id, text, createdAt, completedAt, complete} = this.props;

var renderDate = () => {
var message = 'Created: ';
var timestamp = createdAt;
if (complete) {
message = 'Completed: '
timestamp = completedAt;
}
return message + moment.unix(timestamp).format('d-MMM-YYYY @ HH:mm');
}

return (
<div onClick={() => {this.props.onToggle(this.props.id);}}>
<label>
<input type="checkbox" ref="markCompleted" checked={complete}/> {text}
<p><small>Created {time}</small></p>
<input type="checkbox" ref="markCompleted" checked={complete}/>
<p>{text}</p>
<p><small>{renderDate()}</small></p>
</label>
</div>
);
Expand Down
4 changes: 3 additions & 1 deletion app/components/TodoApp.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var React = require('react');
var UUID = require('node-uuid');
var moment = require('moment');

var TodoList = require('TodoList');
var AddTodo = require('AddTodo');
Expand All @@ -23,14 +24,15 @@ var TodoApp = React.createClass({
this.setState({
todos: [
...this.state.todos,
{id: UUID(), text: text, time: 'Thursday 3pm', complete: false}
{id: UUID(), text: text, createdAt: moment().unix(), completedAt: undefined, complete: false}
]
});
},
handleToggle: function(id) {
var updated = this.state.todos.map((todo) => {
if (id === todo.id) {
todo.complete = !todo.complete;
todo.completedAt = todo.complete ? moment().unix() : undefined;
}
return todo;
});
Expand Down
22 changes: 14 additions & 8 deletions app/tests/components/TodoApp.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ describe('TodoApp', () => {
todoApp.handleAddTodo(todoText);

expect(todoApp.state.todos[0].text).toBe(todoText);
expect(todoApp.state.todos[0].createdAt).toBeA('number');
});

it('should toggle completed value when handleToggle called', () => {
var todo = {id: 13, text: 'learn react', time: 'Monday 8am', complete: false};
var todo = {id: 13, text: 'learn react', createdAt: 0, completedAt: undefined, complete: false};
var todoApp = TestUtils.renderIntoDocument(<TodoApp/>);
todoApp.setState({todos: [todo]});

expect(todoApp.state.todos[0].complete).toBe(false);
todoApp.handleToggle(13);
expect(todoApp.state.todos[0].complete).toBe(true);
expect(todoApp.state.todos[0].completedAt).toBeA('number');
});

it('should toggle completed value when handleToggle called on a completed todo', () => {
var todo = {id: 13, text: 'learn react', createdAt: 0, completedAt: 60, complete: true};
var todoApp = TestUtils.renderIntoDocument(<TodoApp/>);
todoApp.setState({todos: [todo]});

expect(todoApp.state.todos[0].complete).toBe(true);
todoApp.handleToggle(13);
expect(todoApp.state.todos[0].complete).toBe(false);
expect(todoApp.state.todos[0].completedAt).toNotExist();
});

// describe('render', () => {
// it('should render TodoApp to output', () => {
// var todoApp = TestUtils.renderIntoDocument(<TodoApp/>);
// expect(todoApp).toBe(1);
// });
// });
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.8.0",
"mocha": "^3.2.0",
"moment": "^2.17.1",
"node-sass": "^3.13.0",
"node-uuid": "^1.4.7",
"react-addons-test-utils": "^15.4.1",
Expand Down
Loading

0 comments on commit b833541

Please sign in to comment.