Skip to content

Commit

Permalink
test: ensure NextEventRxPresenter map NextEventPlayer to doubt player
Browse files Browse the repository at this point in the history
  • Loading branch information
hmartiins committed Jan 5, 2025
1 parent fd258ef commit fb49f30
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
2 changes: 2 additions & 0 deletions test/helpers/fakes.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

String anyString() => Random().nextInt(1_000_000).toString();
bool anyBool() => Random().nextBool();
DateTime anyDateTime() => DateTime.fromMillisecondsSinceEpoch(Random().nextInt(1_000_000));
60 changes: 56 additions & 4 deletions test/presentation/rx/next_event_rx_presenter_test.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
@Timeout(Duration(seconds: 1))
library;

import 'package:advanced_flutter/domain/entities/next_event.dart';
import 'package:advanced_flutter/domain/entities/next_event_player.dart';
import 'package:advanced_flutter/presentation/presenters/next_event_presenter.dart';
import 'package:dartx/dartx.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:rxdart/subjects.dart';

import '../../helpers/fakes.dart';

final class NextEventRxPresenter {
final Future<void> Function({required String groupId}) nextEventLoader;
final Future<NextEvent> Function({required String groupId}) nextEventLoader;
final nextEventSubject = BehaviorSubject<NextEventViewModel>();
final isBusySubject = BehaviorSubject<bool>();

Expand All @@ -23,25 +26,53 @@ final class NextEventRxPresenter {
}) async {
try {
if (isReload) isBusySubject.add(true);
await nextEventLoader(groupId: groupId);
nextEventSubject.add(const NextEventViewModel());
final event = await nextEventLoader(groupId: groupId);
nextEventSubject.add(_mapEvent(event));
} catch (error) {
nextEventSubject.addError(error);
} finally {
if (isReload) isBusySubject.add(false);
}
}

NextEventViewModel _mapEvent(NextEvent event) => NextEventViewModel(
doubt: event.players
.where((player) => player.confirmationDate == null)
.sortedBy((player) => player.name)
.map(_mapPlayer)
.toList(),
);

NextEventPlayerViewModel _mapPlayer(NextEventPlayer player) =>
NextEventPlayerViewModel(
name: player.name,
initials: player.initials,
);
}

final class NextEventLoaderSpy {
int callsCount = 0;
String? groupId;
Error? error;
NextEvent output = NextEvent(
groupName: anyString(),
date: anyDateTime(),
players: [],
);

void simulatePlayers(List<NextEventPlayer> players) {
output = NextEvent(
groupName: anyString(),
date: anyDateTime(),
players: players,
);
}

Future<void> call({required String groupId}) async {
Future<NextEvent> call({required String groupId}) async {
callsCount++;
this.groupId = groupId;
if (error != null) throw error!;
return output;
}
}

Expand Down Expand Up @@ -93,4 +124,25 @@ void main() {
);
await sut.loadNextEvent(groupId: groupId);
});

test('should build doubt list sorted by name', () async {
nextEventLoader.simulatePlayers([
NextEventPlayer(id: anyString(), name: 'C', isConfirmed: anyBool()),
NextEventPlayer(id: anyString(), name: 'A', isConfirmed: anyBool()),
NextEventPlayer(
id: anyString(),
name: 'B',
isConfirmed: anyBool(),
confirmationDate: anyDateTime(),
),
NextEventPlayer(id: anyString(), name: 'D', isConfirmed: anyBool()),
]);
sut.nextEventStream.listen((event) {
expect(event.doubt.length, 3);
expect(event.doubt[0].name, 'A');
expect(event.doubt[1].name, 'C');
expect(event.doubt[2].name, 'D');
});
await sut.loadNextEvent(groupId: groupId);
});
}

0 comments on commit fb49f30

Please sign in to comment.