From 0ca5f2161389b6f0e2cf2e7a797b5d085b74f55b Mon Sep 17 00:00:00 2001 From: AyadLaouissi Date: Mon, 15 Apr 2024 13:25:35 +0200 Subject: [PATCH] updated tests --- lib/leaderboard/view/leaderboard_success.dart | 3 +- lib/player/bloc/player_bloc.dart | 2 +- lib/player/bloc/player_state.dart | 10 +-- .../view/leaderboard_success_test.dart | 6 +- test/player/bloc/player_bloc_test.dart | 2 +- test/player/bloc/player_event_test.dart | 22 +++++ test/player/bloc/player_state_test.dart | 81 +++++++++++++++++++ 7 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 test/player/bloc/player_event_test.dart create mode 100644 test/player/bloc/player_state_test.dart diff --git a/lib/leaderboard/view/leaderboard_success.dart b/lib/leaderboard/view/leaderboard_success.dart index 367ed9c73..b5a76ce89 100644 --- a/lib/leaderboard/view/leaderboard_success.dart +++ b/lib/leaderboard/view/leaderboard_success.dart @@ -115,8 +115,7 @@ class CurrentPlayerNotTopRank extends StatelessWidget { @override Widget build(BuildContext context) { - final player = - context.select((PlayerBloc bloc) => bloc.state.leaderboardPlayer); + final player = context.select((PlayerBloc bloc) => bloc.state.player); final rank = context.select( (PlayerBloc bloc) => bloc.state.rank, diff --git a/lib/player/bloc/player_bloc.dart b/lib/player/bloc/player_bloc.dart index 9654b83aa..8b082b0dc 100644 --- a/lib/player/bloc/player_bloc.dart +++ b/lib/player/bloc/player_bloc.dart @@ -25,7 +25,7 @@ class PlayerBloc extends Bloc { onData: (data) { return PlayerState( status: PlayerStatus.playing, - leaderboardPlayer: data.$1, + player: data.$1, rank: data.$2, ); }, diff --git a/lib/player/bloc/player_state.dart b/lib/player/bloc/player_state.dart index da09a6375..6b590e0a4 100644 --- a/lib/player/bloc/player_state.dart +++ b/lib/player/bloc/player_state.dart @@ -10,26 +10,26 @@ enum PlayerStatus { class PlayerState extends Equatable { const PlayerState({ this.status = PlayerStatus.onboarding, - this.leaderboardPlayer = LeaderboardPlayer.empty, + this.player = LeaderboardPlayer.empty, this.rank = 0, }); PlayerState copyWith({ PlayerStatus? status, - LeaderboardPlayer? leaderboardPlayer, + LeaderboardPlayer? player, int? rank, }) { return PlayerState( status: status ?? this.status, - leaderboardPlayer: leaderboardPlayer ?? this.leaderboardPlayer, + player: player ?? this.player, rank: rank ?? this.rank, ); } final PlayerStatus status; - final LeaderboardPlayer leaderboardPlayer; + final LeaderboardPlayer player; final int rank; @override - List get props => [status, leaderboardPlayer, rank]; + List get props => [status, player, rank]; } diff --git a/test/leaderboard/view/leaderboard_success_test.dart b/test/leaderboard/view/leaderboard_success_test.dart index 54ba54f64..694c1e10f 100644 --- a/test/leaderboard/view/leaderboard_success_test.dart +++ b/test/leaderboard/view/leaderboard_success_test.dart @@ -129,7 +129,7 @@ void main() { when(() => playerBloc.state).thenReturn( PlayerState( rank: 3, - leaderboardPlayer: currentPlayer, + player: currentPlayer, ), ); @@ -248,7 +248,7 @@ void main() { when(() => playerBloc.state).thenReturn( PlayerState( - leaderboardPlayer: player, + player: player, rank: i, ), ); @@ -273,7 +273,7 @@ void main() { when(() => playerBloc.state).thenReturn( PlayerState( - leaderboardPlayer: player, + player: player, rank: 11, ), ); diff --git a/test/player/bloc/player_bloc_test.dart b/test/player/bloc/player_bloc_test.dart index b134244ae..5b1c38772 100644 --- a/test/player/bloc/player_bloc_test.dart +++ b/test/player/bloc/player_bloc_test.dart @@ -43,7 +43,7 @@ void main() { PlayerState( status: PlayerStatus.playing, rank: 3, - leaderboardPlayer: player, + player: player, ), ], ); diff --git a/test/player/bloc/player_event_test.dart b/test/player/bloc/player_event_test.dart new file mode 100644 index 000000000..1cb7922d5 --- /dev/null +++ b/test/player/bloc/player_event_test.dart @@ -0,0 +1,22 @@ +// ignore_for_file: prefer_const_constructors + +import 'package:flutter_test/flutter_test.dart'; +import 'package:io_crossword/player/bloc/player_bloc.dart'; + +void main() { + group('$PlayerEvent', () { + group('$PlayerLoaded', () { + test('checks equality', () { + expect( + PlayerLoaded(userId: 'user-id'), + equals(PlayerLoaded(userId: 'user-id')), + ); + + expect( + PlayerLoaded(userId: 'user-id'), + isNot(equals(PlayerLoaded(userId: 'user-id-1'))), + ); + }); + }); + }); +} diff --git a/test/player/bloc/player_state_test.dart b/test/player/bloc/player_state_test.dart new file mode 100644 index 000000000..cabc166f0 --- /dev/null +++ b/test/player/bloc/player_state_test.dart @@ -0,0 +1,81 @@ +// ignore_for_file: prefer_const_constructors +// ignore_for_file: prefer_const_literals_to_create_immutables + +import 'package:flutter_test/flutter_test.dart'; +import 'package:game_domain/game_domain.dart'; +import 'package:io_crossword/player/bloc/player_bloc.dart'; + +void main() { + group('$PlayerState', () { + test('supports value comparisons', () { + expect( + PlayerState(rank: 4, status: PlayerStatus.failure), + equals(PlayerState(rank: 4, status: PlayerStatus.failure)), + ); + + expect( + PlayerState(status: PlayerStatus.failure), + isNot(equals(PlayerState())), + ); + + expect( + PlayerState( + player: LeaderboardPlayer( + userId: '1', + initials: 'AAA', + score: 100, + streak: 2, + mascot: Mascots.dash, + ), + ), + isNot(equals(PlayerState())), + ); + + expect( + PlayerState(rank: 4), + isNot(equals(PlayerState())), + ); + }); + + group('copyWith', () { + test('updates status', () { + expect( + PlayerState().copyWith(status: PlayerStatus.playing), + equals(PlayerState(status: PlayerStatus.playing)), + ); + }); + + test('updates status', () { + expect( + PlayerState().copyWith(rank: 5), + equals(PlayerState(rank: 5)), + ); + }); + + test('updates players', () { + expect( + PlayerState().copyWith( + player: LeaderboardPlayer( + userId: '1', + initials: 'AAA', + score: 100, + streak: 2, + mascot: Mascots.dash, + ), + ), + equals( + PlayerState( + player: LeaderboardPlayer( + userId: '1', + initials: 'AAA', + score: 100, + streak: 2, + mascot: Mascots.dash, + ), + ), + ), + ); + }); + }); + }); +}