Skip to content

Commit

Permalink
Add unit test album list screen
Browse files Browse the repository at this point in the history
  • Loading branch information
up2code committed May 8, 2024
1 parent 89043d3 commit b055fdf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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');
});
});
}
Original file line number Diff line number Diff line change
@@ -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<void> 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<void> expectAlbumsDisplayCountAtLeast(int count) async {
final finder = find.byType(AlbumTile);
expect(finder, findsAtLeastNWidgets(count));
}
}
Original file line number Diff line number Diff line change
@@ -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(),
]);
});
}
3 changes: 3 additions & 0 deletions test/src/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b055fdf

Please sign in to comment.