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> { class LeaderboardBloc extends Bloc<LeaderboardEvent, LeaderboardState> {
/// {@macro leaderboard_bloc} /// {@macro leaderboard_bloc}
LeaderboardBloc(this._leaderboardRepository) LeaderboardBloc(this._leaderboardRepository)
: super(const LeaderboardState()) { : super(const LeaderboardState.initial()) {
on<Top10Fetched>(_onTop10Fetched); on<Top10Fetched>(_onTop10Fetched);
on<LeaderboardEntryAdded>(_onLeaderboardEntryAdded); on<LeaderboardEntryAdded>(_onLeaderboardEntryAdded);
} }

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

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

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

Loading…
Cancel
Save