From fb49f301b59a657c2e11d2738b36d7ad1411eafd Mon Sep 17 00:00:00 2001 From: hmartiins Date: Sun, 5 Jan 2025 17:36:18 -0300 Subject: [PATCH] test: ensure NextEventRxPresenter map NextEventPlayer to doubt player --- test/helpers/fakes.dart | 2 + .../rx/next_event_rx_presenter_test.dart | 60 +++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/test/helpers/fakes.dart b/test/helpers/fakes.dart index 420e83b..514e135 100644 --- a/test/helpers/fakes.dart +++ b/test/helpers/fakes.dart @@ -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)); diff --git a/test/presentation/rx/next_event_rx_presenter_test.dart b/test/presentation/rx/next_event_rx_presenter_test.dart index 4e83b8e..88bcc6d 100644 --- a/test/presentation/rx/next_event_rx_presenter_test.dart +++ b/test/presentation/rx/next_event_rx_presenter_test.dart @@ -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 Function({required String groupId}) nextEventLoader; + final Future Function({required String groupId}) nextEventLoader; final nextEventSubject = BehaviorSubject(); final isBusySubject = BehaviorSubject(); @@ -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 players) { + output = NextEvent( + groupName: anyString(), + date: anyDateTime(), + players: players, + ); + } - Future call({required String groupId}) async { + Future call({required String groupId}) async { callsCount++; this.groupId = groupId; if (error != null) throw error!; + return output; } } @@ -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); + }); }