Commit c462121c authored by Pascal Hartig's avatar Pascal Hartig

AngularJS: Trim todos on save

parent ae73f1c9
...@@ -13,7 +13,7 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage, ...@@ -13,7 +13,7 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage,
$scope.editedTodo = null; $scope.editedTodo = null;
$scope.$watch('todos', function () { $scope.$watch('todos', function () {
$scope.remainingCount = filterFilter(todos, {completed: false}).length; $scope.remainingCount = filterFilter(todos, { completed: false }).length;
$scope.completedCount = todos.length - $scope.remainingCount; $scope.completedCount = todos.length - $scope.remainingCount;
$scope.allChecked = !$scope.remainingCount; $scope.allChecked = !$scope.remainingCount;
todoStorage.put(todos); todoStorage.put(todos);
...@@ -32,12 +32,13 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage, ...@@ -32,12 +32,13 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage,
}); });
$scope.addTodo = function () { $scope.addTodo = function () {
if (!$scope.newTodo.length) { var newTodo = $scope.newTodo.trim();
if (!newTodo.length) {
return; return;
} }
todos.push({ todos.push({
title: $scope.newTodo, title: newTodo,
completed: false completed: false
}); });
...@@ -50,6 +51,8 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage, ...@@ -50,6 +51,8 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage,
$scope.doneEditing = function (todo) { $scope.doneEditing = function (todo) {
$scope.editedTodo = null; $scope.editedTodo = null;
todo.title = todo.title.trim();
if (!todo.title) { if (!todo.title) {
$scope.removeTodo(todo); $scope.removeTodo(todo);
} }
......
...@@ -4,13 +4,23 @@ ...@@ -4,13 +4,23 @@
describe('Todo Controller', function () { describe('Todo Controller', function () {
var ctrl, scope; var ctrl, scope;
var todoList;
var todoStorage = {
storage: {},
get: function () {
return this.storage;
},
put: function (value) {
this.storage = value;
}
};
// Load the module containing the app, only 'ng' is loaded by default. // Load the module containing the app, only 'ng' is loaded by default.
beforeEach(module('todomvc')); beforeEach(module('todomvc'));
beforeEach(inject(function ($controller, $rootScope) { beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new(); scope = $rootScope.$new();
ctrl = $controller('TodoCtrl', {$scope: scope}); ctrl = $controller('TodoCtrl', { $scope: scope });
})); }));
it('should not have an edited Todo on start', function () { it('should not have an edited Todo on start', function () {
...@@ -60,17 +70,44 @@ ...@@ -60,17 +70,44 @@
}); });
}); });
describe('having no Todos', function () {
var ctrl;
beforeEach(inject(function ($controller) {
todoStorage.storage = [];
ctrl = $controller('TodoCtrl', {
$scope: scope,
todoStorage: todoStorage
});
scope.$digest();
}));
it('should not add empty Todos', function () {
scope.newTodo = '';
scope.addTodo();
scope.$digest();
expect(scope.todos.length).toBe(0);
});
it('should not add items consisting only of whitespaces', function () {
scope.newTodo = ' ';
scope.addTodo();
scope.$digest();
expect(scope.todos.length).toBe(0);
});
it('should trim whitespace from new Todos', function () {
scope.newTodo = ' buy some unicorns ';
scope.addTodo();
scope.$digest();
expect(scope.todos.length).toBe(1);
expect(scope.todos[0].title).toBe('buy some unicorns');
});
});
describe('having some saved Todos', function () { describe('having some saved Todos', function () {
var todoList, var ctrl;
todoStorage = {
storage: {},
get: function () {
return this.storage;
},
put: function (value) {
this.storage = value;
}
};
beforeEach(inject(function ($controller) { beforeEach(inject(function ($controller) {
todoList = [{ todoList = [{
...@@ -117,6 +154,14 @@ ...@@ -117,6 +154,14 @@
expect(scope.todos.length).toBe(4); expect(scope.todos.length).toBe(4);
}); });
it('should trim Todos on saving', function () {
var todo = todoList[0];
todo.title = ' buy moar unicorns ';
scope.doneEditing(todo);
expect(scope.todos[0].title).toBe('buy moar unicorns');
});
it('clearCompletedTodos() should clear completed Todos', function () { it('clearCompletedTodos() should clear completed Todos', function () {
scope.clearCompletedTodos(); scope.clearCompletedTodos();
expect(scope.todos.length).toBe(3); expect(scope.todos.length).toBe(3);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment