|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:mockito/annotations.dart'; |
| 3 | +import 'package:mockito/mockito.dart'; |
| 4 | +import 'package:readeck_app/data/repository/bookmark/bookmark_repository.dart'; |
| 5 | +import 'package:readeck_app/data/repository/label/label_repository.dart'; |
| 6 | +import 'package:readeck_app/domain/models/bookmark/bookmark.dart'; |
| 7 | +import 'package:readeck_app/domain/models/bookmark_display_model/bookmark_display_model.dart'; |
| 8 | +import 'package:readeck_app/domain/use_cases/bookmark_operation_use_cases.dart'; |
| 9 | +import 'package:readeck_app/ui/bookmarks/view_models/bookmarks_viewmodel.dart'; |
| 10 | +import 'package:logger/logger.dart'; |
| 11 | +import 'package:readeck_app/main.dart'; |
| 12 | +import 'package:result_dart/result_dart.dart'; |
| 13 | + |
| 14 | +import 'reading_viewmodel_test.mocks.dart'; |
| 15 | + |
| 16 | +@GenerateMocks([BookmarkRepository, BookmarkOperationUseCases, LabelRepository]) |
| 17 | +void main() { |
| 18 | + late MockBookmarkRepository mockBookmarkRepository; |
| 19 | + late MockBookmarkOperationUseCases mockBookmarkOperationUseCases; |
| 20 | + late MockLabelRepository mockLabelRepository; |
| 21 | + late ReadingViewmodel readingViewmodel; |
| 22 | + |
| 23 | + setUpAll(() { |
| 24 | + appLogger = Logger(); |
| 25 | + provideDummy<ResultDart<List<BookmarkDisplayModel>, Exception>>( |
| 26 | + const Success([]), |
| 27 | + ); |
| 28 | + provideDummy<ResultDart<void, Exception>>( |
| 29 | + const Success(unit), |
| 30 | + ); |
| 31 | + provideDummy<ResultDart<List<String>, Exception>>( |
| 32 | + const Success([]), |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + setUp(() { |
| 37 | + mockBookmarkRepository = MockBookmarkRepository(); |
| 38 | + mockBookmarkOperationUseCases = MockBookmarkOperationUseCases(); |
| 39 | + mockLabelRepository = MockLabelRepository(); |
| 40 | + |
| 41 | + // Setup default mock behaviors |
| 42 | + when(mockBookmarkRepository.addListener(any)).thenAnswer((_) {}); |
| 43 | + when(mockLabelRepository.addListener(any)).thenAnswer((_) {}); |
| 44 | + when(mockLabelRepository.labelNames).thenReturn([]); |
| 45 | + }); |
| 46 | + |
| 47 | + group('ReadingViewmodel', () { |
| 48 | + test('should load reading bookmarks on initialization', () async { |
| 49 | + // Arrange |
| 50 | + final readingBookmarks = [ |
| 51 | + BookmarkDisplayModel( |
| 52 | + bookmark: Bookmark( |
| 53 | + id: '1', |
| 54 | + url: 'https://example.com/1', |
| 55 | + title: 'Reading Book 1', |
| 56 | + isArchived: false, |
| 57 | + isMarked: false, |
| 58 | + labels: [], |
| 59 | + created: DateTime.now(), |
| 60 | + readProgress: 25, |
| 61 | + ), |
| 62 | + ), |
| 63 | + BookmarkDisplayModel( |
| 64 | + bookmark: Bookmark( |
| 65 | + id: '2', |
| 66 | + url: 'https://example.com/2', |
| 67 | + title: 'Reading Book 2', |
| 68 | + isArchived: false, |
| 69 | + isMarked: false, |
| 70 | + labels: [], |
| 71 | + created: DateTime.now(), |
| 72 | + readProgress: 75, |
| 73 | + ), |
| 74 | + ), |
| 75 | + ]; |
| 76 | + |
| 77 | + when(mockBookmarkRepository.loadReadingBookmarks( |
| 78 | + limit: anyNamed('limit'), page: anyNamed('page'))) |
| 79 | + .thenAnswer((_) async => Success(readingBookmarks)); |
| 80 | + |
| 81 | + // Act |
| 82 | + readingViewmodel = ReadingViewmodel( |
| 83 | + mockBookmarkRepository, |
| 84 | + mockBookmarkOperationUseCases, |
| 85 | + mockLabelRepository, |
| 86 | + ); |
| 87 | + |
| 88 | + // Wait for the load command to complete |
| 89 | + await Future.delayed(Duration.zero); |
| 90 | + |
| 91 | + // Assert |
| 92 | + expect(readingViewmodel.bookmarks, readingBookmarks); |
| 93 | + verify(mockBookmarkRepository.loadReadingBookmarks(limit: 10, page: 1)) |
| 94 | + .called(1); |
| 95 | + }); |
| 96 | + |
| 97 | + test('should load more reading bookmarks when loadNextPage is called', |
| 98 | + () async { |
| 99 | + // Arrange |
| 100 | + final additionalBookmarks = [ |
| 101 | + BookmarkDisplayModel( |
| 102 | + bookmark: Bookmark( |
| 103 | + id: '11', |
| 104 | + url: 'https://example.com/11', |
| 105 | + title: 'Reading Book 11', |
| 106 | + isArchived: false, |
| 107 | + isMarked: false, |
| 108 | + labels: [], |
| 109 | + created: DateTime.now(), |
| 110 | + readProgress: 75, |
| 111 | + ), |
| 112 | + ), |
| 113 | + ]; |
| 114 | + |
| 115 | + // Make sure initial load returns 10 items to set hasMoreData = true |
| 116 | + final initialBookmarksWithFullPage = List.generate( |
| 117 | + 10, |
| 118 | + (index) => BookmarkDisplayModel( |
| 119 | + bookmark: Bookmark( |
| 120 | + id: '${index + 1}', |
| 121 | + url: 'https://example.com/${index + 1}', |
| 122 | + title: 'Reading Book ${index + 1}', |
| 123 | + isArchived: false, |
| 124 | + isMarked: false, |
| 125 | + labels: [], |
| 126 | + created: DateTime.now(), |
| 127 | + readProgress: 25, |
| 128 | + ), |
| 129 | + ), |
| 130 | + ); |
| 131 | + |
| 132 | + when(mockBookmarkRepository.loadReadingBookmarks(limit: 10, page: 1)) |
| 133 | + .thenAnswer((_) async => Success(initialBookmarksWithFullPage)); |
| 134 | + when(mockBookmarkRepository.loadReadingBookmarks(limit: 10, page: 2)) |
| 135 | + .thenAnswer((_) async => Success(additionalBookmarks)); |
| 136 | + |
| 137 | + readingViewmodel = ReadingViewmodel( |
| 138 | + mockBookmarkRepository, |
| 139 | + mockBookmarkOperationUseCases, |
| 140 | + mockLabelRepository, |
| 141 | + ); |
| 142 | + |
| 143 | + // Wait for initial load |
| 144 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 145 | + |
| 146 | + // Act |
| 147 | + readingViewmodel.loadNextPage(); |
| 148 | + |
| 149 | + // Wait for loadMore command to complete |
| 150 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 151 | + |
| 152 | + // Assert |
| 153 | + expect( |
| 154 | + readingViewmodel.bookmarks.length, 11); // 10 initial + 1 additional |
| 155 | + expect( |
| 156 | + readingViewmodel.bookmarks, |
| 157 | + containsAll( |
| 158 | + [...initialBookmarksWithFullPage, ...additionalBookmarks])); |
| 159 | + verify(mockBookmarkRepository.loadReadingBookmarks(limit: 10, page: 2)) |
| 160 | + .called(1); |
| 161 | + }); |
| 162 | + |
| 163 | + test('should call loadReadingBookmarks method from repository', () async { |
| 164 | + // Arrange |
| 165 | + when(mockBookmarkRepository.loadReadingBookmarks( |
| 166 | + limit: anyNamed('limit'), page: anyNamed('page'))) |
| 167 | + .thenAnswer((_) async => const Success([])); |
| 168 | + |
| 169 | + // Act |
| 170 | + readingViewmodel = ReadingViewmodel( |
| 171 | + mockBookmarkRepository, |
| 172 | + mockBookmarkOperationUseCases, |
| 173 | + mockLabelRepository, |
| 174 | + ); |
| 175 | + |
| 176 | + // Wait for initial load |
| 177 | + await Future.delayed(Duration.zero); |
| 178 | + |
| 179 | + // Assert |
| 180 | + verify(mockBookmarkRepository.loadReadingBookmarks(limit: 10, page: 1)) |
| 181 | + .called(1); |
| 182 | + }); |
| 183 | + }); |
| 184 | +} |
0 commit comments