From 89043d343dc81782d21a8dfc170c3c639ce3f17b Mon Sep 17 00:00:00 2001 From: Augsorn Chanklad Date: Wed, 8 May 2024 21:30:04 +0700 Subject: [PATCH] Add unit test album list filter screen --- .../albums_list_filter_screen.dart | 2 +- .../albums_list_filter_screen_robot.dart | 56 +++++++++++++++++++ .../albums_list/albums_list_screen_test.dart | 31 ++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/src/features/albums/presentation/albums_list/albums_list_filter_screen_robot.dart create mode 100644 test/src/features/albums/presentation/albums_list/albums_list_screen_test.dart diff --git a/lib/src/features/albums/presentation/albums_list/albums_list_filter_screen.dart b/lib/src/features/albums/presentation/albums_list/albums_list_filter_screen.dart index 12467131..f0718bde 100644 --- a/lib/src/features/albums/presentation/albums_list/albums_list_filter_screen.dart +++ b/lib/src/features/albums/presentation/albums_list/albums_list_filter_screen.dart @@ -26,7 +26,7 @@ class AlbumsFilterScreen extends StatelessWidget { return ListView( children: [ DropdownAlbumType( - value: state.discTypes ?? '', + value: state.discTypes ?? 'Unknown', onChanged: (value) => onAlbumTypesChanged?.call(value) ?? ref diff --git a/test/src/features/albums/presentation/albums_list/albums_list_filter_screen_robot.dart b/test/src/features/albums/presentation/albums_list/albums_list_filter_screen_robot.dart new file mode 100644 index 00000000..d100bdab --- /dev/null +++ b/test/src/features/albums/presentation/albums_list/albums_list_filter_screen_robot.dart @@ -0,0 +1,56 @@ +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/presentation/albums_list/albums_list_filter_screen.dart'; +import 'package:vocadb_app/src/features/albums/presentation/widgets/dropdown_album_sort.dart'; +import 'package:vocadb_app/src/features/albums/presentation/widgets/dropdown_album_type.dart'; + +class AlbumsListFilterScreenRobot { + final WidgetTester tester; + + AlbumsListFilterScreenRobot(this.tester); + + Future pumpAlbumsListFilterScreen({ + Function(String?)? onAlbumsTypesChanged, + Function(String?)? onSortChanged, + }) async { + await tester.pumpWidget( + ProviderScope( + child: MaterialApp( + home: AlbumsFilterScreen( + onAlbumTypesChanged: onAlbumsTypesChanged, + onSortChanged: onSortChanged, + ), + ), + ), + ); + } + + Future selectAlbumTypes(String value) async { + final albumTypeDropdownBtnFinder = + find.byKey(DropdownAlbumType.dropdownKey); + expect(albumTypeDropdownBtnFinder, findsOneWidget); + + await tester.tap(albumTypeDropdownBtnFinder); + await tester.pump(); + + final selectedFinder = find.text(value).last; + expect(selectedFinder, findsOneWidget); + await tester.tap(selectedFinder); + await tester.pump(); + } + + Future selectSort(String value) async { + final albumSortDropdownBtnFinder = + find.byKey(DropdownAlbumSort.dropdownKey); + expect(albumSortDropdownBtnFinder, findsOneWidget); + + await tester.tap(albumSortDropdownBtnFinder); + await tester.pump(); + + final selectedFinder = find.text(value).last; + expect(selectedFinder, findsOneWidget); + await tester.tap(selectedFinder); + await tester.pump(); + } +} 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 new file mode 100644 index 00000000..9ad5f53e --- /dev/null +++ b/test/src/features/albums/presentation/albums_list/albums_list_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'); + }); + }); +}