Advertisement
Guest User

Untitled

a guest
May 4th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. /*global describe, it, beforeEach, inject, expect*/
  2. (function () {
  3. 'use strict';
  4.  
  5. describe('Todo Controller', function () {
  6. var ctrl, scope;
  7. var todoList;
  8. var todoStorage = {
  9. storage: {},
  10. get: function () {
  11. return this.storage;
  12. },
  13. put: function (value) {
  14. this.storage = value;
  15. }
  16. };
  17.  
  18. // Load the module containing the app, only 'ng' is loaded by default.
  19. beforeEach(module('todomvc'));
  20.  
  21. beforeEach(inject(function ($controller, $rootScope) {
  22. scope = $rootScope.$new();
  23. ctrl = $controller('TodoCtrl', { $scope: scope });
  24. }));
  25.  
  26. it('should not have an edited Todo on start', function () {
  27. expect(scope.editedTodo).toBeNull();
  28. });
  29.  
  30. it('should not have any Todos on start', function () {
  31. expect(scope.todos.length).toBe(0);
  32. });
  33.  
  34. it('should have all Todos completed', function () {
  35. scope.$digest();
  36. expect(scope.allChecked).toBeTruthy();
  37. });
  38.  
  39. describe('the filter', function () {
  40. it('should default to ""', function () {
  41. scope.$emit('$routeChangeSuccess');
  42.  
  43. expect(scope.status).toBe('');
  44. expect(scope.statusFilter).toBeNull();
  45. });
  46.  
  47. describe('being at /active', function () {
  48. it('should filter non-completed', inject(function ($controller) {
  49. ctrl = $controller('TodoCtrl', {
  50. $scope: scope,
  51. $routeParams: {
  52. status: 'active'
  53. }
  54. });
  55.  
  56. scope.$emit('$routeChangeSuccess');
  57. expect(scope.statusFilter.completed).toBeFalsy();
  58. }));
  59. });
  60.  
  61. describe('being at /completed', function () {
  62. it('should filter completed', inject(function ($controller) {
  63. ctrl = $controller('TodoCtrl', {
  64. $scope: scope,
  65. $routeParams: {
  66. status: 'completed'
  67. }
  68. });
  69.  
  70. scope.$emit('$routeChangeSuccess');
  71. expect(scope.statusFilter.completed).toBeTruthy();
  72. }));
  73. });
  74. });
  75.  
  76. describe('having no Todos', function () {
  77. var ctrl;
  78.  
  79. beforeEach(inject(function ($controller) {
  80. todoStorage.storage = [];
  81. ctrl = $controller('TodoCtrl', {
  82. $scope: scope,
  83. todoStorage: todoStorage
  84. });
  85. scope.$digest();
  86. }));
  87.  
  88. it('should not add empty Todos', function () {
  89. scope.newTodo = '';
  90. scope.addTodo();
  91. scope.$digest();
  92. expect(scope.todos.length).toBe(0);
  93. });
  94.  
  95. it('should not add items consisting only of whitespaces', function () {
  96. scope.newTodo = ' ';
  97. scope.addTodo();
  98. scope.$digest();
  99. expect(scope.todos.length).toBe(0);
  100. });
  101.  
  102.  
  103. it('should trim whitespace from new Todos', function () {
  104. scope.newTodo = ' buy some unicorns ';
  105. scope.addTodo();
  106. scope.$digest();
  107. expect(scope.todos.length).toBe(1);
  108. expect(scope.todos[0].title).toBe('buy some unicorns');
  109. });
  110. });
  111.  
  112. describe('having some saved Todos', function () {
  113. var ctrl;
  114.  
  115. beforeEach(inject(function ($controller) {
  116. todoList = [{
  117. 'title': 'Uncompleted Item 0',
  118. 'completed': false
  119. }, {
  120. 'title': 'Uncompleted Item 1',
  121. 'completed': false
  122. }, {
  123. 'title': 'Uncompleted Item 2',
  124. 'completed': false
  125. }, {
  126. 'title': 'Completed Item 0',
  127. 'completed': true
  128. }, {
  129. 'title': 'Completed Item 1',
  130. 'completed': true
  131. }];
  132.  
  133. todoStorage.storage = todoList;
  134. ctrl = $controller('TodoCtrl', {
  135. $scope: scope,
  136. todoStorage: todoStorage
  137. });
  138. scope.$digest();
  139. }));
  140.  
  141. it('should count Todos correctly', function () {
  142. expect(scope.todos.length).toBe(5);
  143. expect(scope.remainingCount).toBe(3);
  144. expect(scope.completedCount).toBe(2);
  145. expect(scope.allChecked).toBeFalsy();
  146. });
  147.  
  148. it('should save Todos to local storage', function () {
  149. expect(todoStorage.storage.length).toBe(5);
  150. });
  151.  
  152. it('should remove Todos w/o title on saving', function () {
  153. var todo = todoList[2];
  154. todo.title = '';
  155.  
  156. scope.doneEditing(todo);
  157. expect(scope.todos.length).toBe(4);
  158. });
  159.  
  160. it('should trim Todos on saving', function () {
  161. var todo = todoList[0];
  162. todo.title = ' buy moar unicorns ';
  163.  
  164. scope.doneEditing(todo);
  165. expect(scope.todos[0].title).toBe('buy moar unicorns');
  166. });
  167.  
  168. it('clearCompletedTodos() should clear completed Todos', function () {
  169. scope.clearCompletedTodos();
  170. expect(scope.todos.length).toBe(3);
  171. });
  172.  
  173. it('markAll() should mark all Todos completed', function () {
  174. scope.markAll();
  175. scope.$digest();
  176. expect(scope.completedCount).toBe(5);
  177. });
  178.  
  179. it('revertTodo() get a Todo to its previous state', function () {
  180. var todo = todoList[0];
  181. scope.editTodo(todo);
  182. todo.title = 'Unicorn sparkly skypuffles.';
  183. scope.revertEditing(todo);
  184. scope.$digest();
  185. expect(scope.todos[0].title).toBe('Uncompleted Item 0');
  186. });
  187. });
  188. });
  189. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement