refactor: leaderboard state initial method

pull/57/head
RuiAlonso 4 years ago
parent a7c24cdf62
commit fd3cf2d657

@ -15,7 +15,7 @@ part 'leaderboard_state.dart';
class LeaderboardBloc extends Bloc<LeaderboardEvent, LeaderboardState> {
/// {@macro leaderboard_bloc}
LeaderboardBloc(this._leaderboardRepository)
: super(const LeaderboardState()) {
: super(const LeaderboardState.initial()) {
on<Top10Fetched>(_onTop10Fetched);
on<LeaderboardEntryAdded>(_onLeaderboardEntryAdded);
}

@ -20,14 +20,19 @@ enum LeaderboardStatus {
class LeaderboardState extends Equatable {
/// {@macro leaderboard_state}
const LeaderboardState({
this.status = LeaderboardStatus.loading,
this.ranking = const LeaderboardRanking(
ranking: 0,
outOf: 0,
),
this.leaderboard = const [],
required this.status,
required this.ranking,
required this.leaderboard,
});
const LeaderboardState.initial()
: status = LeaderboardStatus.loading,
ranking = const LeaderboardRanking(
ranking: 0,
outOf: 0,
),
leaderboard = const [];
/// The current [LeaderboardStatus] of the state.
final LeaderboardStatus status;

@ -26,7 +26,7 @@ void main() {
});
group('Top10Fetched', () {
final top10Scores = [
const top10Scores = [
2500,
2200,
2200,
@ -61,7 +61,7 @@ void main() {
build: () => LeaderboardBloc(leaderboardRepository),
act: (bloc) => bloc.add(Top10Fetched()),
expect: () => [
const LeaderboardState(),
LeaderboardState.initial(),
isA<LeaderboardState>()
..having(
(element) => element.status,
@ -90,8 +90,8 @@ void main() {
build: () => LeaderboardBloc(leaderboardRepository),
act: (bloc) => bloc.add(Top10Fetched()),
expect: () => <LeaderboardState>[
const LeaderboardState(),
const LeaderboardState(status: LeaderboardStatus.error),
LeaderboardState.initial(),
LeaderboardState.initial().copyWith(status: LeaderboardStatus.error),
],
verify: (_) =>
verify(() => leaderboardRepository.fetchTop10Leaderboard())
@ -122,7 +122,7 @@ void main() {
build: () => LeaderboardBloc(leaderboardRepository),
act: (bloc) => bloc.add(LeaderboardEntryAdded(entry: leaderboardEntry)),
expect: () => [
const LeaderboardState(),
LeaderboardState.initial(),
isA<LeaderboardState>()
..having(
(element) => element.status,
@ -153,8 +153,8 @@ void main() {
build: () => LeaderboardBloc(leaderboardRepository),
act: (bloc) => bloc.add(LeaderboardEntryAdded(entry: leaderboardEntry)),
expect: () => <LeaderboardState>[
const LeaderboardState(),
const LeaderboardState(status: LeaderboardStatus.error),
LeaderboardState.initial(),
LeaderboardState.initial().copyWith(status: LeaderboardStatus.error),
],
verify: (_) => verify(
() => leaderboardRepository.addLeaderboardEntry(leaderboardEntry),

@ -8,9 +8,9 @@ void main() {
group('LeaderboardState', () {
test('supports value equality', () {
expect(
LeaderboardState(),
LeaderboardState.initial(),
equals(
LeaderboardState(),
LeaderboardState.initial(),
),
);
});
@ -18,7 +18,7 @@ void main() {
group('constructor', () {
test('can be instantiated', () {
expect(
LeaderboardState(),
LeaderboardState.initial(),
isNotNull,
);
});
@ -35,7 +35,7 @@ void main() {
'copies correctly '
'when no argument specified',
() {
const leaderboardState = LeaderboardState();
const leaderboardState = LeaderboardState.initial();
expect(
leaderboardState.copyWith(),
equals(leaderboardState),
@ -47,7 +47,7 @@ void main() {
'copies correctly '
'when all arguments specified',
() {
const leaderboardState = LeaderboardState();
const leaderboardState = LeaderboardState.initial();
final otherLeaderboardState = LeaderboardState(
status: LeaderboardStatus.success,
ranking: LeaderboardRanking(ranking: 0, outOf: 0),

Loading…
Cancel
Save