Skip to content

Commit

Permalink
Merge pull request tastejs#571 from passy/angular-trim
Browse files Browse the repository at this point in the history
AngularJS: Trim todos on save
  • Loading branch information
sindresorhus committed May 23, 2013
2 parents ae73f1c + c462121 commit a9abf95
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
9 changes: 6 additions & 3 deletions architecture-examples/angularjs/js/controllers/todoCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage,
$scope.editedTodo = null;

$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.allChecked = !$scope.remainingCount;
todoStorage.put(todos);
Expand All @@ -32,12 +32,13 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage,
});

$scope.addTodo = function () {
if (!$scope.newTodo.length) {
var newTodo = $scope.newTodo.trim();
if (!newTodo.length) {
return;
}

todos.push({
title: $scope.newTodo,
title: newTodo,
completed: false
});

Expand All @@ -50,6 +51,8 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage,

$scope.doneEditing = function (todo) {
$scope.editedTodo = null;
todo.title = todo.title.trim();

if (!todo.title) {
$scope.removeTodo(todo);
}
Expand Down
67 changes: 56 additions & 11 deletions architecture-examples/angularjs/test/unit/todoCtrlSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@

describe('Todo Controller', function () {
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.
beforeEach(module('todomvc'));

beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
ctrl = $controller('TodoCtrl', {$scope: scope});
ctrl = $controller('TodoCtrl', { $scope: scope });
}));

it('should not have an edited Todo on start', function () {
Expand Down Expand Up @@ -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 () {
var todoList,
todoStorage = {
storage: {},
get: function () {
return this.storage;
},
put: function (value) {
this.storage = value;
}
};
var ctrl;

beforeEach(inject(function ($controller) {
todoList = [{
Expand Down Expand Up @@ -117,6 +154,14 @@
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 () {
scope.clearCompletedTodos();
expect(scope.todos.length).toBe(3);
Expand Down

0 comments on commit a9abf95

Please sign in to comment.