From f24b6b744e174cfffdc1c633f41315b233f7fd76 Mon Sep 17 00:00:00 2001 From: Lockie Richter Date: Wed, 29 Jan 2025 21:50:34 +1030 Subject: [PATCH] Add more missing tests --- lib/providers/statistics.dart | 6 +- test/providers/statistics_test.dart | 109 ++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 test/providers/statistics_test.dart diff --git a/lib/providers/statistics.dart b/lib/providers/statistics.dart index 0a61bb5..ded16ee 100644 --- a/lib/providers/statistics.dart +++ b/lib/providers/statistics.dart @@ -11,10 +11,11 @@ import 'package:riverpod_annotation/riverpod_annotation.dart'; part 'statistics.g.dart'; +typedef BookCount = ({int readLaterCount, int readingCount, int readCount}); const zeroBookCount = (readLaterCount: 0, readingCount: 0, readCount: 0); @riverpod -({int readLaterCount, int readingCount, int readCount}) bookCounts(Ref ref) { +BookCount bookCounts(Ref ref) { final books = ref.watch(allBooksProvider); return books.when( @@ -37,10 +38,11 @@ const zeroBookCount = (readLaterCount: 0, readingCount: 0, readCount: 0); ); } +typedef PageCount = ({int pagesWaiting, int pagesRead}); const zeroPageCount = (pagesWaiting: 0, pagesRead: 0); @riverpod -({int pagesWaiting, int pagesRead}) pageCounts(Ref ref) { +PageCount pageCounts(Ref ref) { final books = ref.watch(allBooksProvider); return books.when( diff --git a/test/providers/statistics_test.dart b/test/providers/statistics_test.dart new file mode 100644 index 0000000..39284d9 --- /dev/null +++ b/test/providers/statistics_test.dart @@ -0,0 +1,109 @@ +import 'package:dantex/models/book_state.dart'; +import 'package:dantex/providers/book.dart'; +import 'package:dantex/providers/statistics.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../test_utilities.dart'; + +void main() { + group('Given a bookCountsProvider', () { + group('When the allBooksProvider has data', () { + test('Then the correct book counts are returned', () async { + final mockBooks = List.generate( + 5, + (index) => getMockBook().copyWith( + id: 'book-$index', + state: index == 0 + ? BookState.wishlist + : index == 1 + ? BookState.readLater + : index == 2 + ? BookState.reading + : BookState.read, + ), + ); + final container = createContainer( + overrides: [ + allBooksProvider.overrideWith((ref) => Stream.value(mockBooks)), + ], + ); + final subscription = container.listen( + bookCountsProvider, + (_, __) {}, + ); + await container.pump(); + + final stats = subscription.read(); + expect(stats, (readLaterCount: 1, readingCount: 1, readCount: 2)); + }); + }); + group('When the allBooksDataProvider has an error', () { + test('Then zero book counts are returned', () async { + final container = createContainer( + overrides: [ + allBooksProvider.overrideWith((ref) => Stream.error('error')), + ], + ); + final subscription = container.listen( + bookCountsProvider, + (_, __) {}, + ); + await container.pump(); + + final stats = subscription.read(); + expect(stats, zeroBookCount); + }); + }); + }); + + group('Given a pageCountsProvider', () { + group('When the allBooksProvider has data', () { + test('Then the correct page counts are returned', () async { + final mockBooks = List.generate( + 5, + (index) => getMockBook().copyWith( + id: 'book-$index', + state: index == 0 + ? BookState.wishlist + : index == 1 + ? BookState.readLater + : index == 2 + ? BookState.reading + : BookState.read, + pageCount: 10, + ), + ); + final container = createContainer( + overrides: [ + allBooksProvider.overrideWith((ref) => Stream.value(mockBooks)), + ], + ); + final subscription = container.listen( + pageCountsProvider, + (_, __) {}, + ); + await container.pump(); + + final stats = subscription.read(); + expect(stats, (pagesWaiting: 10, pagesRead: 20)); + }); + }); + group('When the allBooksDataProvider has an error', () { + test('Then zero page counts are returned', () async { + final container = createContainer( + overrides: [ + allBooksProvider.overrideWith((ref) => Stream.error('error')), + ], + ); + final subscription = container.listen( + pageCountsProvider, + (_, __) {}, + ); + await container.pump(); + + final stats = subscription.read(); + expect(stats, zeroPageCount); + }); + }); + }); +}