From b055fdf0f0acddac9e73e02ff42a445ed1995c59 Mon Sep 17 00:00:00 2001 From: Augsorn Chanklad Date: Wed, 8 May 2024 22:03:02 +0700 Subject: [PATCH] Add unit test album list screen --- .../albums_list_filter_screen_test.dart | 31 ++++++++++++++ .../albums_list/albums_list_screen_robot.dart | 39 ++++++++++++++++++ .../albums_list/albums_list_screen_test.dart | 41 ++++++++++--------- test/src/mocks.dart | 3 ++ 4 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 test/src/features/albums/presentation/albums_list/albums_list_filter_screen_test.dart create mode 100644 test/src/features/albums/presentation/albums_list/albums_list_screen_robot.dart diff --git a/test/src/features/albums/presentation/albums_list/albums_list_filter_screen_test.dart b/test/src/features/albums/presentation/albums_list/albums_list_filter_screen_test.dart new file mode 100644 index 00000000..9ad5f53e --- /dev/null +++ b/test/src/features/albums/presentation/albums_list/albums_list_filter_screen_test.dart @@ -0,0 +1,31 @@ +import 'package:flutter_test/flutter_test.dart'; + +import 'albums_list_filter_screen_robot.dart'; + +void main() { + group('albums filter screen', () { + testWidgets('change album type', (tester) async { + String? selectedValue; + final r = AlbumsListFilterScreenRobot(tester); + + await r.pumpAlbumsListFilterScreen( + onAlbumsTypesChanged: (value) => selectedValue = value, + ); + + await r.selectAlbumTypes('E.P.'); + expect(selectedValue, 'EP'); + }); + + testWidgets('change album sort', (tester) async { + String? selectedValue; + final r = AlbumsListFilterScreenRobot(tester); + + await r.pumpAlbumsListFilterScreen( + onAlbumsTypesChanged: (value) => selectedValue = value, + ); + + await r.selectSort('Addition date'); + expect(selectedValue, 'AdditionDate'); + }); + }); +} diff --git a/test/src/features/albums/presentation/albums_list/albums_list_screen_robot.dart b/test/src/features/albums/presentation/albums_list/albums_list_screen_robot.dart new file mode 100644 index 00000000..d54f9215 --- /dev/null +++ b/test/src/features/albums/presentation/albums_list/albums_list_screen_robot.dart @@ -0,0 +1,39 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:vocadb_app/src/features/albums/data/album_repository.dart'; +import 'package:vocadb_app/src/features/albums/presentation/album_tile/album_tile.dart'; +import 'package:vocadb_app/src/features/albums/presentation/albums_list/albums_list_screen.dart'; +import 'package:vocadb_app/src/features/settings/data/user_settings_repository.dart'; + +class AlbumsListScreenRobot { + final WidgetTester tester; + + AlbumsListScreenRobot(this.tester); + + Future pumpAlbumsListScreen( + {AlbumRepository? albumRepository}) async { + await tester.pumpWidget( + ProviderScope( + overrides: [ + if (albumRepository != null) + albumRepositoryProvider.overrideWithValue(albumRepository), + userSettingsRepositoryProvider + .overrideWithValue(UserSettingsRepository()) + ], + child: const MaterialApp( + home: AlbumsListScreen(), + ), + ), + ); + + await tester.pump(); + await tester.pump(); + await tester.pump(); + } + + Future expectAlbumsDisplayCountAtLeast(int count) async { + final finder = find.byType(AlbumTile); + expect(finder, findsAtLeastNWidgets(count)); + } +} diff --git a/test/src/features/albums/presentation/albums_list/albums_list_screen_test.dart b/test/src/features/albums/presentation/albums_list/albums_list_screen_test.dart index 9ad5f53e..64b4a4aa 100644 --- a/test/src/features/albums/presentation/albums_list/albums_list_screen_test.dart +++ b/test/src/features/albums/presentation/albums_list/albums_list_screen_test.dart @@ -1,31 +1,32 @@ import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:vocadb_app/src/features/albums/data/constants/fake_albums_list.dart'; +import 'package:vocadb_app/src/features/albums/domain/albums_list_params.dart'; -import 'albums_list_filter_screen_robot.dart'; +import '../../../../mocks.dart'; +import 'albums_list_screen_robot.dart'; void main() { - group('albums filter screen', () { - testWidgets('change album type', (tester) async { - String? selectedValue; - final r = AlbumsListFilterScreenRobot(tester); + testWidgets('albums list screen ...', (tester) async { + registerFallbackValue(FakeAlbumsListParams()); - await r.pumpAlbumsListFilterScreen( - onAlbumsTypesChanged: (value) => selectedValue = value, - ); + final r = AlbumsListScreenRobot(tester); + final albumRepository = MockAlbumRepository(); - await r.selectAlbumTypes('E.P.'); - expect(selectedValue, 'EP'); - }); + when(() => albumRepository.fetchAlbums( + params: any(named: 'params', that: isNotNull), + )).thenAnswer((_) => Future.value(kFakeAlbumsList)); - testWidgets('change album sort', (tester) async { - String? selectedValue; - final r = AlbumsListFilterScreenRobot(tester); + await r.pumpAlbumsListScreen(albumRepository: albumRepository); - await r.pumpAlbumsListFilterScreen( - onAlbumsTypesChanged: (value) => selectedValue = value, - ); + await r.expectAlbumsDisplayCountAtLeast(3); - await r.selectSort('Addition date'); - expect(selectedValue, 'AdditionDate'); - }); + expect( + verify(() => + albumRepository.fetchAlbums(params: captureAny(named: 'params'))) + .captured, + [ + const AlbumsListParams(), + ]); }); } diff --git a/test/src/mocks.dart b/test/src/mocks.dart index 08d8929f..5a194c9b 100644 --- a/test/src/mocks.dart +++ b/test/src/mocks.dart @@ -2,6 +2,7 @@ import 'package:hive/hive.dart'; import 'package:http/http.dart'; import 'package:mocktail/mocktail.dart'; import 'package:vocadb_app/src/features/albums/data/album_repository.dart'; +import 'package:vocadb_app/src/features/albums/domain/albums_list_params.dart'; import 'package:vocadb_app/src/features/api/api_cache.dart'; import 'package:vocadb_app/src/features/api/api_client.dart'; import 'package:vocadb_app/src/features/api/data/cookie_storage.dart'; @@ -37,6 +38,8 @@ class RatedSongsListParamsFake extends Fake implements RatedSongsListParams {} class FakeArtistsListParams extends Fake implements ArtistsListParams {} +class FakeAlbumsListParams extends Fake implements AlbumsListParams {} + class FakeSongsListParams extends Fake implements SongsListParams {} class MockUserSettingsRepository extends Mock