-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create top level challenge bloc to track the challenge progress (…
…#230) * feat: rename welcome bloc to challenge bloc * feat: convert board info retrieval to stream * test: board info repo
- Loading branch information
1 parent
4ef2cac
commit 6042856
Showing
23 changed files
with
220 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:board_info_repository/board_info_repository.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:rxdart/rxdart.dart'; | ||
|
||
part 'challenge_event.dart'; | ||
part 'challenge_state.dart'; | ||
|
||
class ChallengeBloc extends Bloc<ChallengeEvent, ChallengeState> { | ||
ChallengeBloc({ | ||
required BoardInfoRepository boardInfoRepository, | ||
}) : _boardInfoRepository = boardInfoRepository, | ||
super(const ChallengeState.initial()) { | ||
on<ChallengeDataRequested>(_onDataRequested); | ||
} | ||
|
||
final BoardInfoRepository _boardInfoRepository; | ||
|
||
Future<void> _onDataRequested( | ||
ChallengeDataRequested event, | ||
Emitter<ChallengeState> emit, | ||
) async { | ||
return emit.forEach( | ||
Rx.combineLatest2( | ||
_boardInfoRepository.getSolvedWordsCount(), | ||
_boardInfoRepository.getTotalWordsCount(), | ||
(solved, total) => state.copyWith( | ||
solvedWords: solved, | ||
totalWords: total, | ||
), | ||
), | ||
onData: (state) => state, | ||
onError: (error, stackTrace) { | ||
addError(error, stackTrace); | ||
return state; | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
part of 'challenge_bloc.dart'; | ||
|
||
sealed class ChallengeEvent extends Equatable { | ||
const ChallengeEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
/// Requests the data needed for the challenge progress. | ||
class ChallengeDataRequested extends ChallengeEvent { | ||
const ChallengeDataRequested(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'bloc/challenge_bloc.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'package:bloc_test/bloc_test.dart'; | ||
import 'package:board_info_repository/board_info_repository.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:io_crossword/challenge/challenge.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
class _MockBoardInfoRepository extends Mock implements BoardInfoRepository {} | ||
|
||
void main() { | ||
group('$ChallengeBloc', () { | ||
late BoardInfoRepository boardInfoRepository; | ||
|
||
setUp(() { | ||
boardInfoRepository = _MockBoardInfoRepository(); | ||
}); | ||
|
||
test('initial state is ChallengeState.initial', () { | ||
expect( | ||
ChallengeBloc(boardInfoRepository: boardInfoRepository).state, | ||
equals(const ChallengeState.initial()), | ||
); | ||
}); | ||
|
||
blocTest<ChallengeBloc, ChallengeState>( | ||
'remains the same when retrieval fails', | ||
seed: () => const ChallengeState(solvedWords: 1, totalWords: 2), | ||
build: () => ChallengeBloc(boardInfoRepository: boardInfoRepository), | ||
act: (bloc) { | ||
when(() => boardInfoRepository.getSolvedWordsCount()) | ||
.thenAnswer((_) => Stream.error(Exception())); | ||
when(() => boardInfoRepository.getTotalWordsCount()) | ||
.thenAnswer((_) => Stream.error(Exception())); | ||
bloc.add(const ChallengeDataRequested()); | ||
}, | ||
expect: () => <ChallengeState>[], | ||
); | ||
|
||
blocTest<ChallengeBloc, ChallengeState>( | ||
'emits ChallengeState with updated values when data is requested', | ||
build: () => ChallengeBloc(boardInfoRepository: boardInfoRepository), | ||
act: (bloc) { | ||
when(() => boardInfoRepository.getSolvedWordsCount()) | ||
.thenAnswer((_) => Stream.value(1)); | ||
when(() => boardInfoRepository.getTotalWordsCount()) | ||
.thenAnswer((_) => Stream.value(2)); | ||
bloc.add(const ChallengeDataRequested()); | ||
}, | ||
expect: () => <ChallengeState>[ | ||
const ChallengeState(solvedWords: 1, totalWords: 2), | ||
], | ||
); | ||
}); | ||
} |
Oops, something went wrong.