|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:mockito/annotations.dart'; |
| 4 | +import 'package:mockito/mockito.dart'; |
| 5 | +import 'package:simple_bloc_flutter_sample/anonymous_user_repository.dart'; |
| 6 | +import 'package:simple_bloc_flutter_sample/dependency_injection.dart'; |
| 7 | +import 'package:simple_bloc_flutter_sample/localization.dart'; |
| 8 | +import 'package:simple_bloc_flutter_sample/screens/home_screen.dart'; |
| 9 | +import 'package:simple_bloc_flutter_sample/widgets/todos_bloc_provider.dart'; |
| 10 | +import 'package:simple_blocs/simple_blocs.dart'; |
| 11 | +import 'package:todos_app_core/todos_app_core.dart'; |
| 12 | +import 'package:todos_repository_core/todos_repository_core.dart'; |
| 13 | + |
| 14 | +import 'home_screen_test.mocks.dart'; |
| 15 | + |
| 16 | +@GenerateNiceMocks([MockSpec<TodosInteractor>(), MockSpec<UserRepository>()]) |
| 17 | +void main() { |
| 18 | + group('HomeScreen', () { |
| 19 | + final todoListFinder = find.byKey(ArchSampleKeys.todoList); |
| 20 | + final todoItem1Finder = find.byKey(ArchSampleKeys.todoItem('1')); |
| 21 | + final todoItem2Finder = find.byKey(ArchSampleKeys.todoItem('2')); |
| 22 | + final todoItem3Finder = find.byKey(ArchSampleKeys.todoItem('3')); |
| 23 | + |
| 24 | + testWidgets('should render loading indicator at first', (tester) async { |
| 25 | + await tester.pumpWidget( |
| 26 | + _TestWidget( |
| 27 | + todosInteractor: MockTodosInteractor(), |
| 28 | + userRepository: AnonymousUserRepository(), |
| 29 | + ), |
| 30 | + ); |
| 31 | + await tester.pump(Duration.zero); |
| 32 | + |
| 33 | + expect(find.byKey(ArchSampleKeys.todosLoading), findsOneWidget); |
| 34 | + }); |
| 35 | + |
| 36 | + testWidgets('should display a list after loading todos', (tester) async { |
| 37 | + final handle = tester.ensureSemantics(); |
| 38 | + final interactor = MockTodosInteractor(); |
| 39 | + |
| 40 | + when( |
| 41 | + interactor.todos, |
| 42 | + ).thenAnswer((_) => Stream.fromIterable([_TestWidget._defaultTodos])); |
| 43 | + |
| 44 | + await tester.pumpWidget( |
| 45 | + _TestWidget( |
| 46 | + todosInteractor: interactor, |
| 47 | + userRepository: AnonymousUserRepository(), |
| 48 | + ), |
| 49 | + ); |
| 50 | + await tester.pumpAndSettle(); |
| 51 | + |
| 52 | + final checkbox1 = find.descendant( |
| 53 | + of: find.byKey(ArchSampleKeys.todoItemCheckbox('1')), |
| 54 | + matching: find.byType(Focus), |
| 55 | + ); |
| 56 | + final checkbox2 = find.descendant( |
| 57 | + of: find.byKey(ArchSampleKeys.todoItemCheckbox('2')), |
| 58 | + matching: find.byType(Focus), |
| 59 | + ); |
| 60 | + final checkbox3 = find.descendant( |
| 61 | + of: find.byKey(ArchSampleKeys.todoItemCheckbox('3')), |
| 62 | + matching: find.byType(Focus), |
| 63 | + ); |
| 64 | + |
| 65 | + expect(todoListFinder, findsOneWidget); |
| 66 | + expect(todoItem1Finder, findsOneWidget); |
| 67 | + expect(find.text('T1'), findsOneWidget); |
| 68 | + expect(find.text('N1'), findsOneWidget); |
| 69 | + expect(tester.getSemantics(checkbox1), isChecked(false)); |
| 70 | + expect(todoItem2Finder, findsOneWidget); |
| 71 | + expect(find.text('T2'), findsOneWidget); |
| 72 | + expect(tester.getSemantics(checkbox2), isChecked(false)); |
| 73 | + expect(todoItem3Finder, findsOneWidget); |
| 74 | + expect(find.text('T3'), findsOneWidget); |
| 75 | + expect(tester.getSemantics(checkbox3), isChecked(true)); |
| 76 | + |
| 77 | + handle.dispose(); |
| 78 | + }); |
| 79 | + |
| 80 | + testWidgets('should remove todos using a dismissible', (tester) async { |
| 81 | + final interactor = MockTodosInteractor(); |
| 82 | + |
| 83 | + when( |
| 84 | + interactor.todos, |
| 85 | + ).thenAnswer((_) => Stream.fromIterable([_TestWidget._defaultTodos])); |
| 86 | + |
| 87 | + await tester.pumpWidget( |
| 88 | + _TestWidget( |
| 89 | + todosInteractor: interactor, |
| 90 | + userRepository: AnonymousUserRepository(), |
| 91 | + ), |
| 92 | + ); |
| 93 | + await tester.pumpAndSettle(); |
| 94 | + await tester.drag(todoItem1Finder, Offset(-1000, 0)); |
| 95 | + await tester.pumpAndSettle(Duration(seconds: 5)); |
| 96 | + |
| 97 | + expect(todoItem1Finder, findsNothing); |
| 98 | + expect(todoItem2Finder, findsOneWidget); |
| 99 | + expect(todoItem3Finder, findsOneWidget); |
| 100 | + }); |
| 101 | + |
| 102 | + testWidgets('should display stats when switching tabs', (tester) async { |
| 103 | + final interactor = MockTodosInteractor(); |
| 104 | + |
| 105 | + when( |
| 106 | + interactor.todos, |
| 107 | + ).thenAnswer((_) => Stream.fromIterable([_TestWidget._defaultTodos])); |
| 108 | + |
| 109 | + await tester.pumpWidget( |
| 110 | + _TestWidget( |
| 111 | + todosInteractor: interactor, |
| 112 | + userRepository: AnonymousUserRepository(), |
| 113 | + ), |
| 114 | + ); |
| 115 | + await tester.pumpAndSettle(); |
| 116 | + await tester.tap(find.byKey(ArchSampleKeys.statsTab)); |
| 117 | + await tester.pump(); |
| 118 | + |
| 119 | + expect(find.byKey(ArchSampleKeys.statsNumActive), findsOneWidget); |
| 120 | + expect(find.byKey(ArchSampleKeys.statsNumActive), findsOneWidget); |
| 121 | + }); |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +class _TestWidget extends StatelessWidget { |
| 126 | + const _TestWidget({ |
| 127 | + required this.todosInteractor, |
| 128 | + required this.userRepository, |
| 129 | + }); |
| 130 | + |
| 131 | + final TodosInteractor todosInteractor; |
| 132 | + final UserRepository userRepository; |
| 133 | + |
| 134 | + @override |
| 135 | + Widget build(BuildContext context) { |
| 136 | + return Injector( |
| 137 | + todosInteractor: todosInteractor, |
| 138 | + userRepository: userRepository, |
| 139 | + child: TodosBlocProvider( |
| 140 | + bloc: TodosListBloc(todosInteractor), |
| 141 | + child: MaterialApp( |
| 142 | + localizationsDelegates: [ |
| 143 | + SimpleBlocLocalizationsDelegate(), |
| 144 | + ArchSampleLocalizationsDelegate(), |
| 145 | + ], |
| 146 | + home: const HomeScreen(), |
| 147 | + ), |
| 148 | + ), |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + static List<Todo> get _defaultTodos { |
| 153 | + return [ |
| 154 | + Todo('T1', id: '1', note: 'N1'), |
| 155 | + Todo('T2', id: '2'), |
| 156 | + Todo('T3', id: '3', complete: true), |
| 157 | + ]; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +Matcher isChecked(bool isChecked) { |
| 162 | + return matchesSemantics( |
| 163 | + isChecked: isChecked, |
| 164 | + hasTapAction: true, |
| 165 | + hasFocusAction: true, |
| 166 | + hasCheckedState: true, |
| 167 | + isFocusable: true, |
| 168 | + hasEnabledState: true, |
| 169 | + isEnabled: true, |
| 170 | + ); |
| 171 | +} |
0 commit comments