|
| 1 | +import 'package:mockito/annotations.dart'; |
| 2 | +import 'package:mockito/mockito.dart'; |
| 3 | +import 'package:signals_sample/todo.dart'; |
| 4 | +import 'package:signals_sample/todo_list_controller.dart'; |
| 5 | +import 'package:test/test.dart'; |
| 6 | +import 'package:todos_repository_core/todos_repository_core.dart'; |
| 7 | + |
| 8 | +import 'todo_list_controller_test.mocks.dart'; |
| 9 | + |
| 10 | +@GenerateNiceMocks([MockSpec<TodosRepository>()]) |
| 11 | +void main() { |
| 12 | + group('$TodoListController', () { |
| 13 | + test('should compute the number of completed todos', () async { |
| 14 | + final repository = MockTodosRepository(); |
| 15 | + final controller = TodoListController(repository: repository); |
| 16 | + |
| 17 | + when(repository.loadTodos()).thenAnswer( |
| 18 | + (_) async => [ |
| 19 | + TodoEntity('a', '1', '', false), |
| 20 | + TodoEntity('b', '2', '', false), |
| 21 | + TodoEntity('c', '3', '', true), |
| 22 | + ], |
| 23 | + ); |
| 24 | + await controller.init(); |
| 25 | + |
| 26 | + expect(controller.numCompleted.value, 1); |
| 27 | + }); |
| 28 | + |
| 29 | + test('should calculate the number of active todos', () async { |
| 30 | + final repository = MockTodosRepository(); |
| 31 | + final controller = TodoListController(repository: repository); |
| 32 | + |
| 33 | + when(repository.loadTodos()).thenAnswer( |
| 34 | + (_) async => [ |
| 35 | + TodoEntity('a', '1', '', false), |
| 36 | + TodoEntity('b', '2', '', false), |
| 37 | + TodoEntity('c', '3', '', true), |
| 38 | + ], |
| 39 | + ); |
| 40 | + await controller.init(); |
| 41 | + |
| 42 | + expect(controller.hasActiveTodos.value, isTrue); |
| 43 | + expect(controller.numActive.value, 2); |
| 44 | + }); |
| 45 | + |
| 46 | + test('should return all todos if the VisibilityFilter is all', () async { |
| 47 | + final repository = MockTodosRepository(); |
| 48 | + final controller = TodoListController( |
| 49 | + filter: VisibilityFilter.all, |
| 50 | + repository: repository, |
| 51 | + ); |
| 52 | + |
| 53 | + when(repository.loadTodos()).thenAnswer( |
| 54 | + (_) async => [ |
| 55 | + TodoEntity('a', '1', '', false), |
| 56 | + TodoEntity('b', '2', '', false), |
| 57 | + TodoEntity('c', '3', '', true), |
| 58 | + ], |
| 59 | + ); |
| 60 | + |
| 61 | + await controller.init(); |
| 62 | + |
| 63 | + expect(controller.visibleTodos.value, [ |
| 64 | + Todo('a', id: '1'), |
| 65 | + Todo('b', id: '2'), |
| 66 | + Todo('c', id: '3', complete: true), |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | + test( |
| 71 | + 'should return active todos if the VisibilityFilter is active', |
| 72 | + () async { |
| 73 | + final repository = MockTodosRepository(); |
| 74 | + final controller = TodoListController( |
| 75 | + filter: VisibilityFilter.active, |
| 76 | + repository: repository, |
| 77 | + ); |
| 78 | + |
| 79 | + when(repository.loadTodos()).thenAnswer( |
| 80 | + (_) async => [ |
| 81 | + TodoEntity('a', '1', '', false), |
| 82 | + TodoEntity('b', '2', '', false), |
| 83 | + TodoEntity('c', '3', '', true), |
| 84 | + ], |
| 85 | + ); |
| 86 | + await controller.init(); |
| 87 | + |
| 88 | + expect(controller.visibleTodos.value, [ |
| 89 | + Todo('a', id: '1'), |
| 90 | + Todo('b', id: '2'), |
| 91 | + ]); |
| 92 | + }, |
| 93 | + ); |
| 94 | + |
| 95 | + test( |
| 96 | + 'should return completed todos if the VisibilityFilter is completed', |
| 97 | + () async { |
| 98 | + final repository = MockTodosRepository(); |
| 99 | + final controller = TodoListController( |
| 100 | + filter: VisibilityFilter.completed, |
| 101 | + repository: repository, |
| 102 | + ); |
| 103 | + |
| 104 | + when(repository.loadTodos()).thenAnswer( |
| 105 | + (_) async => [ |
| 106 | + TodoEntity('a', '1', '', false), |
| 107 | + TodoEntity('b', '2', '', false), |
| 108 | + TodoEntity('c', '3', '', true), |
| 109 | + ], |
| 110 | + ); |
| 111 | + await controller.init(); |
| 112 | + |
| 113 | + expect(controller.visibleTodos.value, [ |
| 114 | + Todo('c', id: '3', complete: true), |
| 115 | + ]); |
| 116 | + }, |
| 117 | + ); |
| 118 | + |
| 119 | + test('should clear the completed todos', () async { |
| 120 | + final repository = MockTodosRepository(); |
| 121 | + final controller = TodoListController(repository: repository); |
| 122 | + |
| 123 | + when(repository.loadTodos()).thenAnswer( |
| 124 | + (_) async => [ |
| 125 | + TodoEntity('a', '1', '', false), |
| 126 | + TodoEntity('b', '2', '', false), |
| 127 | + TodoEntity('c', '3', '', true), |
| 128 | + ], |
| 129 | + ); |
| 130 | + |
| 131 | + await controller.init(); |
| 132 | + controller.clearCompleted(); |
| 133 | + |
| 134 | + expect(controller.todos.value, [Todo('a', id: '1'), Todo('b', id: '2')]); |
| 135 | + verify( |
| 136 | + repository.saveTodos([ |
| 137 | + TodoEntity('a', '1', '', false), |
| 138 | + TodoEntity('b', '2', '', false), |
| 139 | + ]), |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + test('toggle all as complete or incomplete', () async { |
| 144 | + final repository = MockTodosRepository(); |
| 145 | + final controller = TodoListController(repository: repository); |
| 146 | + |
| 147 | + when(repository.loadTodos()).thenAnswer( |
| 148 | + (_) async => [ |
| 149 | + TodoEntity('a', '1', '', false), |
| 150 | + TodoEntity('b', '2', '', false), |
| 151 | + TodoEntity('c', '3', '', true), |
| 152 | + ], |
| 153 | + ); |
| 154 | + |
| 155 | + await controller.init(); |
| 156 | + |
| 157 | + // Toggle all complete |
| 158 | + controller.toggleAll(); |
| 159 | + expect(controller.todos.every((t) => t.complete.value), isTrue); |
| 160 | + verify( |
| 161 | + repository.saveTodos([ |
| 162 | + TodoEntity('a', '1', '', true), |
| 163 | + TodoEntity('b', '2', '', true), |
| 164 | + TodoEntity('c', '3', '', true), |
| 165 | + ]), |
| 166 | + ); |
| 167 | + |
| 168 | + // Toggle all incomplete |
| 169 | + controller.toggleAll(); |
| 170 | + expect(controller.todos.every((t) => !t.complete.value), isTrue); |
| 171 | + verify( |
| 172 | + repository.saveTodos([ |
| 173 | + TodoEntity('a', '1', '', false), |
| 174 | + TodoEntity('b', '2', '', false), |
| 175 | + TodoEntity('c', '3', '', false), |
| 176 | + ]), |
| 177 | + ); |
| 178 | + }); |
| 179 | + |
| 180 | + test('should add a todo', () async { |
| 181 | + final repository = MockTodosRepository(); |
| 182 | + final controller = TodoListController(repository: repository); |
| 183 | + |
| 184 | + when( |
| 185 | + repository.loadTodos(), |
| 186 | + ).thenAnswer((_) async => [TodoEntity('a', '1', '', false)]); |
| 187 | + |
| 188 | + await controller.init(); |
| 189 | + controller.todos.add(Todo('b', id: '2')); |
| 190 | + |
| 191 | + expect(controller.todos, [Todo('a', id: '1'), Todo('b', id: '2')]); |
| 192 | + verify( |
| 193 | + repository.saveTodos([ |
| 194 | + TodoEntity('a', '1', '', false), |
| 195 | + TodoEntity('b', '2', '', false), |
| 196 | + ]), |
| 197 | + ); |
| 198 | + }); |
| 199 | + |
| 200 | + test('should remove a todo', () async { |
| 201 | + final repository = MockTodosRepository(); |
| 202 | + final controller = TodoListController(repository: repository); |
| 203 | + |
| 204 | + when( |
| 205 | + repository.loadTodos(), |
| 206 | + ).thenAnswer((_) async => [TodoEntity('a', '1', '', false)]); |
| 207 | + |
| 208 | + await controller.init(); |
| 209 | + |
| 210 | + controller.todos.remove(Todo('a', id: '1')); |
| 211 | + |
| 212 | + expect(controller.todos.value, <Todo>[]); |
| 213 | + verify(repository.saveTodos([])); |
| 214 | + }); |
| 215 | + |
| 216 | + test('should update a todo', () async { |
| 217 | + final repository = MockTodosRepository(); |
| 218 | + final controller = TodoListController(repository: repository); |
| 219 | + |
| 220 | + when(repository.loadTodos()).thenAnswer( |
| 221 | + (_) async => [ |
| 222 | + TodoEntity('a', '1', '', false), |
| 223 | + TodoEntity('b', '2', '', false), |
| 224 | + TodoEntity('c', '3', '', true), |
| 225 | + ], |
| 226 | + ); |
| 227 | + await controller.init(); |
| 228 | + |
| 229 | + controller.todos[1].complete.value = true; |
| 230 | + |
| 231 | + expect(controller.todos.value, [ |
| 232 | + Todo('a', id: '1'), |
| 233 | + Todo('b', id: '2', complete: true), |
| 234 | + Todo('c', id: '3', complete: true), |
| 235 | + ]); |
| 236 | + verify( |
| 237 | + repository.saveTodos([ |
| 238 | + TodoEntity('a', '1', '', false), |
| 239 | + TodoEntity('b', '2', '', true), |
| 240 | + TodoEntity('c', '3', '', true), |
| 241 | + ]), |
| 242 | + ); |
| 243 | + }); |
| 244 | + }); |
| 245 | +} |
0 commit comments