Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AyadLaouissi committed Apr 15, 2024
1 parent e4493af commit 0ca5f21
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 12 deletions.
3 changes: 1 addition & 2 deletions lib/leaderboard/view/leaderboard_success.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/player/bloc/player_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
onData: (data) {
return PlayerState(
status: PlayerStatus.playing,
leaderboardPlayer: data.$1,
player: data.$1,
rank: data.$2,
);
},
Expand Down
10 changes: 5 additions & 5 deletions lib/player/bloc/player_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object?> get props => [status, leaderboardPlayer, rank];
List<Object?> get props => [status, player, rank];
}
6 changes: 3 additions & 3 deletions test/leaderboard/view/leaderboard_success_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void main() {
when(() => playerBloc.state).thenReturn(
PlayerState(
rank: 3,
leaderboardPlayer: currentPlayer,
player: currentPlayer,
),
);

Expand Down Expand Up @@ -248,7 +248,7 @@ void main() {

when(() => playerBloc.state).thenReturn(
PlayerState(
leaderboardPlayer: player,
player: player,
rank: i,
),
);
Expand All @@ -273,7 +273,7 @@ void main() {

when(() => playerBloc.state).thenReturn(
PlayerState(
leaderboardPlayer: player,
player: player,
rank: 11,
),
);
Expand Down
2 changes: 1 addition & 1 deletion test/player/bloc/player_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void main() {
PlayerState(
status: PlayerStatus.playing,
rank: 3,
leaderboardPlayer: player,
player: player,
),
],
);
Expand Down
22 changes: 22 additions & 0 deletions test/player/bloc/player_event_test.dart
Original file line number Diff line number Diff line change
@@ -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'))),
);
});
});
});
}
81 changes: 81 additions & 0 deletions test/player/bloc/player_state_test.dart
Original file line number Diff line number Diff line change
@@ -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,
),
),
),
);
});
});
});
}

0 comments on commit 0ca5f21

Please sign in to comment.