Skip to content

Commit

Permalink
Add more missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lockie Richter committed Jan 29, 2025
1 parent 338ae2a commit f24b6b7
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/providers/statistics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
109 changes: 109 additions & 0 deletions test/providers/statistics_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
}

0 comments on commit f24b6b7

Please sign in to comment.